The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { useEffect, useRef, useState } from "react"; |
| 2 | import { highlightTS } from "../marketing/highlightTS"; |
| 3 | |
| 4 | interface Panel { |
| 5 | filename: string; |
| 6 | code: string; |
| 7 | caption: string; |
| 8 | } |
| 9 | |
| 10 | const DWELL_MS = 5000; |
| 11 | |
| 12 | export default function AsyncTabs({ panels }: { panels: Panel[] }) { |
| 13 | const [active, setActive] = useState(0); |
| 14 | const wrapRef = useRef<HTMLDivElement>(null); |
| 15 | |
| 16 | useEffect(() => { |
| 17 | let cancelled = false; |
| 18 | let timer: ReturnType<typeof setTimeout> | null = null; |
| 19 | |
| 20 | const schedule = () => { |
| 21 | timer = setTimeout(() => { |
| 22 | if (cancelled) return; |
| 23 | setActive((i) => (i + 1) % panels.length); |
| 24 | }, DWELL_MS); |
| 25 | }; |
| 26 | |
| 27 | const obs = new IntersectionObserver( |
| 28 | (entries) => { |
| 29 | for (const e of entries) { |
| 30 | if (e.isIntersecting) { |
| 31 | schedule(); |
| 32 | } else if (timer) { |
| 33 | clearTimeout(timer); |
| 34 | timer = null; |
| 35 | } |
| 36 | } |
| 37 | }, |
| 38 | { threshold: 0.3 }, |
| 39 | ); |
| 40 | if (wrapRef.current) obs.observe(wrapRef.current); |
| 41 | |
| 42 | return () => { |
| 43 | cancelled = true; |
| 44 | if (timer) clearTimeout(timer); |
| 45 | obs.disconnect(); |
| 46 | }; |
| 47 | }, [active, panels.length]); |
| 48 | |
| 49 | const html = panels.map((p) => highlightTS(p.code)); |
| 50 | |
| 51 | return ( |
| 52 | <div ref={wrapRef} className="async-tabs" data-nosnippet=""> |
| 53 | <div className="async-tabs__chrome"> |
| 54 | <div className="async-tabs__header"> |
| 55 | <span className="alc-code-block__dot" style={{ background: "var(--alc-danger)" }} /> |
| 56 | <span className="alc-code-block__dot" style={{ background: "var(--alc-warn)" }} /> |
| 57 | <span |
| 58 | className="alc-code-block__dot" |
| 59 | style={{ background: "var(--alc-accent-bright)" }} |
| 60 | /> |
| 61 | <div className="async-tabs__tabs" role="tablist"> |
| 62 | {panels.map((p, i) => ( |
| 63 | <button |
| 64 | key={p.filename} |
| 65 | type="button" |
| 66 | role="tab" |
| 67 | aria-selected={active === i} |
| 68 | data-active={active === i} |
| 69 | className="async-tabs__tab" |
| 70 | onClick={() => setActive(i)} |
| 71 | > |
| 72 | {p.filename} |
| 73 | </button> |
| 74 | ))} |
| 75 | </div> |
| 76 | </div> |
| 77 | <div className="async-tabs__body"> |
| 78 | {panels.map((p, i) => ( |
| 79 | <div key={p.filename} className="async-tabs__panel" hidden={active !== i}> |
| 80 | <pre |
| 81 | className="alc-code-block__pre async-tabs__pre" |
| 82 | dangerouslySetInnerHTML={{ __html: html[i] }} |
| 83 | /> |
| 84 | </div> |
| 85 | ))} |
| 86 | </div> |
| 87 | </div> |
| 88 | <div key={active} className="alc-code-block__caption" aria-live="polite"> |
| 89 | {panels[active].caption} |
| 90 | </div> |
| 91 | </div> |
| 92 | ); |
| 93 | } |