Open source CapCut alternative.
TypeScript
fix(site): keep the library listing through a failed read
David Lebd7c45097eef
diff --git a/site/src/cut/lib/library.ts b/site/src/cut/lib/library.tsindex 591e0a01..7fd11290 100644--- a/site/src/cut/lib/library.ts+++ b/site/src/cut/lib/library.ts@@ -266,10 +266,25 @@ async function rememberedLibraryFrom(return hit?.value ?? null;}+/** One residency's part of a listing already on screen, standing in for a+ * read of that shelf that failed. */+function heldShelf(r: Residency, held: LibraryData | undefined): LibraryData | null {+ if (!held) {+ return null;+ }+ return {+ assets: held.assets.filter((a) => a.residency === r),+ folders: held.folders.filter((f) => f.residency === r),+ templates: held.templates.filter((t) => t.residency === r),+ };+}+/*** The whole shelf: every residency's library in one listing, newest first. A- * residency that fails is left out rather than failing the read — one- * unreachable half never hides the other; only both failing is an error.+ * residency that fails keeps what `held` — the listing the caller already+ * shows — lists for it, so a read that blinks never empties the page; with+ * nothing held it is recalled when `remembered`, else left out. Only every+ * shelf failing is an error.** `remembered` widens that from what this browser can reach to what the user* has: a shelf whose backend isn't answering is recalled from its last@@ -279,13 +294,17 @@ async function rememberedLibraryFrom(*/export async function fetchLibrary(opts?: {remembered?: boolean;+ held?: LibraryData;}): Promise<LibraryData> {const live = availableResidencies();const rs = opts?.remembered ? listedResidencies() : live;const parts = await Promise.all(rs.map((r) =>live.includes(r)- ? fetchLibraryFrom(r).catch(() => null)+ ? fetchLibraryFrom(r).catch(() =>+ heldShelf(r, opts?.held) ??+ (opts?.remembered ? rememberedLibraryFrom(r) : null),+ ): rememberedLibraryFrom(r),),);diff --git a/site/src/cut/lib/libraryShelfRead.test.ts b/site/src/cut/lib/libraryShelfRead.test.tsnew file mode 100644index 00000000..dcc3dac0--- /dev/null+++ b/site/src/cut/lib/libraryShelfRead.test.ts@@ -0,0 +1,23 @@+import { afterEach, expect, spyOn, test } from "bun:test";+import { cloudBackend } from "@/cut/lib/backend/cloud";+import { fetchLibrary, type LibraryData } from "@/cut/lib/library";++const restores: (() => void)[] = [];+afterEach(() => {+ for (const restore of restores.splice(0)) restore();+});++const STATUS_UNAVAILABLE = 503;++test("a shelf whose read fails keeps the listing already held for it", async () => {+ const read = spyOn(cloudBackend, "fetch").mockResolvedValue(new Response("down", { status: STATUS_UNAVAILABLE }));+ restores.push(() => read.mockRestore());+ const held: LibraryData = {+ assets: [{ id: "a1", fileName: "a1.mp4", name: "Clip", type: "video", duration: 1, addedAt: 0, residency: "cloud" }],+ folders: [{ id: "f1", name: "B-Roll", createdAt: 0, residency: "cloud" }],+ templates: [],+ };+ const data = await fetchLibrary({ remembered: true, held });+ expect(data.assets.map((a) => a.id)).toEqual(["a1"]);+ expect(data.folders.map((f) => f.id)).toEqual(["f1"]);+});diff --git a/site/src/cut/lib/queries.ts b/site/src/cut/lib/queries.tsindex 0a97de56..d265d9bf 100644--- a/site/src/cut/lib/queries.ts+++ b/site/src/cut/lib/queries.ts@@ -16,7 +16,7 @@* user asked for it.*/-import { useQueries, useQuery, useQueryClient, type QueryClient, type UseQueryResult } from "@tanstack/react-query";+import { keepPreviousData, useQueries, useQuery, useQueryClient, type QueryClient, type UseQueryResult } from "@tanstack/react-query";import { useEffect } from "react";import { confirmedEngineUser, currentEngineUser, engineLost, onEngineUserConfirmed } from "./api";@@ -236,7 +236,7 @@ export function useLibrary({ live = false, enabled = true }: { live?: boolean; eenabled,queryKey: libraryKey(scope),queryFn: async () => {- const data = await fetchLibrary({ remembered: true });+ const data = await fetchLibrary({ remembered: true, held: client.getQueryData<LibraryData>(libraryKey(scope)) });writeSnapshot(snap, data);return data;},@@ -245,6 +245,9 @@ export function useLibrary({ live = false, enabled = true }: { live?: boolean; erefetchOnWindowFocus: live,refetchInterval: live ? LIBRARY_LIVE_MS : false,retry: false,+ // The shelf set keys the listing, so the engine connecting or dropping+ // starts a new read; the page keeps the last listing until it answers.+ placeholderData: keepPreviousData,});}