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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
'use client'
import { useState, useEffect, useCallback, useMemo } from 'react'
import { Button } from '@/components/ui/button'
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'
import { Input } from '@/components/ui/input'
import { Badge } from '@/components/ui/badge'
import { Search, Check } from 'lucide-react'
import {
ColumnDef,
flexRender,
getCoreRowModel,
getFilteredRowModel,
getPaginationRowModel,
getSortedRowModel,
useReactTable,
SortingState,
ColumnFiltersState,
VisibilityState,
RowSelectionState,
} from '@tanstack/react-table'
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table'
import { searchUnifiedProjects, UnifiedProject, ProjectSearchOptions } from './project-service'
import { toast } from 'sonner'
export interface UnifiedProjectSelectorProps {
selectedProject?: UnifiedProject
onProjectSelect: (project: UnifiedProject) => void
disabled?: boolean
searchOptions?: Partial<ProjectSearchOptions>
placeholder?: string
className?: string
}
export function UnifiedProjectSelector({
selectedProject,
onProjectSelect,
disabled,
searchOptions = {},
placeholder = "프로젝트를 선택하세요",
className
}: UnifiedProjectSelectorProps) {
const [open, setOpen] = useState(false)
const [projects, setProjects] = useState<UnifiedProject[]>([])
const [loading, setLoading] = useState(false)
const [sorting, setSorting] = useState<SortingState>([])
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([])
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({})
const [rowSelection, setRowSelection] = useState<RowSelectionState>({})
const [globalFilter, setGlobalFilter] = useState('')
// 기본 검색 옵션 설정
const finalSearchOptions: ProjectSearchOptions = useMemo(() => ({
searchFrom: 'both',
limit: 100,
...searchOptions
}), [searchOptions])
// 테이블 컬럼 정의
const columns: ColumnDef<UnifiedProject>[] = useMemo(() => [
{
accessorKey: 'code',
header: '프로젝트 코드',
cell: ({ row }) => (
<div className="font-mono text-sm">{row.getValue('code')}</div>
),
},
{
accessorKey: 'name',
header: '프로젝트명',
cell: ({ row }) => (
<div className="max-w-[200px] truncate">{row.getValue('name')}</div>
),
},
{
accessorKey: 'type',
header: '타입',
cell: ({ row }) => (
<Badge variant="outline">{row.getValue('type')}</Badge>
),
},
{
accessorKey: 'source',
header: '출처',
cell: ({ row }) => {
const source = row.getValue('source') as string
return (
<Badge variant={source === 'projects' ? 'default' : 'secondary'}>
{source === 'projects' ? '프로젝트' : '견적프로젝트'}
</Badge>
)
},
},
{
id: 'actions',
header: '선택',
cell: ({ row }) => (
<Button
variant="ghost"
size="sm"
onClick={(e) => {
e.stopPropagation()
handleProjectSelect(row.original)
}}
>
<Check className="h-4 w-4" />
</Button>
),
},
], [])
// 프로젝트 테이블 설정
const table = useReactTable({
data: projects,
columns,
onSortingChange: setSorting,
onColumnFiltersChange: setColumnFilters,
onColumnVisibilityChange: setColumnVisibility,
onRowSelectionChange: setRowSelection,
onGlobalFilterChange: setGlobalFilter,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
getSortedRowModel: getSortedRowModel(),
getFilteredRowModel: getFilteredRowModel(),
state: {
sorting,
columnFilters,
columnVisibility,
rowSelection,
globalFilter,
},
})
// 프로젝트 목록 로드
const loadProjects = useCallback(async (searchTerm?: string) => {
setLoading(true)
try {
const result = await searchUnifiedProjects({
...finalSearchOptions,
searchTerm: searchTerm || globalFilter
})
if (result.success) {
setProjects(result.data)
} else {
toast.error(result.error || '프로젝트를 불러오는데 실패했습니다.')
setProjects([])
}
} catch (error) {
console.error('프로젝트 목록 로드 실패:', error)
toast.error('프로젝트를 불러오는 중 오류가 발생했습니다.')
setProjects([])
} finally {
setLoading(false)
}
}, [finalSearchOptions, globalFilter])
// 프로젝트 선택 핸들러
const handleProjectSelect = useCallback((project: UnifiedProject) => {
onProjectSelect(project)
setOpen(false)
}, [onProjectSelect])
// 다이얼로그 열릴 때 프로젝트 목록 로드
useEffect(() => {
if (open && projects.length === 0) {
loadProjects()
}
}, [open, projects.length, loadProjects])
// 검색어 변경 시 디바운스된 검색
useEffect(() => {
if (!open) return
const timeoutId = setTimeout(() => {
loadProjects(globalFilter)
}, 300)
return () => clearTimeout(timeoutId)
}, [globalFilter, open, loadProjects])
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button
variant="outline"
disabled={disabled}
className={`w-full justify-start ${className || ''}`}
>
{selectedProject ? (
<div className="flex items-center gap-2 w-full">
<span className="font-mono text-sm">[{selectedProject.code}]</span>
<span className="truncate flex-1 text-left">{selectedProject.name}</span>
<Badge variant={selectedProject.source === 'projects' ? 'default' : 'secondary'} className="text-xs">
{selectedProject.source === 'projects' ? 'P' : 'B'}
</Badge>
</div>
) : (
<span className="text-muted-foreground">{placeholder}</span>
)}
</Button>
</DialogTrigger>
<DialogContent className="max-w-4xl max-h-[80vh]">
<DialogHeader>
<DialogTitle>프로젝트 선택</DialogTitle>
<div className="text-sm text-muted-foreground">
{finalSearchOptions.searchFrom === 'both' && '프로젝트 및 견적프로젝트에서 검색'}
{finalSearchOptions.searchFrom === 'projects' && '프로젝트에서 검색'}
{finalSearchOptions.searchFrom === 'biddingProjects' && '견적프로젝트에서 검색'}
</div>
</DialogHeader>
<div className="space-y-4">
<div className="flex items-center space-x-2">
<Search className="h-4 w-4" />
<Input
placeholder="프로젝트 코드, 이름으로 검색..."
value={globalFilter}
onChange={(e) => setGlobalFilter(e.target.value)}
className="flex-1"
/>
</div>
{loading ? (
<div className="flex justify-center py-8">
<div className="text-sm text-muted-foreground">프로젝트를 불러오는 중...</div>
</div>
) : (
<div className="border rounded-md">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
))}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
className="cursor-pointer hover:bg-muted/50"
onClick={() => handleProjectSelect(row.original)}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(
cell.column.columnDef.cell,
cell.getContext()
)}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell
colSpan={columns.length}
className="h-24 text-center"
>
검색 결과가 없습니다.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
)}
<div className="flex items-center justify-between">
<div className="text-sm text-muted-foreground">
총 {table.getFilteredRowModel().rows.length}개 프로젝트
</div>
<div className="flex items-center space-x-2">
<Button
variant="outline"
size="sm"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
이전
</Button>
<div className="text-sm">
{table.getState().pagination.pageIndex + 1} / {table.getPageCount()}
</div>
<Button
variant="outline"
size="sm"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
다음
</Button>
</div>
</div>
</div>
</DialogContent>
</Dialog>
)
}
|