Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 2 additions & 27 deletions apps/mail/components/create/email-composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ export function EmailComposer({
const [isComposeOpen, setIsComposeOpen] = useQueryState('isComposeOpen');
const { data: emailData } = useThread(threadId ?? null);
const { data: session } = useSession();
const [urlDraftId] = useQueryState('draftId');
const [draftId, setDraftId] = useState<string | null>(urlDraftId ?? null);
const [draftId, setDraftId] = useQueryState('draftId');
const [aiGeneratedMessage, setAiGeneratedMessage] = useState<string | null>(null);
const [aiIsLoading, setAiIsLoading] = useState(false);
const [isGeneratingSubject, setIsGeneratingSubject] = useState(false);
Expand Down Expand Up @@ -341,24 +340,6 @@ export function EmailComposer({
}
};

// It needs to be done this way so that react doesn't catch on to the state change
// and we can still refresh to get the latest draft for the reply.
const setDraftIdQueryParam = (draftId: string | null) => {
const url = new URL(window.location.href);

// mutate only one key
draftId == null ? url.searchParams.delete('draftId') : url.searchParams.set('draftId', draftId);

// keep Next's internal state intact and update its mirrors
const nextState = {
...window.history.state, // preserves __NA / _N etc.
as: url.pathname + url.search,
url: url.pathname + url.search,
};
setDraftId(draftId);
window.history.replaceState(nextState, '', url);
};

const saveDraft = async () => {
const values = getValues();

Expand All @@ -383,7 +364,7 @@ export function EmailComposer({
const response = await createDraft(draftData);

if (response?.id && response.id !== draftId) {
setDraftIdQueryParam(response.id);
setDraftId(response.id);
}
} catch (error) {
console.error('Error saving draft:', error);
Expand All @@ -403,12 +384,6 @@ export function EmailComposer({
setIsGeneratingSubject(false);
};

useEffect(() => {
if (urlDraftId !== draftId) {
setDraftId(urlDraftId ?? null);
}
}, [urlDraftId]);

useEffect(() => {
if (!hasUnsavedChanges) return;

Expand Down
5 changes: 0 additions & 5 deletions apps/mail/components/mail/reply-composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,6 @@ export default function ReplyCompose({ messageId }: ReplyComposeProps) {

if (!mode || !emailData) return null;

if (draftId && isDraftLoading) {
// wait for the draft if requesting one
return null;
}

return (
<div className="w-full rounded-xl">
<EmailComposer
Expand Down