diff --git a/apps/app/src/library/store.test.ts b/apps/app/src/library/store.test.ts index b1340de..fe0b0b9 100644 --- a/apps/app/src/library/store.test.ts +++ b/apps/app/src/library/store.test.ts @@ -114,4 +114,25 @@ describe("library store", () => { expect(s.photos.map((p) => p.id)).toEqual(["a", "live"]); expect(s.loading).toBe(false); }); + + it("replaces expired file urls from the cache and keeps a mark made while the list was loading", () => { + const stale = { thumb: "/v1/files/photos/a/thumb?exp=1&v=2", preview: "/old-preview?v=2", source: "/old-source" }; + const fresh = { thumb: "/v1/files/photos/a/thumb?exp=9&v=2", preview: "/new-preview?v=2", source: "/new-source" }; + let s = libraryReducer(initialLibrary, { type: "load", scope: "e1" }); + s = libraryReducer(s, { type: "loaded", scope: "e1", photos: [photo("a", "1", { urls: stale })], done: false }); + s = libraryReducer(s, { type: "patch", ids: ["a"], patch: { flag: "PICK" } }); + s = libraryReducer(s, { type: "loaded", scope: "e1", photos: [photo("a", "1", { urls: fresh, flag: "NONE" })], done: true, drop: ["a"] }); + expect(s.photos[0]).toMatchObject({ flag: "PICK", urls: fresh }); + }); + + it("does not roll a thumbnail back to an older render that the list still has", () => { + const shown = { thumb: "/thumb?v=4", preview: "/preview?v=4", source: "/source" }; + const older = { thumb: "/thumb?v=3", preview: "/preview?v=3", source: "/source" }; + let s = libraryReducer(initialLibrary, { type: "load", scope: "e1" }); + s = libraryReducer(s, { type: "loaded", scope: "e1", photos: [photo("a", "1", { urls: older })], done: false }); + s = libraryReducer(s, { type: "message", message: { type: "photo.rendered", photoId: "a", renderedSeq: 4, urls: { thumb: shown.thumb, preview: shown.preview } } }); + s = libraryReducer(s, { type: "loaded", scope: "e1", photos: [photo("a", "1", { urls: older })], done: true, drop: ["a"] }); + expect(s.photos[0]!.urls.thumb).toBe(shown.thumb); + expect(s.photos[0]!.urls.preview).toBe(shown.preview); + }); }); diff --git a/apps/app/src/library/store.ts b/apps/app/src/library/store.ts index c6e6644..0c40b3c 100644 --- a/apps/app/src/library/store.ts +++ b/apps/app/src/library/store.ts @@ -44,6 +44,29 @@ function inScope(scope: string | null, photo: PhotoSummary): boolean { return scope === "inbox" ? photo.eventId === null : photo.eventId === scope; } +/** Edit version in a signed file URL (`v`). A render that arrived while the list was loading must not be rolled back. */ +function renderedVersion(url: string): number { + const match = /[?&]v=(\d+)/.exec(url); + if (!match) return 0; + return Number(match[1]); +} + +/** + * The folder is drawn from the last visit before the network answers. Marks made in that moment stay, + * but the signed file URLs from that visit expire, so the server's signatures take their place. + */ +function withFreshFileUrls(current: PhotoSummary, incoming: PhotoSummary | undefined): PhotoSummary { + if (!incoming) return current; + let next = current; + if (current.status !== "READY" && incoming.status !== current.status) { + next = { ...next, status: incoming.status }; + if (incoming.error) next = { ...next, error: incoming.error }; + } + if (renderedVersion(incoming.urls.thumb) < renderedVersion(current.urls.thumb)) return next; + if (current.urls.thumb === incoming.urls.thumb && current.urls.preview === incoming.urls.preview && current.urls.source === incoming.urls.source) return next; + return { ...next, urls: incoming.urls }; +} + /** The server's copy replaces a local photo: what was done to it here stays (it is being sent too). */ function replaceLocal(state: LibraryState, local: PhotoSummary, server: PhotoSummary): LibraryState { const keep: Partial = { @@ -67,10 +90,11 @@ export function libraryReducer(state: LibraryState, action: LibraryAction): Libr // A cached folder is shown first. Once the server has answered, photos that were only in that cache // (they have since moved to another event) leave. Anything that arrived meanwhile stays. const drop = new Set(action.drop ?? []); - const incoming = new Set(action.photos.map((p) => p.id)); + const incoming = new Map(action.photos.map((p) => [p.id, p])); const prior = drop.size ? state.photos.filter((p) => !drop.has(p.id) || incoming.has(p.id)) : state.photos; - const seen = new Set(prior.map((p) => p.id)); - const photos = [...prior, ...action.photos.filter((p) => !seen.has(p.id))]; + const refreshed = prior.map((p) => withFreshFileUrls(p, incoming.get(p.id))); + const seen = new Set(refreshed.map((p) => p.id)); + const photos = [...refreshed, ...action.photos.filter((p) => !seen.has(p.id))]; // The active photo from another module (or the last visit): selected as soon as its page arrives. if (state.wanted && photos.some((p) => p.id === state.wanted)) { const id = state.wanted;