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, 04:45 UTC
| 1 | import { useGT } from "gt-react"; |
| 2 | import { ChevronRightIcon } from "lucide-react"; |
| 3 | import { |
| 4 | Children, |
| 5 | isValidElement, |
| 6 | use, |
| 7 | useState, |
| 8 | type ComponentProps, |
| 9 | type ReactNode, |
| 10 | } from "react"; |
| 11 | |
| 12 | import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible"; |
| 13 | import { findRevealRef, MarkdownFindContext } from "./markdownFindContext"; |
| 14 | |
| 15 | export function MarkdownDetails({ |
| 16 | children, |
| 17 | open = false, |
| 18 | dir, |
| 19 | }: Pick<ComponentProps<"details">, "children" | "open" | "dir">) { |
| 20 | const gt = useGT(); |
| 21 | const [isOpen, setIsOpen] = useState(open); |
| 22 | const searching = use(MarkdownFindContext); |
| 23 | const expanded = isOpen; |
| 24 | const revealForFind = () => setIsOpen(true); |
| 25 | // Base UI only listens for `beforematch` on a panel mounted at its first |
| 26 | // render; a closed panel mounts later, when find starts, so listen here. |
| 27 | const revealRef = findRevealRef(revealForFind); |
| 28 | const childNodes = Children.toArray(children); |
| 29 | const summaryIndex = childNodes.findIndex( |
| 30 | (child) => isValidElement(child) && child.type === "summary", |
| 31 | ); |
| 32 | const summaryNode = summaryIndex >= 0 ? childNodes[summaryIndex] : null; |
| 33 | const summary = |
| 34 | isValidElement<{ children?: ReactNode }>(summaryNode) && summaryNode.props.children |
| 35 | ? summaryNode.props.children |
| 36 | : gt("Details", { $context: "fallback title of a collapsible section" }); |
| 37 | const content = childNodes.filter((_, index) => index !== summaryIndex); |
| 38 | |
| 39 | return ( |
| 40 | <div className="my-2 border-y border-border/60" dir={dir}> |
| 41 | <Collapsible |
| 42 | open={expanded} |
| 43 | onOpenChange={setIsOpen} |
| 44 | data-markdown-details="" |
| 45 | data-markdown-details-open={expanded ? "true" : "false"} |
| 46 | > |
| 47 | <CollapsibleTrigger |
| 48 | className="flex w-full items-center gap-2 py-2 text-start text-sm font-medium text-foreground [&:dir(rtl):not([data-panel-open])_svg]:rotate-180 data-panel-open:[&_svg]:rotate-90" |
| 49 | data-markdown-details-summary="" |
| 50 | > |
| 51 | <ChevronRightIcon |
| 52 | className="size-4 shrink-0 text-muted-foreground transition-transform" |
| 53 | aria-hidden |
| 54 | /> |
| 55 | <span>{summary}</span> |
| 56 | </CollapsibleTrigger> |
| 57 | <CollapsiblePanel ref={revealRef} hiddenUntilFound={searching}> |
| 58 | <div |
| 59 | className="pb-3 ps-6 text-foreground/[calc(90%+var(--appearance-contrast-boost)/10)]" |
| 60 | data-markdown-details-content="" |
| 61 | > |
| 62 | {content} |
| 63 | </div> |
| 64 | </CollapsiblePanel> |
| 65 | </Collapsible> |
| 66 | </div> |
| 67 | ); |
| 68 | } |