"use client" import * as React from "react" import { useMemo } from "react" import { Badge } from "@/components/ui/badge" import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle } from "@/components/ui/sheet" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import type { TagTypeClassFormMappings } from "@/db/schema/vendorData" // or your actual type import { fetchFormMetadata, FormColumn } from "@/lib/forms/services" interface ViewMetasProps { open: boolean onOpenChange: (open: boolean) => void form: TagTypeClassFormMappings | null } export function ViewMetas({ open, onOpenChange, form }: ViewMetasProps) { // metadata & loading const [metadata, setMetadata] = React.useState<{ formName: string formCode: string columns: FormColumn[] } | null>(null) const [loading, setLoading] = React.useState(false) // Group columns by type for better organization const groupedColumns = useMemo(() => { if (!metadata?.columns) return {} return metadata.columns.reduce((acc, column) => { const type = column.type if (!acc[type]) { acc[type] = [] } acc[type].push(column) return acc }, {} as Record) }, [metadata]) // Types for the tabs const columnTypes = useMemo(() => { return Object.keys(groupedColumns) }, [groupedColumns]) // Fetch metadata when form changes and dialog is opened React.useEffect(() => { async function fetchMeta() { if (!form || !open) return setLoading(true) try { // 서버 액션 호출 const metaData = await fetchFormMetadata(form.formCode, form.projectId) if (metaData) { setMetadata(metaData) } else { setMetadata(null) } } catch (error) { console.error("Error fetching form metadata:", error) setMetadata(null) } finally { setLoading(false) } } fetchMeta() }, [form, open]) if (!form) return null return ( Form Metadata {loading ? (
Loading metadata...
) : metadata ? (
Form Code: {metadata.formCode}
Form Name: {metadata.formName}
) : (
No metadata found for form code: {form.formCode}
)}
{loading ? (
) : metadata ? ( All ({metadata.columns.length}) {columnTypes.map((type) => ( {type} ({groupedColumns[type].length}) ))} All Fields All form fields and their properties Key Label Type Options {metadata.columns.map((column) => ( {column.key} {column.label} {column.type} {column.options ? (
{column.options.map((option) => ( {option} ))}
) : ( "-" )}
))}
{columnTypes.map((type) => ( {type.charAt(0).toUpperCase() + type.slice(1)} Fields Fields with type "{type}" Key Label {type === "select" && Options} {groupedColumns[type].map((column) => ( {column.key} {column.label} {type === "select" && ( {column.options ? (
{column.options.map((option) => ( {option} ))}
) : ( "-" )}
)}
))}
))}
) : (
No metadata found

Could not find metadata for form code: {form.formCode}

)}
) }