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).
This commit is contained in:
David Gil Pérez committed 2026-04-20 23:19:24 +02:00
1 parent 4439367f4f
commit 09decb7198
1 file changed
+9 -2
@@ -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<string | null>(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 = () => {