fix(unsplash): tell a server Unsplash turns away that the free key helps

Without an Access Key the image search goes through Unsplash's web endpoint,
which refuses datacenter addresses with a 401 or 403 or answers with a
challenge page. The search then said only "Unsplash search unavailable",
although an admin can already enter a free key in the settings. Such a
refusal now answers 502 with that hint and the code unsplash_key_needed, and
the cover search shows it in the user's language.

Closes #1449
This commit is contained in:
Maurice
2026-10-28 14:54:45 +01:00
parent aee275e2b0
commit 091505d17c
31 changed files with 117 additions and 4 deletions
@@ -135,6 +135,16 @@ describe('useTripCoverPicker', () => {
expect(result.current.searchError).toBe('offline');
});
it('FE-COMP-TRIPCOVER-010b: a server Unsplash turned away says the admin can add the key (#1449)', async () => {
vi.spyOn(tripsApi, 'searchCoverImages').mockRejectedValueOnce({
response: { status: 502, data: { error: 'Unsplash refused this server.', code: 'unsplash_key_needed' } },
});
const { result } = picker();
act(() => result.current.setSearchQuery('Lisbon'));
await act(() => result.current.search());
expect(result.current.searchError).toBe('dashboard.unsplashKeyNeeded');
});
it('FE-COMP-TRIPCOVER-011: only the latest search applies its results', async () => {
let resolveFirst!: (v: { photos: (typeof photo)[] }) => void;
vi.spyOn(tripsApi, 'searchCoverImages')
@@ -4,6 +4,7 @@ import { tripsApi } from '../../api/client';
import { useTranslation } from '../../i18n';
import { isEffectivelyOffline } from '../../sync/networkMode';
import { getApiErrorMessage } from '../../types';
import { getApiErrorCode } from '../../utils/apiError';
import { normalizeImageFile } from '../../utils/convertHeic';
import { refusedOffline } from '../../utils/needsConnection';
import { useToast } from '../shared/Toast';
@@ -149,7 +150,12 @@ export function useTripCoverPicker({ trip, title, onCoverUpdate, releasePreviewO
if (photos.length === 0) setSearchError(t('dashboard.unsplashNoResults'));
} catch (err: unknown) {
if (seq !== coverSearchSeq.current) return;
setSearchError(getApiErrorMessage(err, t('dashboard.coverSearchError')));
// #1449: Unsplash turns a datacenter address away until an admin enters the free key.
setSearchError(
getApiErrorCode(err) === 'unsplash_key_needed'
? t('dashboard.unsplashKeyNeeded')
: getApiErrorMessage(err, t('dashboard.coverSearchError'))
);
} finally {
if (seq === coverSearchSeq.current) setSearching(false);
}