hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { Show, Suspense, createSignal, lazy } from "solid-js"; |
| 2 | import { Button } from "@/components/ui/button"; |
| 3 | import { cn } from "@/lib/cn"; |
| 4 | import { pngBytesToScene } from "@/lib/drawing-file"; |
| 5 | import { playableScene, type DrawScene } from "@/lib/draw-stroke"; |
| 6 | import { useT } from "@/stores/preferences-context"; |
| 7 | |
| 8 | const DrawingPlayback = lazy(() => import("@/components/ui/drawing-playback").then((m) => ({ default: m.DrawingPlayback }))); |
| 9 | |
| 10 | /** |
| 11 | * An uploaded image with optional stroke-by-stroke playback. Defaults to the static |
| 12 | * <img>; "Play drawing" fetches the bytes, recovers the scene embedded in the PNG and |
| 13 | * swaps in the replay. A plain photo (no embedded strokes) falls back to the img and |
| 14 | * drops the button — nothing to replay. |
| 15 | */ |
| 16 | export function ReplayableImage(props: { |
| 17 | fetchBlob: () => Promise<Blob>; |
| 18 | src: string; |
| 19 | alt: string; |
| 20 | /** Sizes the img, the loading box and the playback canvas — the swap can't change size. */ |
| 21 | imgClass: string; |
| 22 | class?: string; |
| 23 | }) { |
| 24 | const t = useT(); |
| 25 | const [scene, setScene] = createSignal<DrawScene | null>(null); |
| 26 | const [mode, setMode] = createSignal<"image" | "loading" | "playback" | "plain">("image"); |
| 27 | |
| 28 | const enterPlayback = async () => { |
| 29 | if (scene()) { |
| 30 | setMode("playback"); |
| 31 | return; |
| 32 | } |
| 33 | setMode("loading"); |
| 34 | try { |
| 35 | const blob = await props.fetchBlob(); |
| 36 | const parsed = pngBytesToScene(new Uint8Array(await blob.arrayBuffer())); |
| 37 | const playable = playableScene(parsed); |
| 38 | if (playable) { |
| 39 | setScene(playable); |
| 40 | setMode("playback"); |
| 41 | } else { |
| 42 | setMode("plain"); // plain image or no strokes → keep the img, hide Play |
| 43 | } |
| 44 | } catch { |
| 45 | setMode("image"); // fetch failed → keep the img, allow a retry |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | return ( |
| 50 | <div class={cn("space-y-2", props.class)}> |
| 51 | {/* local boundary: the lazy playback chunk must not suspend the page around it */} |
| 52 | {/* min-h reserves the box while the chunk loads: imgClass alone can be height:auto → 0px */} |
| 53 | <Suspense fallback={<div class={cn("min-h-64", props.imgClass)} />}> |
| 54 | <Show when={mode() === "playback" && scene()} fallback={<img src={props.src} alt={props.alt} class={props.imgClass} />}> |
| 55 | {(s) => <DrawingPlayback scene={s()} canvasClass={props.imgClass} />} |
| 56 | </Show> |
| 57 | </Suspense> |
| 58 | <Show when={mode() !== "plain"}> |
| 59 | <Button |
| 60 | type="button" |
| 61 | variant="outline" |
| 62 | size="sm" |
| 63 | disabled={mode() === "loading"} |
| 64 | onClick={() => (mode() === "playback" ? setMode("image") : void enterPlayback())} |
| 65 | > |
| 66 | {mode() === "playback" ? t("exams.showImage") : t("exams.playDrawing")} |
| 67 | </Button> |
| 68 | </Show> |
| 69 | </div> |
| 70 | ); |
| 71 | } |