From be94ba0520bcbdeb7afb47acd412a5d103a8f3bf Mon Sep 17 00:00:00 2001 From: spark Date: Sat, 17 Oct 2026 11:23:01 +0000 Subject: [PATCH] Library: remember the chosen delivery target across tab switches SendButton wrote the selected target to prefs only on a successful send, so picking a target and then leaving the Library tab unmounted the button, and on return it reloaded the last *sent* target from prefs: a target chosen but not yet sent silently reverted (e.g. FPA back to ZZ), risking a send to the wrong client. Persist the choice the moment the dropdown changes, and also when a stored-but-removed target falls back to the first. Test covers survival across a remount. Claude-Session: https://claude.ai/code/session_01RAkmwWv6L5KmpX4h9JPD2i --- apps/app/src/library/SendButton.test.tsx | 26 ++++++++++++++++++++++++ apps/app/src/library/SendButton.tsx | 7 +++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/apps/app/src/library/SendButton.test.tsx b/apps/app/src/library/SendButton.test.tsx index 3f7b068..fe31185 100644 --- a/apps/app/src/library/SendButton.test.tsx +++ b/apps/app/src/library/SendButton.test.tsx @@ -24,4 +24,30 @@ describe("send button", () => { fireEvent.click(await screen.findByRole("button", { name: "Enviar de novo a que já tinha ido" })); await waitFor(() => expect(calls).toEqual([[["a", "b"], false], [["a"], true]])); }); + + it("remembers the chosen destination across a remount (tab switch)", () => { + try { + window.localStorage.clear(); + } catch { + /* jsdom without storage */ + } + const api = {} as unknown as ApiClient; + const targets = [ + { id: "zz", name: "ZZ", kind: "ZEROZERO" }, + { id: "fpa", name: "FPA", kind: "NEXTCLOUD" }, + ] as unknown as DeliveryTarget[]; + const view = render( + + {}} /> + , + ); + fireEvent.change(screen.getByRole("combobox"), { target: { value: "fpa" } }); + view.unmount(); + render( + + {}} /> + , + ); + expect((screen.getByRole("combobox") as HTMLSelectElement).value).toBe("fpa"); + }); }); diff --git a/apps/app/src/library/SendButton.tsx b/apps/app/src/library/SendButton.tsx index bf6e52e..0316b77 100644 --- a/apps/app/src/library/SendButton.tsx +++ b/apps/app/src/library/SendButton.tsx @@ -26,7 +26,10 @@ export function SendButton(props: { api: ApiClient; orgId: string; selection: Ph const [busy, setBusy] = useState(false); const [note, setNote] = useState(null); useEffect(() => { - if (targets.length && !targets.some((x) => x.id === targetId)) setTargetId(targets[0]!.id); + if (targets.length && !targets.some((x) => x.id === targetId)) { + setTargetId(targets[0]!.id); + prefs.set(LAST_TARGET, targets[0]!.id); + } }, [targets, targetId]); useEffect(() => setAlready([]), [targetId, eventId]); useEffect(() => { @@ -84,7 +87,7 @@ export function SendButton(props: { api: ApiClient; orgId: string; selection: Ph {target?.kind === "ZEROZERO" && eventId && } {targets.length > 1 && ( - { setTargetId(e.target.value); prefs.set(LAST_TARGET, e.target.value); }} className="h-6 max-w-[140px] border border-black bg-cell px-1 text-[12px] text-ink"> {targets.map((x) => (