Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Home: the remembered Recent skeleton has the rows the last visit had

Claudefb6d571ad70c
diff --git a/src/components/home-view.tsx b/src/components/home-view.tsx
index f71989e7..1d540ba6 100644
--- a/src/components/home-view.tsx
+++ b/src/components/home-view.tsx
@@ -145,7 +145,8 @@ export function HomeView({ counts, motions, apps, effects }: { counts: Counts; m
const [prompt, setPrompt] = useState("");
const [engine, setEngine, engineMode] = useEngine();
const recent = useGenerations({ limit: 6 });
- useHadItems("home-recent", recent.data?.length);
+ // How many (up to 6): a phone's grid holds 3 a row, so 1 to 3 results are one row of skeletons, 4 to 6 two.
+ useHadItems("home-recent", recent.data?.length, 6);
// The page hydrates inside the route's Suspense boundary, possibly after the sidebar already fetched
// the status: render the server's (status-less) view until hydrated, as useEngineMode does.
const hydrated = useHydrated();
@@ -251,10 +252,17 @@ export function HomeView({ counts, motions, apps, effects }: { counts: Counts; m
!recent.data && (
<>
<HadItemsScript name="home-recent" />
- <Row title="Recent" href="/library" className="hidden [html[data-had-home-recent='1']_&]:block">
+ <Row title="Recent" href="/library" className="hidden [html[data-had-home-recent]_&]:block">
<div className="grid grid-cols-3 gap-2 sm:grid-cols-6" aria-busy="true" aria-label="Loading recent results">
{Array.from({ length: 6 }, (_, i) => (
- <Skeleton key={i} className="aspect-square rounded-xl" />
+ <Skeleton
+ key={i}
+ className={cn(
+ "aspect-square rounded-xl",
+ // A phone's second row only when the last visit had more than 3.
+ i >= 3 && "max-sm:hidden [html[data-had-home-recent='4']_&]:block [html[data-had-home-recent='5']_&]:block [html[data-had-home-recent='6']_&]:block",
+ )}
+ />
))}
</div>
</Row>
diff --git a/src/components/use-had-items.ts b/src/components/use-had-items.ts
index a7aed60e..e8ec4bdd 100644
--- a/src/components/use-had-items.ts
+++ b/src/components/use-had-items.ts
@@ -3,25 +3,31 @@
import { createElement, useEffect, useLayoutEffect } from "react";
/*
- * Whether a list was non-empty the last time this browser loaded it, so its loading state can take
- * the shape that's coming (a row of skeletons, or the empty state) instead of a guess swapped for
- * something of another height. The answer lives in localStorage and, for CSS, in a `data-had-<name>`
- * attribute on <html>, set before the first paint by <HadItemsScript> (the server's shell renders
- * both shapes; a class like `[html[data-had-home-recent='1']_&]:block` picks one). A browser that has
- * never loaded the list gets the "empty" shape.
+ * How many items a list had the last time this browser loaded it (capped, 0 for none), so its
+ * loading state can take the shape that's coming (that many skeletons, or the empty state) instead
+ * of a guess swapped for something of another height. The answer lives in localStorage and, for CSS,
+ * in a `data-had-<name>="<count>"` attribute on <html> (absent for none), set before the first paint
+ * by <HadItemsScript> (the server's shell renders both shapes; a class like
+ * `[html[data-had-home-recent]_&]:block` or `[html[data-had-cinema-projects='1']_&]:grid` picks one).
+ * A browser that has never loaded the list gets the "empty" shape.
*/
const storageKey = (name: string) => `fuobe:had:${name}`;
const attr = (name: string) => `data-had-${name}`;
-function apply(name: string, had: boolean) {
- if (had) document.documentElement.setAttribute(attr(name), "1");
+/** A stored count as the attribute's value: "1"… for some, null for none (or anything unexpected). */
+export function hadValue(stored: string | null): string | null {
+ return stored && /^[1-9][0-9]?$/.test(stored) ? stored : null;
+}
+
+function apply(name: string, value: string | null) {
+ if (value) document.documentElement.setAttribute(attr(name), value);
else document.documentElement.removeAttribute(attr(name));
}
-/** The pre-paint script's text (exported for tests). */
+/** The pre-paint script's text (exported for tests): hadValue, inlined. */
export function hadItemsScript(name: string): string {
- return `(function(){try{if(localStorage.getItem(${JSON.stringify(storageKey(name))})==="1")document.documentElement.setAttribute(${JSON.stringify(attr(name))},"1")}catch(e){}})()`;
+ return `(function(){try{var v=localStorage.getItem(${JSON.stringify(storageKey(name))});if(v&&/^[1-9][0-9]?$/.test(v))document.documentElement.setAttribute(${JSON.stringify(attr(name))},v)}catch(e){}})()`;
}
/**
@@ -37,18 +43,22 @@ export function HadItemsScript({ name }: { name: string }) {
});
}
-/** Remember whether the list (`count`, undefined while it loads) had anything, and keep <html>'s attribute in step. */
-export function useHadItems(name: string, count: number | undefined) {
+/**
+ * Remember how many the list had (`count`, undefined while it loads), up to `max` (1: just whether it
+ * had any), and keep <html>'s attribute in step.
+ */
+export function useHadItems(name: string, count: number | undefined, max = 1) {
useLayoutEffect(() => {
try {
- apply(name, localStorage.getItem(storageKey(name)) === "1");
+ apply(name, hadValue(localStorage.getItem(storageKey(name))));
} catch {}
}, [name]);
useEffect(() => {
if (count === undefined) return;
+ const value = String(Math.max(0, Math.min(count, max, 99)));
try {
- localStorage.setItem(storageKey(name), count ? "1" : "0");
+ localStorage.setItem(storageKey(name), value);
} catch {}
- apply(name, count > 0);
- }, [name, count]);
+ apply(name, hadValue(value));
+ }, [name, count, max]);
}
diff --git a/tests/ux-flows.test.ts b/tests/ux-flows.test.ts
index 0c3a03f1..5992a96d 100644
--- a/tests/ux-flows.test.ts
+++ b/tests/ux-flows.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import { directorPicks } from "@/components/cinema/draft";
+import { hadItemsScript, hadValue } from "@/components/use-had-items";
import type { Generation, OutputFile } from "@/lib/catalog/types";
import { leftLabel, notePace, timeLeft, untitledName, type Pace } from "@/lib/format";
import { toggleAll } from "@/lib/library";
@@ -152,3 +153,27 @@ describe("video tiles have a poster", () => {
expect(videoPoster(gen([{ kind: "image" }]), { kind: "image" })).toBeUndefined();
});
});
+
+describe("a list's loading shape remembers how many it had", () => {
+ it("reads the stored count back as the <html> attribute's value", () => {
+ expect(hadValue("3")).toBe("3");
+ expect(hadValue("6")).toBe("6");
+ expect(hadValue("0")).toBeNull();
+ expect(hadValue(null)).toBeNull();
+ expect(hadValue("1\" onload=\"x")).toBeNull();
+ });
+
+ it("sets the same value before the first paint", () => {
+ const run = (stored: string | null) => {
+ const attrs: Record<string, string> = {};
+ const localStorage = { getItem: () => stored };
+ const document = { documentElement: { setAttribute: (k: string, v: string) => (attrs[k] = v) } };
+ new Function("localStorage", "document", hadItemsScript("home-recent"))(localStorage, document);
+ return attrs;
+ };
+ expect(run("4")).toEqual({ "data-had-home-recent": "4" });
+ expect(run("0")).toEqual({});
+ expect(run(null)).toEqual({});
+ expect(run("<b>")).toEqual({});
+ });
+});