The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 76 lines 3.17 KB
Raw
1 import { useGT } from "gt-react";
2 import { useLayoutEffect, useRef, useState, type ReactNode } from "react";
3
4 import { observeResize } from "~/lib/observeResize";
5
6 const PROJECT_SLOT = "\uE000";
7
8 export function DraftHeroProjectHeading({
9 children,
10 projectName,
11 }: {
12 children: ReactNode;
13 projectName: string;
14 }) {
15 const gt = useGT();
16 // One sentence for translators; the layout places the project picker at the placeholder.
17 // The flex gap stands in for the space before it; the suffix keeps its own leading space.
18 const [before = "", after = ""] = gt("What should we build in {project}?", {
19 project: PROJECT_SLOT,
20 }).split(PROJECT_SLOT);
21 const buildIn = before.trim();
22 const buildInSuffix = after.trimEnd();
23 const containerRef = useRef<HTMLDivElement>(null);
24 const inlineRef = useRef<HTMLSpanElement>(null);
25 const [fitsInline, setFitsInline] = useState(false);
26
27 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- measures the heading against its container via ResizeObserver to choose inline or stacked layout
28 useLayoutEffect(() => {
29 const container = containerRef.current;
30 const inline = inlineRef.current;
31 if (!container || !inline) return;
32
33 // Measure the full inline heading independently of its current layout, so
34 // moving the name below it cannot make the fit check oscillate.
35 const update = () =>
36 setFitsInline(
37 inline.getBoundingClientRect().width <= container.getBoundingClientRect().width,
38 );
39 update();
40 return observeResize([container, inline], update);
41 }, []);
42
43 return (
44 <div ref={containerRef} className="group/draft-hero relative w-full" data-inline={fitsInline}>
45 <span
46 aria-hidden="true"
47 className="pointer-events-none invisible absolute inset-x-0 top-0 h-0 overflow-hidden"
48 >
49 <span
50 ref={inlineRef}
51 className="flex w-max items-baseline gap-2 whitespace-nowrap font-normal text-3xl tracking-tight"
52 >
53 <span>{buildIn}</span>
54 <span>
55 <span className="font-medium">{projectName}</span>
56 {buildInSuffix}
57 </span>
58 </span>
59 </span>
60 <h1 className="flex w-full flex-col items-center justify-center gap-2 text-center font-normal text-2xl text-foreground tracking-tight sm:text-3xl lg:group-data-[inline=true]/draft-hero:flex-row">
61 <span className="max-w-full lg:group-data-[inline=true]/draft-hero:shrink-0 [@media(max-height:28rem)]:sr-only">
62 <span className="lg:group-data-[inline=true]/draft-hero:hidden">
63 {gt("What should we build?")}
64 </span>
65 <span className="hidden lg:group-data-[inline=true]/draft-hero:inline">{buildIn}</span>
66 </span>
67 <span className="flex min-w-0 max-w-full items-baseline justify-center text-base tracking-normal lg:group-data-[inline=true]/draft-hero:text-3xl lg:group-data-[inline=true]/draft-hero:tracking-tight">
68 <span className="min-w-0">{children}</span>
69 <span className="hidden shrink-0 whitespace-pre lg:group-data-[inline=true]/draft-hero:inline [@media(max-height:28rem)]:hidden!">
70 {buildInSuffix}
71 </span>
72 </span>
73 </h1>
74 </div>
75 );
76 }