summaryrefslogtreecommitdiff
path: root/components/project/ProjectList.tsx
blob: 4a4f7962f8b6b1f2ce312fe367d4740fc081a259 (plain)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// components/project/ProjectList.tsx
'use client';

import { useState, useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useForm } from 'react-hook-form';
import { 
  Plus, 
  Folder, 
  Users, 
  Globe, 
  Lock,
  Crown,
  Calendar,
  Search,
  Filter,
  Grid3x3,
  List
} from 'lucide-react';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Input } from '@/components/ui/input';
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from '@/components/ui/dialog';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { useToast } from '@/hooks/use-toast';
import { cn } from '@/lib/utils';

interface Project {
  id: string;
  code: string;
  name: string;
  description?: string;
  isPublic: boolean;
  createdAt: string;
  updatedAt: string;
  role?: string;
  memberCount?: number;
  fileCount?: number;
}

interface ProjectFormData {
  code: string;
  name: string;
  description?: string;
  isPublic: boolean;
}

export function ProjectList() {
  const [projects, setProjects] = useState<{
    owned: Project[];
    member: Project[];
    public: Project[];
  }>({ owned: [], member: [], public: [] });
  const [searchQuery, setSearchQuery] = useState('');
  const [viewMode, setViewMode] = useState<'grid' | 'list'>('grid');
  const [createDialogOpen, setCreateDialogOpen] = useState(false);
  const [isSubmitting, setIsSubmitting] = useState(false);
  
  const router = useRouter();
  const { toast } = useToast();

  // React Hook Form 설정
  const {
    register,
    handleSubmit,
    reset,
    formState: { errors, isValid },
    watch,
    setValue,
  } = useForm<ProjectFormData>({
    mode: 'onChange',
    defaultValues: {
      code: '',
      name: '',
      description: '',
      isPublic: false,
    },
  });

  const watchIsPublic = watch('isPublic');

  useEffect(() => {
    fetchProjects();
  }, []);

  const fetchProjects = async () => {
    try {
      const response = await fetch('/api/projects');
      const data = await response.json();
      setProjects(data);
    } catch (error) {
      toast({
        title: '오류',
        description: '프로젝트 목록을 불러올 수 없습니다.',
        variant: 'destructive',
      });
    }
  };

  const onSubmit = async (data: ProjectFormData) => {
    setIsSubmitting(true);
    try {
      const response = await fetch('/api/projects', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(data),
      });

      if (!response.ok) throw new Error('프로젝트 생성 실패');

      const project = await response.json();
      
      toast({
        title: '성공',
        description: '프로젝트가 생성되었습니다.',
      });

      setCreateDialogOpen(false);
      reset();
      fetchProjects();
      
      // 생성된 프로젝트로 이동
      router.push(`/evcp/data-room/${project.id}`);
    } catch (error) {
      toast({
        title: '오류',
        description: '프로젝트 생성에 실패했습니다.',
        variant: 'destructive',
      });
    } finally {
      setIsSubmitting(false);
    }
  };

  const handleDialogClose = (open: boolean) => {
    setCreateDialogOpen(open);
    if (!open) {
      reset();
    }
  };

  const filteredProjects = {
    owned: projects.owned?.filter(p => 
      p.name.toLowerCase().includes(searchQuery.toLowerCase())
    ),
    member: projects.member?.filter(p => 
      p.name.toLowerCase().includes(searchQuery.toLowerCase())
    ),
    public: projects.public?.filter(p => 
      p.name.toLowerCase().includes(searchQuery.toLowerCase())
    ),
  };

  const ProjectCard = ({ project, role }: { project: Project; role?: string }) => (
    <Card 
      className="cursor-pointer hover:shadow-lg transition-shadow"
      onClick={() => router.push(`/evcp/data-room/${project.id}/files`)}
    >
      <CardHeader>
        <div className="flex items-start justify-between">
          <div className="flex items-center gap-2">
            <Folder className="h-5 w-5 text-blue-500" />
            <CardTitle className="text-base">{project.code} {project.name}</CardTitle>
          </div>
          {role === 'owner' && (
            <Crown className="h-4 w-4 text-yellow-500" />
          )}
          {project.isPublic ? (
            <Globe className="h-4 w-4 text-green-500" />
          ) : (
            <Lock className="h-4 w-4 text-gray-500" />
          )}
        </div>
        <CardDescription className="line-clamp-2">
          {project.description || '설명이 없습니다'}
        </CardDescription>
      </CardHeader>
      <CardContent>
        <div className="flex items-center justify-between text-sm text-muted-foreground">
          <div className="flex items-center gap-3">
            {project.memberCount && (
              <span className="flex items-center gap-1">
                <Users className="h-3 w-3" />
                {project.memberCount}
              </span>
            )}
            {project.fileCount !== undefined && (
              <span className="flex items-center gap-1">
                <Folder className="h-3 w-3" />
                {project.fileCount}
              </span>
            )}
          </div>
          <span className="flex items-center gap-1">
            <Calendar className="h-3 w-3" />
            {new Date(project.updatedAt).toLocaleDateString()}
          </span>
        </div>
        {role && (
          <Badge variant="secondary" className="mt-2">
            {role}
          </Badge>
        )}
      </CardContent>
    </Card>
  );

  return (
    <>
      {/* 헤더 */}
      <div className="flex items-center justify-between mb-6">
        <div>
          <h1 className="text-3xl font-bold">프로젝트</h1>
          <p className="text-muted-foreground mt-1">
            파일을 관리하고 팀과 협업하세요
          </p>
        </div>
        {/* <Button onClick={() => setCreateDialogOpen(true)}>
          <Plus className="h-4 w-4 mr-2" />
          새 프로젝트
        </Button> */}
      </div>

      {/* 검색 및 필터 */}
      <div className="flex items-center gap-3 mb-6">
        <div className="relative flex-1 max-w-md">
          <Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
          <Input
            placeholder="프로젝트 검색..."
            className="pl-9"
            value={searchQuery}
            onChange={(e) => setSearchQuery(e.target.value)}
          />
        </div>
        <Button
          variant="outline"
          size="icon"
          onClick={() => setViewMode(viewMode === 'grid' ? 'list' : 'grid')}
        >
          {viewMode === 'grid' ? <List className="h-4 w-4" /> : <Grid3x3 className="h-4 w-4" />}
        </Button>
      </div>

      {/* 프로젝트 목록 */}
      <Tabs defaultValue="owned" className="space-y-6">
        <TabsList>
          <TabsTrigger value="member">
            참여 프로젝트 ({filteredProjects.member?.length})
          </TabsTrigger>
          <TabsTrigger value="public">
            공개 프로젝트 ({filteredProjects.public?.length})
          </TabsTrigger>
        </TabsList>

        <TabsContent value="member">
          {filteredProjects.member?.length === 0 ? (
            <div className="text-center py-12">
              <Users className="h-12 w-12 text-muted-foreground mx-auto mb-3" />
              <p className="text-muted-foreground">참여 중인 프로젝트가 없습니다</p>
            </div>
          ) : viewMode === 'grid' ? (
            <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
              {filteredProjects.member?.map(project => (
                <ProjectCard key={project.id} project={project} role={project.role} />
              ))}
            </div>
          ) : (
            <div className="space-y-2">
              {filteredProjects.member?.map(project => (
                <Card 
                  key={project.id}
                  className="cursor-pointer hover:shadow transition-shadow"
                  onClick={() => router.push(`/evcp/data-room/${project.id}/files`)}
                >
                  <CardContent className="flex items-center justify-between p-4">
                    <div className="flex items-center gap-3">
                      <Folder className="h-5 w-5 text-blue-500" />
                      <div>
                        <p className="font-medium">{project.name}</p>
                        <p className="text-sm text-muted-foreground">
                          {project.description || '설명이 없습니다'}
                        </p>
                      </div>
                    </div>
                    <div className="flex items-center gap-2">
                      <Badge variant="secondary">{project.role}</Badge>
                      {project.isPublic ? (
                        <Globe className="h-4 w-4 text-green-500" />
                      ) : (
                        <Lock className="h-4 w-4 text-gray-500" />
                      )}
                    </div>
                  </CardContent>
                </Card>
              ))}
            </div>
          )}
        </TabsContent>

        <TabsContent value="public">
          {filteredProjects.public?.length === 0 ? (
            <div className="text-center py-12">
              <Globe className="h-12 w-12 text-muted-foreground mx-auto mb-3" />
              <p className="text-muted-foreground">공개 프로젝트가 없습니다</p>
            </div>
          ) : viewMode === 'grid' ? (
            <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
              {filteredProjects.public?.map(project => (
                <ProjectCard key={project.id} project={project} />
              ))}
            </div>
          ) : (
            <div className="space-y-2">
              {filteredProjects.public?.map(project => (
                <Card 
                  key={project.id}
                  className="cursor-pointer hover:shadow transition-shadow"
                  onClick={() => router.push(`/evcp/data-room/${project.id}/files`)}
                >
                  <CardContent className="flex items-center justify-between p-4">
                    <div className="flex items-center gap-3">
                      <Globe className="h-5 w-5 text-green-500" />
                      <div>
                        <p className="font-medium">{project.name}</p>
                        <p className="text-sm text-muted-foreground">
                          {project.description || '설명이 없습니다'}
                        </p>
                      </div>
                    </div>
                    <Badge variant="outline">공개</Badge>
                  </CardContent>
                </Card>
              ))}
            </div>
          )}
        </TabsContent>
      </Tabs>

      {/* 프로젝트 생성 다이얼로그 */}
      <Dialog open={createDialogOpen} onOpenChange={handleDialogClose}>
        <DialogContent>
          <DialogHeader>
            <DialogTitle>새 프로젝트 만들기</DialogTitle>
            <DialogDescription>
              팀과 파일을 공유할 새 프로젝트를 생성합니다
            </DialogDescription>
          </DialogHeader>
          
          <form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
            <div>
              <Label htmlFor="code">
                프로젝트 코드 <span className="text-red-500">*</span>
              </Label>
              <Input
                id="code"
                {...register('code', {
                  required: '프로젝트 코드는 필수입니다',
                  minLength: {
                    value: 2,
                    message: '프로젝트 코드는 최소 2자 이상이어야 합니다',
                  },
                  pattern: {
                    value: /^[A-Z0-9]+$/,
                    message: '프로젝트 코드는 대문자와 숫자만 사용 가능합니다',
                  },
                })}
                placeholder="SN1001"
                className={errors.code ? 'border-red-500' : ''}
              />
              {errors.code && (
                <p className="text-sm text-red-500 mt-1">{errors.code.message}</p>
              )}
            </div>

            <div>
              <Label htmlFor="name">
                프로젝트 이름 <span className="text-red-500">*</span>
              </Label>
              <Input
                id="name"
                {...register('name', {
                  required: '프로젝트 이름은 필수입니다',
                  minLength: {
                    value: 2,
                    message: '프로젝트 이름은 최소 2자 이상이어야 합니다',
                  },
                  maxLength: {
                    value: 50,
                    message: '프로젝트 이름은 50자를 초과할 수 없습니다',
                  },
                })}
                placeholder="예: FNLG"
                className={errors.name ? 'border-red-500' : ''}
              />
              {errors.name && (
                <p className="text-sm text-red-500 mt-1">{errors.name.message}</p>
              )}
            </div>
            
            <div>
              <Label htmlFor="description">설명 (선택)</Label>
              <Input
                id="description"
                {...register('description', {
                  maxLength: {
                    value: 200,
                    message: '설명은 200자를 초과할 수 없습니다',
                  },
                })}
                placeholder="프로젝트에 대한 간단한 설명"
                className={errors.description ? 'border-red-500' : ''}
              />
              {errors.description && (
                <p className="text-sm text-red-500 mt-1">{errors.description.message}</p>
              )}
            </div>
            
            <div className="flex items-center justify-between">
              <div>
                <Label htmlFor="public">공개 프로젝트</Label>
                <p className="text-sm text-muted-foreground">
                  모든 사용자가 이 프로젝트를 볼 수 있습니다
                </p>
              </div>
              <Switch
                id="public"
                checked={watchIsPublic}
                onCheckedChange={(checked) => setValue('isPublic', checked)}
              />
            </div>
            
            <DialogFooter>
              <Button 
                type="button" 
                variant="outline" 
                onClick={() => handleDialogClose(false)}
                disabled={isSubmitting}
              >
                취소
              </Button>
              <Button 
                type="submit"
                disabled={!isValid || isSubmitting}
              >
                {isSubmitting ? '생성 중...' : '프로젝트 생성'}
              </Button>
            </DialogFooter>
          </form>
        </DialogContent>
      </Dialog>
    </>
  );
}