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.

Preset header: its preview follows the gallery's rules, and a tap plays it

Adarshe447352417fe
diff --git a/src/components/preset-header.tsx b/src/components/preset-header.tsx
index 477421a9..6b3f2aef 100644
--- a/src/components/preset-header.tsx
+++ b/src/components/preset-header.tsx
@@ -1,9 +1,8 @@
"use client";
import { Cloud, Cpu, Info, X } from "lucide-react";
-import { useSyncExternalStore } from "react";
import { HeaderPill } from "@/components/gpu/readiness-chip";
-import { PresetPreview, useSaveData } from "@/components/preset-preview";
+import { PresetPreview, usePreviewPlayback } from "@/components/preset-preview";
import { openGpuSetup, useGpuBadges } from "@/hooks/use-gpu";
import { useEngine } from "@/lib/api";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
@@ -13,23 +12,11 @@ import { toPresetCard } from "@/lib/preset-card-data";
import { presetHowTo, presetRouteLines, previewFlip } from "@/lib/preset-cards";
import { cn } from "@/lib/utils";
-const REDUCED = "(prefers-reduced-motion: reduce)";
-function useReducedMotion() {
- return useSyncExternalStore(
- (cb) => {
- const mq = window.matchMedia(REDUCED);
- mq.addEventListener("change", cb);
- return () => mq.removeEventListener("change", cb);
- },
- () => window.matchMedia(REDUCED).matches,
- () => true,
- );
-}
-
/**
* The card a studio shows above its composer while a preset (app, camera motion, effect) is active
- * and the feed is still empty: the preview looping, the name, one line on what to do, the models it
- * runs on behind a Details popover, and a way to clear the preset.
+ * and the feed is still empty: the preview looping while it's in view (with reduced motion, Save-Data
+ * or on 2G it waits for a hover or a tap), the name, one line on what to do, the models it runs on
+ * behind a Details popover, and a way to clear the preset.
*
* Hook (studio-page.tsx, empty state, above the composer):
* {preset && !hasItems && <PresetHeader preset={preset} modelName={(id) => getModel(id)?.name}
@@ -49,9 +36,7 @@ export function PresetHeader({
className?: string;
}) {
const card = toPresetCard(preset);
- const reduced = useReducedMotion();
- // Not with Save-Data or on 2G either: a clip cover would download as soon as the studio opens.
- const saveData = useSaveData();
+ const { ref, playing, handlers, onTap } = usePreviewPlayback<HTMLDivElement>(card.id, card.preview, { always: true });
const routes = presetRouteLines(preset, modelName);
// Its state on the viewer's GPU (open engine only): a pill that opens the guided fix.
const badges = useGpuBadges().data;
@@ -59,8 +44,14 @@ export function PresetHeader({
const gpu = preset.open && engine === "open" ? (badges?.presets[preset.id] ?? null) : null;
return (
<div className={cn("mx-auto flex w-full max-w-[760px] items-center gap-3 rounded-2xl border bg-background p-2 pr-3 text-left shadow-soft", className)}>
- <div className="relative aspect-[4/3] w-24 shrink-0 overflow-hidden rounded-xl bg-muted sm:w-32">
- <PresetPreview preview={card.preview} hue={card.hue} playing={!reduced && !saveData} flip={previewFlip(card)} />
+ <div
+ ref={ref}
+ onPointerEnter={handlers.onPointerEnter}
+ onPointerLeave={handlers.onPointerLeave}
+ onPointerUp={onTap}
+ className="relative aspect-[4/3] w-24 shrink-0 overflow-hidden rounded-xl bg-muted sm:w-32"
+ >
+ <PresetPreview preview={card.preview} hue={card.hue} playing={playing} flip={previewFlip(card)} />
</div>
<div className="min-w-0 flex-1">
<p className="truncate font-heading text-[19px] leading-snug">{preset.name}</p>
diff --git a/src/components/preset-preview.tsx b/src/components/preset-preview.tsx
index aba7783e..d62384e8 100644
--- a/src/components/preset-preview.tsx
+++ b/src/components/preset-preview.tsx
@@ -20,15 +20,16 @@ import { glyphFor } from "@/lib/move-glyphs";
import { autoplays, browserPlaybackEnv, createPlaybackStore, playsWhenAsked, previewKind } from "@/lib/preview-playback";
import { cn } from "@/lib/utils";
-/** Which previews play, shared by every card (rules in lib/preview-playback.ts). */
+/** Which previews play, shared by every card and the preset header (rules in lib/preview-playback.ts). */
const store = createPlaybackStore(browserPlaybackEnv);
/**
- * Wire a card element to the playback store. Returns whether its preview should play and the
- * handlers to spread on the card (hover and keyboard focus, after a short rest). A clip plays by
- * itself while the card is in view (see autoplays).
+ * Wire a card element to the playback store. Returns whether its preview should play, the handlers
+ * to spread on the card (hover and keyboard focus, after a short rest) and `onTap`, which plays or
+ * stops it from a touch (for an element a tap doesn't otherwise use). A clip plays by itself while
+ * the card is in view; with `always`, any preview that moves does (the preset header).
*/
-export function usePreviewPlayback<T extends HTMLElement>(id: string, preview: Pick<Preview, "video" | "move" | "fx">) {
+export function usePreviewPlayback<T extends HTMLElement>(id: string, preview: Pick<Preview, "video" | "move" | "fx">, { always = false } = {}) {
const ref = useRef<T | null>(null);
const playing = useSyncExternalStore(
store.subscribe,
@@ -38,9 +39,9 @@ export function usePreviewPlayback<T extends HTMLElement>(id: string, preview: P
const kind = previewKind(preview);
useEffect(() => {
const el = ref.current;
- if (!el || !autoplays(kind, store.canHover())) return;
+ if (!el || !(always ? kind !== "still" : autoplays(kind, store.canHover()))) return;
return store.watch(el, id);
- }, [id, kind]);
+ }, [id, kind, always]);
useEffect(() => () => store.stop(id), [id]);
const on = () => playsWhenAsked(kind, store.reducedMotion()) && store.play(id);
const off = () => store.stop(id);
@@ -54,19 +55,10 @@ export function usePreviewPlayback<T extends HTMLElement>(id: string, preview: P
onFocus: (e: React.FocusEvent) => e.currentTarget.matches(":focus-visible") && on(),
onBlur: off,
},
+ onTap: (e: React.PointerEvent) => e.pointerType !== "mouse" && playsWhenAsked(kind, store.reducedMotion()) && store.toggle(id),
};
}
-const noSubscribe = () => () => {};
-
-/**
- * Save-Data or a 2G connection (lib/preview-playback.ts): a preview that would play without being
- * asked (the preset header's loop) stays on its poster. True before hydration, so nothing starts early.
- */
-export function useSaveData(): boolean {
- return useSyncExternalStore(noSubscribe, store.saveData, () => true);
-}
-
const layerVars = (l: PreviewLayer) => ({ "--d": LAYER_DEPTH[l], "--o": LAYER_OFFSET[l] }) as React.CSSProperties;
/** The other still a transition wipes to. */