Skip to content
Python

fix: preserve drafts across page reload and StrictMode cleanup

cyureklif24b3fd40df3
diff --git a/surfaces/gui/e2e/draft-reload.spec.ts b/surfaces/gui/e2e/draft-reload.spec.ts
new file mode 100644
index 00000000..4735eb8f
--- /dev/null
+++ b/surfaces/gui/e2e/draft-reload.spec.ts
@@ -0,0 +1,8 @@
+import { expect } from '@playwright/test';
+import { test } from './fixtures';
+test('unsent draft survives reload', async ({page}) => {
+ await page.goto('/'); await page.getByTitle('Draft the launch note').first().click();
+ const box=page.getByPlaceholder(/Ask the coworker/);await box.fill('draft before reload');
+ await page.reload(); await page.getByTitle('Draft the launch note').first().click();
+ await expect(box).toHaveValue('draft before reload');
+});
diff --git a/surfaces/gui/src/components/Composer.tsx b/surfaces/gui/src/components/Composer.tsx
index 326f99e7..0e2a4a99 100644
--- a/surfaces/gui/src/components/Composer.tsx
+++ b/surfaces/gui/src/components/Composer.tsx
@@ -254,6 +254,9 @@ export function Composer(props: Props) {
if (prev && prev !== next) saveDraft(prev, draftRef.current);
// Restore the incoming session's saved draft (none → empty box).
const restored = loadDraft(next);
+ // StrictMode replays effect cleanup before the restored state renders. Keep
+ // that cleanup from persisting the initial empty state over the saved draft.
+ draftRef.current = restored ?? { text: "", attachments: [], skill: null };
setText(restored?.text ?? "");
setAttachments(restored?.attachments ?? []);
setPendingSkill(restored?.skill ?? null);
@@ -263,9 +266,16 @@ export function Composer(props: Props) {
// Save the draft when the composer unmounts (e.g. navigating to Settings/Inbox or closing
// the session view), so it survives leaving the page and is restored when you come back.
useEffect(() => {
- return () => {
+ const persist = () => {
if (prevResetKey.current) saveDraft(prevResetKey.current, draftRef.current);
};
+ window.addEventListener("pagehide", persist);
+ window.addEventListener("beforeunload", persist);
+ return () => {
+ persist();
+ window.removeEventListener("pagehide", persist);
+ window.removeEventListener("beforeunload", persist);
+ };
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);