Skip to content

Open source CapCut alternative.

TypeScript

feat(site): the Camera Roll fills itself while the page is open

David Lefbff0f3983d3
diff --git a/site/src/cut/components/CameraRollView.tsx b/site/src/cut/components/CameraRollView.tsx
index 0563d05b..722f14a0 100644
--- a/site/src/cut/components/CameraRollView.tsx
+++ b/site/src/cut/components/CameraRollView.tsx
@@ -23,10 +23,15 @@ import { LibraryCard } from "./LibraryView";
/** The phone's recordings, synced up from the iOS app: every cloud library
* asset tagged origin "camera", newest first. Clips here are ordinary library
* assets — the editor's Library panel offers them to any project — this page
- * is where they are watched and pruned. */
+ * is where they are watched and pruned. The listing stays live while the tab
+ * is open, so a recording made on the phone arrives on its own. */
export function CameraRollView() {
const client = useQueryClient();
- const library = useLibrary();
+ // The phone fills this page, so it re-reads on a timer and whenever the
+ // window comes back to the front: a clip finishes uploading and takes its
+ // place in the grid — a shimmering tile first, the clip a moment later —
+ // with nothing to reload.
+ const library = useLibrary({ live: true });
const [deleting, setDeleting] = useState<LibraryAsset | null>(null);
const clips = (library.data?.assets ?? []).filter((a) => a.origin === "camera");
const patch = useCallback(
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsx
index 8ce205da..fa054ca0 100644
--- a/site/src/cut/components/LibraryView.tsx
+++ b/site/src/cut/components/LibraryView.tsx
@@ -1079,6 +1079,10 @@ export function LibraryCard({
// An ffmpeg still washes out iPhone HDR (HLG) footage — the browser tone-maps
// the video correctly, so we render the frame instead of a baked thumbnail.
const posterT = Math.min(1, Math.max(0.1, (a.duration || 2) / 10));
+ // Whether the tile's own picture has painted. Until it does the tile is a
+ // shimmering slab, so a clip that lands while the page is open takes its
+ // place in the grid at once and resolves into itself a moment later.
+ const [painted, setPainted] = useState(false);
// The size pill only shows on hover, so the lookup waits for the first one.
// A font wears its size in the footer at rest, so that one asks as it scrolls
// into view.
@@ -1227,6 +1231,11 @@ export function LibraryCard({
playsInline
preload="metadata"
className="size-full object-cover"
+ // Whichever lands first: a clip with a stored poster paints
+ // from that, one without paints its own first frame.
+ onLoadedMetadata={() => setPainted(true)}
+ onLoadedData={() => setPainted(true)}
+ onError={() => setPainted(true)}
/>
{/* The source's own cover — a video's thumbnail where it came
from — is the face at rest: the element's poster gives way as
@@ -1241,6 +1250,8 @@ export function LibraryCard({
alt=""
aria-hidden
className="pointer-events-none absolute inset-0 size-full object-cover transition-opacity group-hover:opacity-0"
+ onLoad={() => setPainted(true)}
+ onError={() => setPainted(true)}
/>
)}
</>
@@ -1253,6 +1264,8 @@ export function LibraryCard({
alt={a.name}
loading="lazy"
className="size-full object-cover"
+ onLoad={() => setPainted(true)}
+ onError={() => setPainted(true)}
/>
) : (
<AudioCardFace
@@ -1264,6 +1277,13 @@ export function LibraryCard({
}
/>
)}
+ {!offline && !font && a.type !== "audio" && !painted && (
+ <Skeleton
+ data-drag-omit
+ aria-hidden
+ className="pointer-events-none absolute inset-0 rounded-none"
+ />
+ )}
{a.type !== "audio" &&
!font &&
(a.type === "video" || sizeBytes != null) && (
diff --git a/site/src/cut/lib/queries.ts b/site/src/cut/lib/queries.ts
index 64d5e040..cc6905cf 100644
--- a/site/src/cut/lib/queries.ts
+++ b/site/src/cut/lib/queries.ts
@@ -130,9 +130,20 @@ export function useProjectsSection(r: Residency, { list, live }: { list: boolean
});
}
+/** How often a live library listing re-reads. Long enough that an open tab is
+ * cheap, short enough that a clip finishing its upload from the phone lands on
+ * screen while the user is still looking at the page. */
+const LIBRARY_LIVE_MS = 6000;
+
/** The user's whole shelf: every residency's library, merged — including the
- * one this browser can only remember. */
-export function useLibrary() {
+ * one this browser can only remember.
+ *
+ * `live` keeps the listing current while the page is open: the surfaces fed
+ * from another device — the Camera Roll, filled by the iOS app — poll on a
+ * timer and re-read the moment the window comes back to the front, so a
+ * recording made on the phone shows up here without a reload. The timer only
+ * runs while this tab is the focused one. */
+export function useLibrary({ live = false }: { live?: boolean } = {}) {
// Not read for the query itself — the backend binding decides which
// residencies are reachable, so a mode change re-keys and re-reads.
useCutMode();
@@ -154,18 +165,33 @@ export function useLibrary() {
},
staleTime: 0,
refetchOnMount: "always",
+ refetchOnWindowFocus: live,
+ refetchInterval: live ? LIBRARY_LIVE_MS : false,
retry: false,
});
}
+/** Drop a read already on the wire for this key, so its answer — taken before
+ * the mutation the caller is about to apply — never lands on top of the edit.
+ * A live listing is re-reading on a timer, and a delete applied while one of
+ * those reads was in the air used to see the deleted item reappear.
+ *
+ * Only once the key holds something: the first load has nothing to overwrite,
+ * and stopping it would leave the page empty until the next trigger. */
+function stopReads(client: QueryClient, queryKey: readonly unknown[]) {
+ if (client.getQueryData(queryKey) === undefined) return;
+ void client.cancelQueries({ queryKey }, { revert: false });
+}
+
/** Edit a residency's cached listing in place — the optimistic half of every
* projects mutation. The snapshot moves with it, so a reload right after a
- * rename shows the new name rather than flashing the old one back. */
+ * rename shows the new name and never flashes the old one back. */
export function patchProjects(
client: QueryClient,
r: Residency,
fn: (prev: ProjectsSection) => ProjectsSection
) {
+ stopReads(client, projectsKey(r));
const next = client.setQueryData<ProjectsSection>(projectsKey(r), (prev) =>
prev ? fn(prev) : prev
);
@@ -176,6 +202,7 @@ export function patchProjects(
* merged listing is patched however the mutation asks. */
export function patchLibrary(client: QueryClient, fn: (prev: LibraryData) => LibraryData) {
const scope = libraryScope();
+ stopReads(client, libraryKey(scope));
const next = client.setQueryData<LibraryData>(libraryKey(scope), (prev) =>
prev ? fn(prev) : prev
);
@@ -195,19 +222,33 @@ export function refetchLibrary(client: QueryClient) {
// --- Phone surfaces: the iOS link flag and synced notes ---
const PHONE_KEY = ["cut", "phone"] as const;
+// Scoped to the account: two people signing in on one Mac must not read each
+// other's answer.
+const phoneSnapshot = () => snapshotKey("cloud", "phone");
/** Whether this account has been seen from the iOS app. Gates the mobile
* surfaces (Camera Roll, Notes) in the home sidebar; an account that never
- * signed in on a phone keeps the desktop exactly as it was. */
+ * signed in on a phone keeps the desktop exactly as it was.
+ *
+ * Asked once per tab: the flag only ever turns on, and the sidebar unmounts
+ * every time a project opens, so a second read would only cost the two rows a
+ * blink on the way back. The snapshot carries the last answer across reloads,
+ * so a cold load draws the same sidebar it drew last time. */
export function usePhoneLink() {
+ const client = useQueryClient();
+ useEffect(
+ () => seedFromSnapshot<boolean>(client, PHONE_KEY, phoneSnapshot()),
+ [client]
+ );
const query = useQuery<boolean>({
queryKey: PHONE_KEY,
queryFn: async () => {
const res = await backendFor("cloud").fetch("/api/cut/phone");
if (!res.ok) throw new Error(String(res.status));
- return ((await res.json()) as { linked?: boolean }).linked === true;
+ const linked = ((await res.json()) as { linked?: boolean }).linked === true;
+ writeSnapshot(phoneSnapshot(), linked);
+ return linked;
},
- // The flag only ever turns on, and once on it never needs re-asking.
staleTime: Infinity,
retry: false,
});