/* eslint-disable @typescript-eslint/no-explicit-any */ 'use client'; import { index } from '@/__registry__'; import { ComponentWrapper } from '@/components/docs/component-wrapper'; import { Tabs, TabsContent, TabsList, TabsTrigger, TabsContents, } from '@/registry/radix/tabs'; import { cn } from '@workspace/ui/lib/utils'; import { Loader } from 'lucide-react'; import { Suspense, useEffect, useMemo, useState } from 'react'; import { DynamicCodeBlock } from '@/components/docs/dynamic-codeblock'; import ReactIcon from '@workspace/ui/components/icons/react-icon'; import { type Binds, Tweakpane } from '@workspace/ui/components/docs/tweakpane'; interface ComponentPreviewProps extends React.HTMLAttributes { name: string; iframe?: boolean; bigScreen?: boolean; } function flattenFirstLevel(input: Record): T { return Object.values(input).reduce((acc, current) => { return { ...acc, ...current }; }, {} as T); } function unwrapValues(obj: Record): Record { if (obj !== null && typeof obj === 'object' && !Array.isArray(obj)) { if ('value' in obj) { return obj.value; } const result: Record = {}; for (const key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { result[key] = unwrapValues(obj[key]); } } return result; } return obj; } export function ComponentPreview({ name, className, iframe = false, bigScreen = false, ...props }: ComponentPreviewProps) { const [binds, setBinds] = useState(null); const [componentProps, setComponentProps] = useState | null>(null); const code = useMemo(() => { const code = index[name]?.files?.[0]?.content; if (!code) { console.error(`Component with name "${name}" not found in registry.`); return null; } return code; }, [name]); const preview = useMemo(() => { const Component = index[name]?.component; if (Object.keys(Component?.demoProps ?? {}).length !== 0) { if (componentProps === null) setComponentProps(unwrapValues(Component?.demoProps)); if (binds === null) setBinds(Component?.demoProps); } if (!Component) { console.error(`Component with name "${name}" not found in registry.`); return (

Component{' '} {name} {' '} not found in registry.

); } return ; }, [name, componentProps, binds]); useEffect(() => { if (!binds) return; setComponentProps(unwrapValues(binds)); }, [binds]); return (
Preview Code
} > Loading...
} > {preview}
} />
); }