diff --git a/client/src/components/Journey/JourneyFromPhotosDialog.tsx b/client/src/components/Journey/JourneyFromPhotosDialog.tsx index 26af215b6..698d9b1ac 100644 --- a/client/src/components/Journey/JourneyFromPhotosDialog.tsx +++ b/client/src/components/Journey/JourneyFromPhotosDialog.tsx @@ -31,11 +31,15 @@ export default function JourneyFromPhotosDialog({ const { t } = useTranslation(); const labelId = useId(); const state = useJourneyEntriesFromPhotos({ journeyId, onCreated, onClose }); + const close = () => { + if (!state.busy) onClose(); + }; const allOn = state.clusters.length > 0 && state.selectedCount === state.clusters.length; return ( {state.clusters.length > 1 && ( - state.selectAll(!allOn)}> + state.selectAll(!allOn)}> {allOn ? t('common.deselectAll') : t('common.selectAll')} )} - {t('common.cancel')} + + {t('common.cancel')} + {t('journey.fromPhotos.create', { count: state.selectedCount })} diff --git a/client/src/components/Journey/JourneyFromPhotosList.test.tsx b/client/src/components/Journey/JourneyFromPhotosList.test.tsx index d30060d8c..92583ff87 100644 --- a/client/src/components/Journey/JourneyFromPhotosList.test.tsx +++ b/client/src/components/Journey/JourneyFromPhotosList.test.tsx @@ -59,8 +59,9 @@ describe('choosing gallery photo drafts', () => { expect(screen.getByText('Unavailable')).toBeVisible(); fireEvent.click(screen.getByRole('button', { name: 'common.errorRetry' })); expect(retry).toHaveBeenCalledOnce(); - rerender(); + rerender(); expect(screen.getByText('journey.fromPhotos.empty')).toBeVisible(); + expect(screen.getByText('journey.fromPhotos.withoutTime')).toBeVisible(); }); it('renders a group, can change its selection and title, and explains missing capture data', () => { diff --git a/client/src/components/Journey/JourneyFromPhotosList.tsx b/client/src/components/Journey/JourneyFromPhotosList.tsx index a15dc1541..4c2f6363c 100644 --- a/client/src/components/Journey/JourneyFromPhotosList.tsx +++ b/client/src/components/Journey/JourneyFromPhotosList.tsx @@ -48,7 +48,7 @@ export default function JourneyFromPhotosList({ ); } - if (state.error && state.clusters.length === 0) { + if (state.error) { return (

{state.error}

@@ -63,12 +63,23 @@ export default function JourneyFromPhotosList({ ); } + const skippedPhotos = state.withoutCaptureTime > 0 && ( +

+ {t('journey.fromPhotos.withoutTime', { count: state.withoutCaptureTime })} +

+ ); + if (state.clusters.length === 0) { - return

{t('journey.fromPhotos.empty')}

; + return ( +
+

{t('journey.fromPhotos.empty')}

+ {skippedPhotos} +
+ ); } return ( -
+
{state.clusters.map((cluster) => { const on = state.isSelected(cluster); return ( @@ -82,7 +93,8 @@ export default function JourneyFromPhotosList({ type="button" role="checkbox" aria-checked={on} - aria-label={state.titleOf(cluster)} + aria-label={state.titleOf(cluster).trim() || cluster.suggestedTitle} + disabled={state.busy} onClick={() => state.toggle(cluster)} className={`flex h-6 w-6 flex-none items-center justify-center rounded-full border transition-colors ${ on ? 'border-accent bg-accent text-accent-text' : 'border-edge text-transparent' @@ -94,8 +106,10 @@ export default function JourneyFromPhotosList({
state.rename(cluster, e.target.value)} - disabled={!on} + disabled={!on || state.busy} + maxLength={255} aria-label={t('common.title')} className="-mx-1.5 w-[calc(100%+12px)] truncate rounded-lg bg-transparent px-1.5 py-0.5 text-body font-semibold text-content outline-none hover:bg-surface-secondary focus:bg-surface-secondary disabled:hover:bg-transparent" /> @@ -120,11 +134,7 @@ export default function JourneyFromPhotosList({
); })} - {state.withoutCaptureTime > 0 && ( -

- {t('journey.fromPhotos.withoutTime', { count: state.withoutCaptureTime })} -

- )} + {skippedPhotos}
); } diff --git a/client/src/components/Journey/useJourneyEntriesFromPhotos.test.ts b/client/src/components/Journey/useJourneyEntriesFromPhotos.test.ts index 5a76707ee..d923b359c 100644 --- a/client/src/components/Journey/useJourneyEntriesFromPhotos.test.ts +++ b/client/src/components/Journey/useJourneyEntriesFromPhotos.test.ts @@ -82,8 +82,11 @@ describe('useJourneyEntriesFromPhotos', () => { const { result, onCreated, onClose } = setup(); await waitFor(() => expect(result.current.selectedCount).toBe(2)); - act(() => result.current.rename(VENICE, 'San Marco')); - expect(result.current.titleOf(VENICE)).toBe('San Marco'); + act(() => result.current.rename(VENICE, '')); + expect(result.current.titleOf(VENICE)).toBe(''); + act(() => result.current.rename(VENICE, 'San ')); + expect(result.current.titleOf(VENICE)).toBe('San '); + act(() => result.current.rename(VENICE, 'San Marco ')); await act(async () => result.current.submit()); expect(repo.create).toHaveBeenCalledWith( @@ -157,7 +160,9 @@ describe('useJourneyEntriesFromPhotos', () => { act(() => result.current.selectAll(true)); expect(result.current.selectedCount).toBe(2); + repo.preview.mockRejectedValueOnce(new Error('offline again')); act(() => result.current.setMaxGapMinutes(60)); + expect(result.current.canSubmit).toBe(false); await waitFor(() => expect(repo.preview).toHaveBeenLastCalledWith( 7, @@ -165,5 +170,9 @@ describe('useJourneyEntriesFromPhotos', () => { expect.any(AbortSignal) ) ); + await waitFor(() => expect(result.current.error).toBeTruthy()); + expect(result.current.canSubmit).toBe(false); + await act(async () => result.current.submit()); + expect(repo.create).not.toHaveBeenCalled(); }); }); diff --git a/client/src/components/Journey/useJourneyEntriesFromPhotos.ts b/client/src/components/Journey/useJourneyEntriesFromPhotos.ts index b88aae68c..3fb7ace0c 100644 --- a/client/src/components/Journey/useJourneyEntriesFromPhotos.ts +++ b/client/src/components/Journey/useJourneyEntriesFromPhotos.ts @@ -10,7 +10,6 @@ import { clusterKey, renameCluster, requestOf, - titleOf, toggleCluster, type FromPhotosChoice, } from './journeyEntriesFromPhotosModel'; @@ -99,7 +98,8 @@ export function useJourneyEntriesFromPhotos({ journeyId, onCreated, onClose }: J const [maxGapMinutes, setMaxGapMinutes] = useState(undefined); const [busy, setBusy] = useState(false); const state = usePreview(journeyId, maxGapMinutes, timezone, locale); - const { preview, choice, update, reload } = state; + const { preview, choice, update, reload, loading, failed } = state; + const canUsePreview = !loading && !failed; const clusters = useMemo(() => preview?.clusters ?? [], [preview]); const request = useMemo( () => (choice ? requestOf(clusters, choice, { timezone, lang: locale }) : null), @@ -107,7 +107,7 @@ export function useJourneyEntriesFromPhotos({ journeyId, onCreated, onClose }: J ); const submit = useCallback(async () => { - if (!request || busy) return; + if (!request || busy || !canUsePreview) return; setBusy(true); try { const result = await journeyEntriesFromPhotosRepo.create(journeyId, request); @@ -126,7 +126,7 @@ export function useJourneyEntriesFromPhotos({ journeyId, onCreated, onClose }: J } finally { setBusy(false); } - }, [request, busy, journeyId, toast, t, onCreated, onClose, reload]); + }, [request, busy, canUsePreview, journeyId, toast, t, onCreated, onClose, reload]); return { clusters, @@ -140,14 +140,15 @@ export function useJourneyEntriesFromPhotos({ journeyId, onCreated, onClose }: J error: state.failed ? t('journey.fromPhotos.previewFailed') : null, retry: reload, isSelected: (cluster: JourneyEntriesFromPhotosCluster) => !!choice?.selected.has(clusterKey(cluster)), - titleOf: (cluster: JourneyEntriesFromPhotosCluster) => (choice ? titleOf(choice, cluster) : cluster.suggestedTitle), + titleOf: (cluster: JourneyEntriesFromPhotosCluster) => + choice?.titles[clusterKey(cluster)] ?? cluster.suggestedTitle, selectedCount: request?.groups.length ?? 0, toggle: (cluster: JourneyEntriesFromPhotosCluster) => update((c) => toggleCluster(c, clusterKey(cluster))), selectAll: (on: boolean) => update((c) => chooseAll(c, clusters, on)), rename: (cluster: JourneyEntriesFromPhotosCluster, title: string) => update((c) => renameCluster(c, clusterKey(cluster), title)), busy, - canSubmit: !!request && !busy, + canSubmit: !!request && !busy && canUsePreview, submit: () => void submit(), }; } diff --git a/client/src/mobile/screens/journey/MJourneyFromPhotosSheet.tsx b/client/src/mobile/screens/journey/MJourneyFromPhotosSheet.tsx index deda447aa..d17a7eeb1 100644 --- a/client/src/mobile/screens/journey/MJourneyFromPhotosSheet.tsx +++ b/client/src/mobile/screens/journey/MJourneyFromPhotosSheet.tsx @@ -20,15 +20,18 @@ export default function MJourneyFromPhotosSheet({ }) { const { t } = useTranslation(); const state = useJourneyEntriesFromPhotos({ journeyId, onCreated, onClose }); + const close = () => { + if (!state.busy) onClose(); + }; const allOn = state.clusters.length > 0 && state.selectedCount === state.clusters.length; return ( - +
@@ -39,6 +42,7 @@ export default function MJourneyFromPhotosSheet({