The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /* oxlint-disable react/no-array-index-key -- A page keeps its ordinal throughout incremental layout. */ |
| 2 | import { useGT } from "gt-react"; |
| 3 | import { observeResize } from "~/lib/observeResize"; |
| 4 | import { |
| 5 | useEffect, |
| 6 | useImperativeHandle, |
| 7 | useRef, |
| 8 | useState, |
| 9 | type ReactNode, |
| 10 | type RefObject, |
| 11 | } from "react"; |
| 12 | |
| 13 | export type PageSize = { |
| 14 | readonly width: number; |
| 15 | readonly height: number; |
| 16 | readonly background?: string | undefined; |
| 17 | }; |
| 18 | export type PaintPage = ( |
| 19 | index: number, |
| 20 | canvas: HTMLCanvasElement, |
| 21 | scale: number, |
| 22 | signal: AbortSignal, |
| 23 | ) => Promise<void>; |
| 24 | |
| 25 | export function OfficePages(props: { |
| 26 | pages: readonly PageSize[]; |
| 27 | paint: PaintPage; |
| 28 | kind: "slide" | "page"; |
| 29 | overlay?: (index: number) => ReactNode; |
| 30 | footer?: (index: number) => ReactNode; |
| 31 | scrollRef?: RefObject<((index: number, y?: number) => void) | null>; |
| 32 | onPageChange?: (index: number) => void; |
| 33 | onError: (error: unknown) => void; |
| 34 | }) { |
| 35 | const scroller = useRef<HTMLDivElement>(null); |
| 36 | const rows = useRef(new Map<number, HTMLDivElement>()); |
| 37 | const [width, setWidth] = useState(0); |
| 38 | const [settledWidth, setSettledWidth] = useState(0); |
| 39 | const [mounted, setMounted] = useState(6); |
| 40 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Observe the scroll container width and debounce raster resizing, releasing the observer and timer on unmount. |
| 41 | useEffect(() => { |
| 42 | const root = scroller.current; |
| 43 | if (!root) return; |
| 44 | let timer: ReturnType<typeof setTimeout>; |
| 45 | const unobserve = observeResize(root, ([entry]) => { |
| 46 | if (!entry) return; |
| 47 | const next = entry.contentRect.width; |
| 48 | setWidth(next); |
| 49 | clearTimeout(timer); |
| 50 | timer = setTimeout(() => setSettledWidth(next), 150); |
| 51 | }); |
| 52 | return () => { |
| 53 | clearTimeout(timer); |
| 54 | unobserve(); |
| 55 | }; |
| 56 | }, []); |
| 57 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Yield between page mounting batches with a timer so large documents do not block the browser. |
| 58 | useEffect(() => { |
| 59 | if (mounted >= props.pages.length) return; |
| 60 | const timer = setTimeout(() => setMounted((n) => Math.min(props.pages.length, n + 50)), 0); |
| 61 | return () => clearTimeout(timer); |
| 62 | }, [mounted, props.pages.length]); |
| 63 | useImperativeHandle( |
| 64 | props.scrollRef, |
| 65 | () => |
| 66 | (index: number, y = 0) => { |
| 67 | const row = rows.current.get(index); |
| 68 | const page = props.pages[index]; |
| 69 | if (row && page) |
| 70 | scroller.current?.scrollTo({ |
| 71 | top: row.offsetTop + y * Math.min(1, Math.max(0, width - 48) / page.width) - 24, |
| 72 | }); |
| 73 | }, |
| 74 | [props.pages, width], |
| 75 | ); |
| 76 | return ( |
| 77 | <div |
| 78 | ref={scroller} |
| 79 | className="relative min-h-0 flex-1 overflow-auto bg-muted/60" |
| 80 | data-office-pages |
| 81 | onScroll={() => { |
| 82 | const root = scroller.current; |
| 83 | if (!root || !props.onPageChange) return; |
| 84 | if ( |
| 85 | root.scrollHeight > root.clientHeight && |
| 86 | root.scrollTop + root.clientHeight >= root.scrollHeight - 1 |
| 87 | ) { |
| 88 | props.onPageChange(Math.min(mounted, props.pages.length) - 1); |
| 89 | return; |
| 90 | } |
| 91 | for (const [index, row] of [...rows.current].sort(([a], [b]) => a - b)) { |
| 92 | if (row.offsetTop + row.offsetHeight > root.scrollTop + 24) { |
| 93 | props.onPageChange(index); |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | }} |
| 98 | > |
| 99 | <div className="flex flex-col items-center gap-4 py-6"> |
| 100 | {props.pages.slice(0, mounted).map((page, index) => ( |
| 101 | <OfficePage |
| 102 | key={index} |
| 103 | page={page} |
| 104 | index={index} |
| 105 | root={scroller} |
| 106 | register={(el) => { |
| 107 | if (el) rows.current.set(index, el); |
| 108 | else rows.current.delete(index); |
| 109 | }} |
| 110 | scale={Math.min(1, Math.max(0, width - 48) / page.width)} |
| 111 | rasterScale={Math.min(1, Math.max(0, (settledWidth || width) - 48) / page.width)} |
| 112 | {...props} |
| 113 | /> |
| 114 | ))} |
| 115 | </div> |
| 116 | </div> |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | function OfficePage(props: { |
| 121 | page: PageSize; |
| 122 | index: number; |
| 123 | root: RefObject<HTMLDivElement | null>; |
| 124 | register: (element: HTMLDivElement | null) => void; |
| 125 | scale: number; |
| 126 | rasterScale: number; |
| 127 | paint: PaintPage; |
| 128 | kind: "slide" | "page"; |
| 129 | overlay?: (index: number) => ReactNode; |
| 130 | footer?: (index: number) => ReactNode; |
| 131 | onError: (error: unknown) => void; |
| 132 | }) { |
| 133 | const { root, rasterScale, index, onError, page, paint, scale, register, kind, overlay, footer } = |
| 134 | props; |
| 135 | const gt = useGT(); |
| 136 | const row = useRef<HTMLDivElement>(null); |
| 137 | const canvas = useRef<HTMLCanvasElement>(null); |
| 138 | const [near, setNear] = useState(false); |
| 139 | const [ratio, setRatio] = useState(window.devicePixelRatio || 1); |
| 140 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Observe the page's viewport proximity to limit painting and text layers to nearby pages. |
| 141 | useEffect(() => { |
| 142 | if (!row.current) return; |
| 143 | const observer = new IntersectionObserver( |
| 144 | ([entry]) => setNear(entry?.isIntersecting ?? false), |
| 145 | { root: root.current, rootMargin: "100% 0px" }, |
| 146 | ); |
| 147 | observer.observe(row.current); |
| 148 | return () => observer.disconnect(); |
| 149 | }, [root]); |
| 150 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Subscribe to display density changes so canvas rasters match the current screen. |
| 151 | useEffect(() => { |
| 152 | const query = matchMedia(`(resolution: ${ratio}dppx)`); |
| 153 | const change = () => setRatio(window.devicePixelRatio || 1); |
| 154 | query.addEventListener("change", change); |
| 155 | return () => query.removeEventListener("change", change); |
| 156 | }, [ratio]); |
| 157 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Paint the visible page canvas at the current raster scale and cancel obsolete paint requests. |
| 158 | useEffect(() => { |
| 159 | const target = canvas.current; |
| 160 | if (!target || !page || !near || ratio <= 0 || rasterScale <= 0) return; |
| 161 | const controller = new AbortController(); |
| 162 | void paint(index, target, rasterScale, controller.signal).catch((cause: unknown) => { |
| 163 | if (!controller.signal.aborted) onError(cause); |
| 164 | }); |
| 165 | return () => controller.abort(); |
| 166 | }, [near, index, page, paint, rasterScale, onError, ratio]); |
| 167 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Release the canvas backing store when its page moves away from the viewport. |
| 168 | useEffect(() => { |
| 169 | const target = canvas.current; |
| 170 | if (!near && target) { |
| 171 | target.width = 0; |
| 172 | target.height = 0; |
| 173 | } |
| 174 | }, [near]); |
| 175 | return ( |
| 176 | <div className="flex shrink-0 flex-col gap-3" style={{ width: page.width * scale }}> |
| 177 | <div |
| 178 | ref={(el) => { |
| 179 | row.current = el; |
| 180 | register(el); |
| 181 | }} |
| 182 | role="group" |
| 183 | aria-label={ |
| 184 | kind === "slide" |
| 185 | ? gt("Slide {number}", { number: String(index + 1) }) |
| 186 | : gt("Page {number}", { number: String(index + 1) }) |
| 187 | } |
| 188 | className="relative shrink-0 shadow-md" |
| 189 | style={{ |
| 190 | width: page.width * scale, |
| 191 | height: page.height * scale, |
| 192 | background: page.background ?? "#fff", |
| 193 | }} |
| 194 | > |
| 195 | <canvas ref={canvas} aria-hidden="true" className="absolute inset-0 block size-full" /> |
| 196 | {near ? overlay?.(index) : null} |
| 197 | </div> |
| 198 | {footer?.(index)} |
| 199 | </div> |
| 200 | ); |
| 201 | } |