From 09decb719817bdb96b54120039709612086cdae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gil=20P=C3=A9rez?= Date: Mon, 20 Apr 2026 23:19:24 +0200 Subject: [PATCH] fix(potential-trips): pre-fill suggested album name reliably The previous guard 'only set if formData.albumName is empty' lost races against the dialog mount cycle: in some orderings the formData starts empty, the effect fires while config.suggestedAlbumName is undefined, then later when the suggestion arrives the guard refuses to update because prev.albumName == ''. Net effect: the user clicks a trip, selects photos, opens Add to Album, and the Album Name field is empty. Track the last applied suggestion via a ref and write the value whenever the suggestion changes to a new non-empty string. Doesn't overwrite mid-typing because the suggestion only flips when the user picks a different trip; doesn't overwrite when the user picks the same trip twice; works on mount-after-suggestion (which was the broken path). --- src/components/albums/AlbumSelectorDialog.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/components/albums/AlbumSelectorDialog.tsx b/src/components/albums/AlbumSelectorDialog.tsx index 27309d6..749ad06 100644 --- a/src/components/albums/AlbumSelectorDialog.tsx +++ b/src/components/albums/AlbumSelectorDialog.tsx @@ -38,10 +38,17 @@ export default function AlbumSelectorDialog({ onSelected, onCreated, onSubmit, d albumName: defaultName ?? config.suggestedAlbumName ?? "", }); - // Sync the pre-fill if it changes after mount (e.g., user picks another trip) + // Track the previous suggestion so we only overwrite the user's typing when + // the suggestion itself changes (e.g. they picked a different trip), not on + // every render. This keeps "user is typing" stable while still flipping the + // input as soon as a trip selection arrives. + const lastSuggestionRef = React.useRef(null); useEffect(() => { const next = defaultName ?? config.suggestedAlbumName ?? ""; - setFormData((prev) => (prev.albumName ? prev : { albumName: next })); + if (!next) return; + if (lastSuggestionRef.current === next) return; + lastSuggestionRef.current = next; + setFormData({ albumName: next }); }, [defaultName, config.suggestedAlbumName]); const fetchData = () => {