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, 03:37 UTC
| 1 | import { useGT } from "gt-react"; |
| 2 | import { InfoIcon } from "lucide-react"; |
| 3 | import { useLayoutEffect, useRef, useState, type ReactNode } from "react"; |
| 4 | |
| 5 | import { Button } from "../ui/button"; |
| 6 | import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover"; |
| 7 | import { ComposerBanner, type ComposerBannerVariant } from "./ComposerBanner"; |
| 8 | import { observeResize } from "~/lib/observeResize"; |
| 9 | |
| 10 | /** |
| 11 | * Where a notice sits in the stack. The machine comes first, then what needs you; the |
| 12 | * order is fixed so rows never trade places as notices come and go. |
| 13 | */ |
| 14 | export type ComposerBannerGroup = "environment" | "attention"; |
| 15 | |
| 16 | export interface ComposerBannerStackItem { |
| 17 | readonly id: string; |
| 18 | readonly variant: ComposerBannerVariant; |
| 19 | readonly group?: ComposerBannerGroup; |
| 20 | readonly icon: ReactNode; |
| 21 | readonly title: ReactNode; |
| 22 | readonly description?: ReactNode; |
| 23 | readonly children?: ReactNode; |
| 24 | readonly actions?: ReactNode; |
| 25 | readonly dismissLabel?: string; |
| 26 | readonly onDismiss?: () => void; |
| 27 | } |
| 28 | |
| 29 | const GROUPS = ["environment", "attention"] as const; |
| 30 | |
| 31 | const FADE_IN = |
| 32 | "transition-opacity duration-150 ease-out starting:opacity-0 motion-reduce:transition-none"; |
| 33 | |
| 34 | /** |
| 35 | * Notices about sending from this composer, each its own attached card. Consecutive cards join |
| 36 | * edge to edge, and the last joins the composer or the drawer below it. Detached cards stand |
| 37 | * apart instead, for layouts where the composer must not move when a notice appears. |
| 38 | */ |
| 39 | export function ComposerBannerStack({ |
| 40 | items, |
| 41 | detached = false, |
| 42 | }: { |
| 43 | readonly items: ReadonlyArray<ComposerBannerStackItem>; |
| 44 | readonly detached?: boolean; |
| 45 | }) { |
| 46 | const ordered = GROUPS.flatMap((group) => |
| 47 | items.filter((item) => (item.group ?? "attention") === group), |
| 48 | ); |
| 49 | if (detached) { |
| 50 | return ordered.map((item) => ( |
| 51 | <div key={item.id} data-composer-banner-stack="true" className={FADE_IN}> |
| 52 | <NoticeCard item={item} detached /> |
| 53 | </div> |
| 54 | )); |
| 55 | } |
| 56 | return ordered.map((item) => ( |
| 57 | <ComposerBanner.Attachment key={item.id} data-composer-banner-stack="true" className={FADE_IN}> |
| 58 | <NoticeCard item={item} /> |
| 59 | </ComposerBanner.Attachment> |
| 60 | )); |
| 61 | } |
| 62 | |
| 63 | const SUBPIXEL_ROUNDING = 0.001; |
| 64 | |
| 65 | export function NoticeText({ |
| 66 | children, |
| 67 | className, |
| 68 | detailsLabel, |
| 69 | }: { |
| 70 | readonly children: ReactNode; |
| 71 | readonly className: string; |
| 72 | readonly detailsLabel: string; |
| 73 | }) { |
| 74 | const gt = useGT(); |
| 75 | const textRef = useRef<HTMLSpanElement>(null); |
| 76 | const detailsRef = useRef<HTMLButtonElement>(null); |
| 77 | const [detailsAtWidth, setDetailsAtWidth] = useState<number | null>(null); |
| 78 | const showDetails = detailsAtWidth !== null; |
| 79 | |
| 80 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- measures text overflow in the DOM via ResizeObserver and MutationObserver to show the details button |
| 81 | useLayoutEffect(() => { |
| 82 | const text = textRef.current; |
| 83 | if (!text) return; |
| 84 | const banner = text.closest("[data-slot=composer-banner]"); |
| 85 | const range = document.createRange(); |
| 86 | const measure = () => { |
| 87 | // Ignore the space taken by the details button itself so it cannot |
| 88 | // sustain its own overflow after the text would otherwise fit. |
| 89 | const recoveredWidth = detailsRef.current ? detailsRef.current.offsetWidth + 4 : 0; |
| 90 | const hidden = getComputedStyle(text).position === "absolute"; |
| 91 | const overflowing = |
| 92 | hidden || |
| 93 | [text, ...text.querySelectorAll("*")].some((element) => { |
| 94 | // A range spans laid-out content only; scrollWidth also counts positioned layers |
| 95 | // such as a link's touch hit area, which overflow without cutting any text. |
| 96 | // The two rects can disagree by float rounding (~0.00001px on phones); real |
| 97 | // overflow is at least one layout unit, 1/64 of a device pixel. |
| 98 | range.selectNodeContents(element); |
| 99 | return ( |
| 100 | range.getBoundingClientRect().width > |
| 101 | element.getBoundingClientRect().width + recoveredWidth + SUBPIXEL_ROUNDING |
| 102 | ); |
| 103 | }); |
| 104 | // The button's width can wrap the banner's actions onto a second row, which gives |
| 105 | // the text room and removes the button, which unwraps the row again. Once shown, |
| 106 | // the button stays until the banner itself gets wider than where it appeared. |
| 107 | const width = banner?.clientWidth ?? text.clientWidth; |
| 108 | setDetailsAtWidth((shownAt) => |
| 109 | overflowing |
| 110 | ? (shownAt ?? width) |
| 111 | : banner && shownAt !== null && width <= shownAt |
| 112 | ? shownAt |
| 113 | : null, |
| 114 | ); |
| 115 | }; |
| 116 | measure(); |
| 117 | const stopObserving = observeResize([text, banner ?? text], measure); |
| 118 | let content = text.textContent; |
| 119 | const mutations = new MutationObserver(() => { |
| 120 | if (text.textContent !== content) { |
| 121 | content = text.textContent; |
| 122 | setDetailsAtWidth(null); |
| 123 | } |
| 124 | measure(); |
| 125 | }); |
| 126 | mutations.observe(text, { childList: true, subtree: true, characterData: true }); |
| 127 | return () => { |
| 128 | stopObserving(); |
| 129 | mutations.disconnect(); |
| 130 | }; |
| 131 | }, []); |
| 132 | |
| 133 | return ( |
| 134 | <span className="contents"> |
| 135 | <span ref={textRef} className={className}> |
| 136 | {children} |
| 137 | </span> |
| 138 | {showDetails ? ( |
| 139 | <Popover> |
| 140 | <PopoverTrigger |
| 141 | openOnHover |
| 142 | render={ |
| 143 | <Button |
| 144 | ref={detailsRef} |
| 145 | size="icon-xs" |
| 146 | variant="ghost-muted" |
| 147 | aria-label={detailsLabel} |
| 148 | className="flex-none" |
| 149 | /> |
| 150 | } |
| 151 | > |
| 152 | <InfoIcon /> |
| 153 | </PopoverTrigger> |
| 154 | <PopoverPopup |
| 155 | aria-label={gt("Notice details")} |
| 156 | tooltipStyle |
| 157 | side="top" |
| 158 | className="max-w-[min(30rem,calc(100vw-2rem))] whitespace-normal wrap-anywhere [--inline-button-text-align:start] [--inline-button-white-space:normal] [&_[data-slot=inline-button]]:max-w-full" |
| 159 | > |
| 160 | <ComposerBanner.Scroll>{children}</ComposerBanner.Scroll> |
| 161 | </PopoverPopup> |
| 162 | </Popover> |
| 163 | ) : null} |
| 164 | </span> |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | function NoticeCard({ |
| 169 | item, |
| 170 | detached = false, |
| 171 | }: { |
| 172 | readonly item: ComposerBannerStackItem; |
| 173 | readonly detached?: boolean; |
| 174 | }) { |
| 175 | const gt = useGT(); |
| 176 | return ( |
| 177 | <ComposerBanner.Root |
| 178 | role={item.variant === "error" || item.variant === "warning" ? "alert" : undefined} |
| 179 | variant={item.variant} |
| 180 | detached={detached} |
| 181 | density="comfortable" |
| 182 | data-chat-composer-collapsed-controls="true" |
| 183 | > |
| 184 | <ComposerBanner.Row layout="wrap-actions"> |
| 185 | <ComposerBanner.Icon className="h-(--composer-banner-icon-column) self-start"> |
| 186 | {item.icon} |
| 187 | </ComposerBanner.Icon> |
| 188 | <ComposerBanner.Content className="items-baseline whitespace-nowrap"> |
| 189 | <NoticeText |
| 190 | detailsLabel={ |
| 191 | typeof item.title === "string" |
| 192 | ? gt("Show full title: {title}", { title: item.title }) |
| 193 | : gt("Show title details") |
| 194 | } |
| 195 | className="min-w-0 truncate font-medium leading-7 sm:leading-6" |
| 196 | > |
| 197 | {item.title} |
| 198 | </NoticeText> |
| 199 | {item.description ? ( |
| 200 | <NoticeText |
| 201 | detailsLabel={ |
| 202 | typeof item.title === "string" |
| 203 | ? gt("Show details: {title}", { title: item.title }) |
| 204 | : gt("Show notice details") |
| 205 | } |
| 206 | className="w-0 min-w-0 grow truncate text-muted-foreground @max-[400px]:sr-only" |
| 207 | > |
| 208 | {item.description} |
| 209 | </NoticeText> |
| 210 | ) : null} |
| 211 | </ComposerBanner.Content> |
| 212 | {item.actions || item.onDismiss ? ( |
| 213 | <ComposerBanner.Actions className="min-w-14 sm:min-w-12"> |
| 214 | {item.actions} |
| 215 | {item.onDismiss ? ( |
| 216 | <ComposerBanner.Dismiss |
| 217 | aria-label={item.dismissLabel ?? gt("Dismiss warning")} |
| 218 | onClick={item.onDismiss} |
| 219 | /> |
| 220 | ) : null} |
| 221 | </ComposerBanner.Actions> |
| 222 | ) : null} |
| 223 | </ComposerBanner.Row> |
| 224 | {item.children ? <ComposerBanner.Children>{item.children}</ComposerBanner.Children> : null} |
| 225 | </ComposerBanner.Root> |
| 226 | ); |
| 227 | } |