1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
"use client"
import * as React from "react"
import { useState } from "react"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
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 { fetchFormMetadata, FormColumn } from "@/lib/forms/services"
import { FormListsView } from "@/db/schema"
// 옵션을 표시하기 위한 새로운 컴포넌트
const CollapsibleOptions = ({ options }: { options?: string[] }) => {
const [isExpanded, setIsExpanded] = useState(false);
// 옵션이 없거나 5개 이하인 경우 모두 표시
if (!options || options.length <= 5) {
return (
<div className="flex flex-wrap gap-1">
{options?.map((option) => (
<Badge key={option} variant="outline" className="text-xs">
{option}
</Badge>
)) || "-"}
</div>
);
}
// 더 많은 옵션이 있는 경우 접었다 펼칠 수 있게 함
return (
<div>
<div className="flex flex-wrap gap-1 mb-1">
{isExpanded
? options.map((option) => (
<Badge key={option} variant="outline" className="text-xs">
{option}
</Badge>
))
: options.slice(0, 3).map((option) => (
<Badge key={option} variant="outline" className="text-xs">
{option}
</Badge>
))
}
{!isExpanded && (
<Badge variant="outline" className="text-xs bg-muted">
+{options.length - 3} more
</Badge>
)}
</div>
<Button
variant="ghost"
size="sm"
className="h-6 text-xs px-2 py-0"
onClick={() => setIsExpanded(!isExpanded)}
>
{isExpanded ? "Show less" : "Show all"}
</Button>
</div>
);
};
interface ViewMetasProps {
open: boolean
onOpenChange: (open: boolean) => void
form: FormListsView | 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 = React.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<string, FormColumn[]>)
}, [metadata])
// Types for the tabs
const columnTypes = React.useMemo(() => {
return Object.keys(groupedColumns)
}, [groupedColumns])
// Fetch metadata when form changes and dialog is opened
React.useEffect(() => {
async function fetchMeta() {
if (!form || !open) return
if (form.formCode === null || form.projectId === null) 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 (
<Sheet open={open} onOpenChange={onOpenChange}>
<SheetContent className="sm:max-w-xl md:max-w-3xl lg:max-w-4xl xl:max-w-5xl overflow-y-auto">
<SheetHeader className="mb-4">
<SheetTitle>Register Metadata</SheetTitle>
<SheetDescription>
</SheetDescription>
{loading ? (
<div className="text-muted-foreground">Loading metadata...</div>
) : metadata ? (
<div className="flex flex-col gap-1">
<div className="flex gap-2 items-center">
<span className="font-semibold">Register ID:</span>
<Badge variant="outline">{metadata.formCode}</Badge>
</div>
<div className="flex gap-2 items-center">
<span className="font-semibold">Register Description:</span>
<span>{metadata.formName}</span>
</div>
</div>
) : (
<div className="text-sm text-muted-foreground">
No metadata found for form code: {form.formCode}
</div>
)}
</SheetHeader>
{loading ? (
<div className="flex items-center justify-center h-40">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div>
</div>
) : metadata ? (
<Tabs defaultValue="all" className="mt-4">
<TabsList className="mb-4 flex-wrap">
<TabsTrigger value="all">All ({metadata.columns.length})</TabsTrigger>
{columnTypes.map((type) => (
<TabsTrigger key={type} value={type}>
{type} ({groupedColumns[type].length})
</TabsTrigger>
))}
</TabsList>
<TabsContent value="all">
<Card>
<CardHeader>
<CardTitle>All Fields</CardTitle>
<CardDescription>All form fields and their properties</CardDescription>
</CardHeader>
<CardContent>
<Table>
<TableHeader>
<TableRow>
<TableHead>Attribute</TableHead>
<TableHead>Description</TableHead>
<TableHead>Value</TableHead>
<TableHead>Value</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{metadata.columns.map((column) => (
<TableRow key={column.key}>
<TableCell className="font-mono text-sm">{column.key}</TableCell>
<TableCell>{column.label}</TableCell>
<TableCell>
<Badge variant="secondary">{column.type}</Badge>
</TableCell>
<TableCell>
<CollapsibleOptions options={column.options} />
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</CardContent>
</Card>
</TabsContent>
{columnTypes.map((type) => (
<TabsContent key={type} value={type}>
<Card>
<CardHeader>
<CardTitle>{type.charAt(0).toUpperCase() + type.slice(1)} Fields</CardTitle>
<CardDescription>Fields with type "{type}"</CardDescription>
</CardHeader>
<CardContent>
<Table>
<TableHeader>
<TableRow>
<TableHead>Attribute</TableHead>
<TableHead>Description</TableHead>
{type === "LIST" && <TableHead>Value</TableHead>}
</TableRow>
</TableHeader>
<TableBody>
{groupedColumns[type].map((column) => (
<TableRow key={column.key}>
<TableCell className="font-mono text-sm">{column.key}</TableCell>
<TableCell>{column.label}</TableCell>
{type === "LIST" && (
<TableCell>
<CollapsibleOptions options={column.options} />
</TableCell>
)}
</TableRow>
))}
</TableBody>
</Table>
</CardContent>
</Card>
</TabsContent>
))}
</Tabs>
) : (
<div className="text-center py-8">
<div className="text-lg font-medium">No metadata found</div>
<p className="text-muted-foreground mt-2">
Could not find metadata for register ID: {form.formCode}
</p>
</div>
)}
</SheetContent>
</Sheet>
)
}
|