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.

Prefetch sidebar rows when idle; Library and gallery tabs switch at once

Adarsh13659f8d2d47
diff --git a/src/components/app-sidebar.tsx b/src/components/app-sidebar.tsx
index 2e504f54..292fcf08 100644
--- a/src/components/app-sidebar.tsx
+++ b/src/components/app-sidebar.tsx
@@ -90,7 +90,7 @@ function Items({ group, pathname, search }: { group: NavGroupDef; pathname: stri
group.compact && "h-7 gap-1.5 px-2 text-[12px]",
)}
>
- <IntentLink href={item.href} eager={item.eager} aria-current={active ? "page" : undefined} title={group.compact && item.short ? item.label : undefined}>
+ <IntentLink href={item.href} eager={item.eager} settled aria-current={active ? "page" : undefined} title={group.compact && item.short ? item.label : undefined}>
<item.icon />
<NavLabel>{group.compact ? (item.short ?? item.label) : item.label}</NavLabel>
</IntentLink>
diff --git a/src/components/intent-link.tsx b/src/components/intent-link.tsx
index b1646b02..a40ef143 100644
--- a/src/components/intent-link.tsx
+++ b/src/components/intent-link.tsx
@@ -3,36 +3,47 @@
import { Loader2 } from "lucide-react";
import Link, { useLinkStatus } from "next/link";
import { useRouter } from "next/navigation";
-import { useState } from "react";
+import { useEffect, useState } from "react";
import { cn } from "@/lib/utils";
+import { useSettled } from "@/lib/when-idle";
/**
* A Link that prefetches on intent: nothing while it merely sits on screen, then the usual prefetch
* once the pointer is over it, a finger touches it or it gets keyboard focus (the "hover-triggered
* prefetch" of Next's prefetching guide). `eager` rows prefetch as soon as they're on screen, like a
- * plain Link: the few routes most visits go to next. A page full of these no longer fetches every
- * route behind it on a cold visit.
+ * plain Link: the few routes most visits go to next. `settled` rows prefetch once the page has
+ * loaded and the browser is idle, also the ones scrolled out of view (the app's own navigation: a tap
+ * there must not wait a round trip, a touch comes too late to start one, and rows that only start
+ * when scrolled into view would queue up in front of the tap). A page full of these no longer
+ * fetches every route behind it on a cold visit.
*/
export function IntentLink({
eager,
+ settled,
onMouseEnter,
onTouchStart,
onFocus,
...props
-}: React.ComponentProps<typeof Link> & { eager?: boolean }) {
+}: React.ComponentProps<typeof Link> & { eager?: boolean; settled?: boolean }) {
const [intent, setIntent] = useState(false);
const router = useRouter();
+ const idle = useSettled();
+ const now = eager || (settled && idle);
+ const href = typeof props.href === "string" ? props.href : null;
+ useEffect(() => {
+ if (settled && idle && href) router.prefetch(href);
+ }, [settled, idle, href, router]);
const start = () => {
- if (eager || intent) return;
+ if (now || intent) return;
setIntent(true);
// Start the fetch from the event itself: the Link only picks it up after it renders again and
// sees itself on screen, a frame or more later (several on a slow phone), which a quick click beats.
- if (typeof props.href === "string") router.prefetch(props.href);
+ if (href) router.prefetch(href);
};
return (
<Link
{...props}
- prefetch={eager || intent ? null : false}
+ prefetch={now || intent ? null : false}
onMouseEnter={(e) => {
start();
onMouseEnter?.(e);
diff --git a/src/components/library-view.tsx b/src/components/library-view.tsx
index 4d3c2160..acc924e8 100644
--- a/src/components/library-view.tsx
+++ b/src/components/library-view.tsx
@@ -2,7 +2,7 @@
import { ArchiveRestore, BookmarkPlus, CheckCheck, CheckSquare, ChevronUp, CircleDot, Download, FolderInput, Globe, Heart, Loader2, MoreHorizontal, Pin, PinOff, Search, Trash2, X } from "lucide-react";
import Link from "next/link";
-import { usePathname, useRouter, useSearchParams } from "next/navigation";
+import { usePathname, useSearchParams } from "next/navigation";
import { Suspense, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import { toast } from "sonner";
import { AssetCard, cardRatio, type CardApi } from "@/components/library/asset-card";
@@ -224,8 +224,12 @@ function Cards({ entries, prefs, expanded, render, panel }: { entries: LibraryEn
}
function Inner() {
- const sp = useSearchParams();
- const router = useRouter();
+ const routed = useSearchParams();
+ // The params just set by go(): the History API hands them to useSearchParams in a transition, a frame later; shown at
+ // once until the router has them.
+ const [set, setSet] = useState<{ from: string; to: URLSearchParams } | null>(null);
+ if (set && set.from !== routed.toString()) setSet(null);
+ const sp = set && set.from === routed.toString() ? set.to : routed;
const pathname = usePathname();
const tab = TABS.find((t) => t.id === sp.get("tab")) ?? TABS[0];
const folderId = sp.get("folder") ?? undefined;
@@ -411,7 +415,10 @@ function Inner() {
else next.delete(k);
}
const s = next.toString();
- router.replace(s ? `${pathname}?${s}` : pathname, { scroll: false });
+ setSet({ from: routed.toString(), to: next });
+ // The page reads no search params on the server: the native History API updates the address with no request (a
+ // router.replace waited a round trip before the tab moved), and useSearchParams follows.
+ window.history.replaceState(null, "", s ? `${pathname}?${s}` : pathname);
window.scrollTo({ top: 0 });
};
diff --git a/src/components/preset-gallery.tsx b/src/components/preset-gallery.tsx
index cc05e685..e9ec4297 100644
--- a/src/components/preset-gallery.tsx
+++ b/src/components/preset-gallery.tsx
@@ -267,23 +267,25 @@ export function PresetGallery({
const locked = mode === "both" ? undefined : mode;
const runsOn: RunsOn = locked ?? runsOnChoice;
const query = useDeferredValue(q.trim().toLowerCase());
+ // The tapped category is marked in the next frame; the grid follows in a render that doesn't hold that frame up.
+ const listCat = useDeferredValue(cat);
const index = useMemo(() => presets.map((p) => [p, searchText(p)] as const), [presets]);
const cats = useMemo(() => ["All", ...categories], [categories]);
const list = useMemo(
() =>
index
- .filter(([p, text]) => (cat === "All" || p.category === cat) && (!query || matchesSearch(text, query)) && (runsOn === "all" || p[runsOn]))
+ .filter(([p, text]) => (listCat === "All" || p.category === listCat) && (!query || matchesSearch(text, query)) && (runsOn === "all" || p[runsOn]))
.map(([p]) => p),
- [index, cat, query, runsOn],
+ [index, listCat, query, runsOn],
);
// Cards render a page at a time; a new filter starts from the first page again.
- const { shown, hasMore, moreFrom, sentinel, left } = useProgressive(list.length, `${cat}|${query}|${runsOn}`, FIRST_PAGE, PAGE);
+ const { shown, hasMore, moreFrom, sentinel, left } = useProgressive(list.length, `${listCat}|${query}|${runsOn}`, FIRST_PAGE, PAGE);
const grid = useRef<HTMLDivElement>(null);
const strip = useFadeEdges<HTMLDivElement>();
- const stale = q.trim().toLowerCase() !== query;
- const lead = (leading ?? []).filter((c) => cat === "All" && runsOn !== "cloud" && (!query || matchesSearch(`${c.title} ${c.line} ${c.words ?? ""}`.toLowerCase(), query)));
+ const stale = q.trim().toLowerCase() !== query || cat !== listCat;
+ const lead = (leading ?? []).filter((c) => listCat === "All" && runsOn !== "cloud" && (!query || matchesSearch(`${c.title} ${c.line} ${c.words ?? ""}`.toLowerCase(), query)));
// One fetch of the viewer's chips (the GPU pre-flight); each card gets one string.
const badges = useGpuBadges().data;
const viewer = useViewer().data;