Skip to content

Open source CapCut alternative.

TypeScript

fix(site): wait for every shelf before the empty home

David Leb6c96b5917f4
diff --git a/site/src/cut/components/ProjectsHome.tsx b/site/src/cut/components/ProjectsHome.tsx
index bf0f2b3f..914ccc7a 100644
--- a/site/src/cut/components/ProjectsHome.tsx
+++ b/site/src/cut/components/ProjectsHome.tsx
@@ -86,6 +86,7 @@ import { cn } from "@/lib/utils";
import { setObjectDragImage } from "@/cut/lib/assetDrag";
import { childrenOf, folderTrail, folderWithin, parentOf, subtreeOf } from "@/cut/lib/folderTree";
import { PICKED_RING } from "@/cut/lib/assetPick";
+import { projectsHomeView } from "@/cut/lib/projectsHomeView";
import {
FolderCrumb,
FolderMenuItems,
@@ -134,6 +135,7 @@ type SectionData = {
projects: ProjectSummary[] | null;
folders: ProjectFolder[];
error: boolean;
+ loading: boolean;
};
/** What a delete is about to take: folders and projects, picked together. */
@@ -284,6 +286,7 @@ export function ProjectsHome() {
// A cached listing on screen outranks a failed revalidation: the cards
// stay, and only a section with nothing to show says it couldn't load.
error: q.isError && q.data === undefined,
+ loading: q.isLoading,
});
const data: Record<Residency, SectionData> = {
local: asSection(localSection),
@@ -676,6 +679,7 @@ export function ProjectsHome() {
const hasContent = residencies.some(
(r) => (data[r].projects?.length ?? 0) > 0 || data[r].folders.length > 0
);
+ const homeView = projectsHomeView(residencies.map((r) => data[r]));
// One flat surface: every residency's folders on one shelf and projects in
// one grid, newest edits first — the per-card badge says where each lives.
@@ -1101,15 +1105,15 @@ export function ProjectsHome() {
{dupError && <p className="mb-4 text-sm text-destructive">{dupError}</p>}
- {!anySettled ? (
+ {homeView === "loading" ? (
<div className="grid place-items-center py-24 text-muted-foreground">
<Loader2 className="size-5 animate-spin" />
</div>
- ) : !hasContent && residencies.every((r) => data[r].error) ? (
+ ) : homeView === "failed" ? (
<p className="py-24 text-center text-sm text-muted-foreground">
Couldn&rsquo;t load these projects.
</p>
- ) : !hasContent ? (
+ ) : homeView === "empty" ? (
<div className="grid min-h-[60vh] place-items-center">
<div className="flex flex-col items-center gap-4 text-center">
<div className="grid size-14 place-items-center rounded-2xl bg-muted">
diff --git a/site/src/cut/lib/projectsHomeView.test.ts b/site/src/cut/lib/projectsHomeView.test.ts
new file mode 100644
index 00000000..eba95025
--- /dev/null
+++ b/site/src/cut/lib/projectsHomeView.test.ts
@@ -0,0 +1,29 @@
+import { expect, test } from "bun:test";
+
+import { projectsHomeView } from "./projectsHomeView";
+
+const empty = { projects: [], folders: [], error: false, loading: false };
+const loading = { projects: null, folders: [], error: false, loading: true };
+const idle = { projects: null, folders: [], error: false, loading: false };
+const failed = { projects: null, folders: [], error: true, loading: false };
+const filled = { projects: [{ id: "p" }], folders: [], error: false, loading: false };
+
+test("an empty shelf waits for the shelves still loading their first answer", () => {
+ expect(projectsHomeView([empty, loading])).toBe("loading");
+});
+
+test("the empty state shows once every read shelf answered empty", () => {
+ expect(projectsHomeView([empty, empty])).toBe("empty");
+ expect(projectsHomeView([empty, idle])).toBe("empty");
+ expect(projectsHomeView([empty, failed])).toBe("empty");
+});
+
+test("cards show as soon as any shelf has them", () => {
+ expect(projectsHomeView([filled, loading])).toBe("content");
+ expect(projectsHomeView([{ ...empty, folders: [{ id: "f" }] }, loading])).toBe("content");
+});
+
+test("nothing answered yet is loading, every shelf failing is failed", () => {
+ expect(projectsHomeView([loading, idle])).toBe("loading");
+ expect(projectsHomeView([failed, failed])).toBe("failed");
+});
diff --git a/site/src/cut/lib/projectsHomeView.ts b/site/src/cut/lib/projectsHomeView.ts
new file mode 100644
index 00000000..deeff13c
--- /dev/null
+++ b/site/src/cut/lib/projectsHomeView.ts
@@ -0,0 +1,30 @@
+/** One shelf's listing as the projects home reads it. */
+type ShelfListing = {
+ // Null until the shelf has answered once (or a snapshot seeded it).
+ projects: readonly unknown[] | null;
+ folders: readonly unknown[];
+ error: boolean;
+ // Fetching its first answer, with nothing cached to show.
+ loading: boolean;
+};
+
+type HomeView = "loading" | "failed" | "empty" | "content";
+
+/**
+ * What the projects home draws. Cards show as soon as any shelf has them. The
+ * empty state waits until no shelf is still loading its first answer: a
+ * browser shelf that answers empty first must not flash "Create a new
+ * project" over cloud projects still on their way. A shelf nothing is reading
+ * (the Mac's, with the app closed) holds nothing up.
+ */
+export function projectsHomeView(shelves: readonly ShelfListing[]): HomeView {
+ if (shelves.some((s) => (s.projects?.length ?? 0) > 0 || s.folders.length > 0)) {
+ return "content";
+ }
+
+ const anySettled = shelves.some((s) => s.projects !== null || s.error);
+ if (!anySettled || shelves.some((s) => s.loading)) {
+ return "loading";
+ }
+ return shelves.every((s) => s.error) ? "failed" : "empty";
+}