({ source }: Props)
| 17 | } |
| 18 | |
| 19 | export function CodeBlock({ source }: Props) { |
| 20 | const [contents, setContents] = useState<Record<string, string>>({}) |
| 21 | const [copiedStates, setCopiedStates] = useState<Record<string, boolean>>({}) |
| 22 | const { copy } = useClipboard() |
| 23 | |
| 24 | const handleCopy = async (key: string, value: string | null) => { |
| 25 | if (value) { |
| 26 | const didCopy = await copy(value) |
| 27 | if (!didCopy) return |
| 28 | setCopiedStates((prev) => ({ ...prev, [key]: true })) |
| 29 | setTimeout(() => { |
| 30 | setCopiedStates((prev) => ({ ...prev, [key]: false })) |
| 31 | }, 2000) |
| 32 | } |
| 33 | } |
| 34 | useEffect(() => { |
| 35 | const fetchContents = async () => { |
| 36 | const loadedContents: Record<string, string> = {} |
| 37 | for (const [name, path] of Object.entries(source)) { |
| 38 | try { |
| 39 | const response = await fetch(`/stubs/${path}.json`) |
| 40 | const data = await response.json() |
| 41 | loadedContents[name] = data.files[0].content |
| 42 | } catch (error) { |
| 43 | console.error(`Error fetching ${path}:`, error) |
| 44 | } |
| 45 | } |
| 46 | setContents(loadedContents) |
| 47 | } |
| 48 | |
| 49 | fetchContents() |
| 50 | }, [source]) |
| 51 | |
| 52 | return ( |
| 53 | <> |
| 54 | {contents && Object.keys(contents).length > 0 ? ( |
| 55 | <Tabs className="relative gap-0"> |
| 56 | <div className="flex items-center justify-between gap-x-2"> |
| 57 | <TabList className="gap-x-4 gap-y-0 border-0"> |
| 58 | {Object.keys(contents).map((key) => ( |
| 59 | <Tab |
| 60 | className={(values) => |
| 61 | twMerge( |
| 62 | 'flex min-w-0 cursor-default items-center gap-x-1 truncate py-2 text-sm/6 *:data-[slot=icon]:hidden sm:*:data-[slot=icon]:block', |
| 63 | values.isSelected || values.isFocused || values.isFocusVisible |
| 64 | ? 'text-fg' |
| 65 | : 'text-muted-fg hover:text-fg' |
| 66 | ) |
| 67 | } |
| 68 | key={key} |
| 69 | id={key} |
| 70 | > |
| 71 | {key.includes('css') ? ( |
| 72 | <BrandCssIcon className="size-4 text-blue-500" /> |
| 73 | ) : key.includes('.tsx') ? ( |
| 74 | <BrandReactjsIcon className="size-4 text-sky-500" /> |
| 75 | ) : key.includes('.ts') ? ( |
| 76 | <BrandTypescriptIcon className="size-4 text-sky-500" /> |
nothing calls this directly
no test coverage detected