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
|
// lib/tbe-last/table/dialogs/email-documents-dialog.tsx
"use client"
import * as React from "react"
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Textarea } from "@/components/ui/textarea"
import { Badge } from "@/components/ui/badge"
import { ScrollArea } from "@/components/ui/scroll-area"
import {
FileText,
X,
Plus,
Mail,
Loader2,
AlertCircle,
} from "lucide-react"
import { toast } from "sonner"
import { sendDocumentsEmail } from "../service"
interface EmailDocumentsDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
selectedDocuments: any[]
sessionDetail: any
onSuccess?: () => void
}
export function EmailDocumentsDialog({
open,
onOpenChange,
selectedDocuments,
sessionDetail,
onSuccess
}: EmailDocumentsDialogProps) {
const [recipients, setRecipients] = React.useState<string[]>([])
const [currentEmail, setCurrentEmail] = React.useState("")
const [ccRecipients, setCcRecipients] = React.useState<string[]>([])
const [currentCc, setCurrentCc] = React.useState("")
const [comments, setComments] = React.useState("")
const [isLoading, setIsLoading] = React.useState(false)
// 이메일 유효성 검사
const validateEmail = (email: string) => {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
return re.test(email)
}
// 수신자 추가
const handleAddRecipient = () => {
if (currentEmail && validateEmail(currentEmail)) {
if (!recipients.includes(currentEmail)) {
setRecipients([...recipients, currentEmail])
setCurrentEmail("")
} else {
toast.error("이미 추가된 이메일입니다")
}
} else {
toast.error("올바른 이메일 주소를 입력하세요")
}
}
// CC 수신자 추가
const handleAddCc = () => {
if (currentCc && validateEmail(currentCc)) {
if (!ccRecipients.includes(currentCc)) {
setCcRecipients([...ccRecipients, currentCc])
setCurrentCc("")
} else {
toast.error("이미 추가된 이메일입니다")
}
} else {
toast.error("올바른 이메일 주소를 입력하세요")
}
}
// 수신자 제거
const removeRecipient = (email: string) => {
setRecipients(recipients.filter(r => r !== email))
}
// CC 수신자 제거
const removeCc = (email: string) => {
setCcRecipients(ccRecipients.filter(r => r !== email))
}
// 이메일 전송
const handleSendEmail = async () => {
if (recipients.length === 0) {
toast.error("최소 한 명의 수신자를 추가하세요")
return
}
if (selectedDocuments.length === 0) {
toast.error("선택된 문서가 없습니다")
return
}
setIsLoading(true)
try {
const result = await sendDocumentsEmail({
to: recipients,
cc: ccRecipients.length > 0 ? ccRecipients : undefined,
documents: selectedDocuments.map(doc => ({
documentId: doc.documentId,
documentReviewId: doc.documentReviewId,
documentName: doc.documentName,
filePath: doc.filePath,
documentType: doc.documentType,
documentSource: doc.documentSource,
reviewStatus: doc.reviewStatus,
})),
comments,
sessionInfo: {
sessionId: sessionDetail?.session?.tbeSessionId,
sessionTitle: sessionDetail?.session?.title,
buyerName: sessionDetail?.session?.buyerName,
vendorName: sessionDetail?.session?.vendorName,
}
})
if (result.success) {
toast.success("이메일이 성공적으로 전송되었습니다")
onSuccess?.()
onOpenChange(false)
// 초기화
setRecipients([])
setCcRecipients([])
setComments("")
setCurrentEmail("")
setCurrentCc("")
} else {
throw new Error(result.error || "이메일 전송 실패")
}
} catch (error) {
console.error("Email send error:", error)
toast.error(error instanceof Error ? error.message : "이메일 전송 중 오류가 발생했습니다")
} finally {
setIsLoading(false)
}
}
// 파일 크기 포맷
const formatFileSize = (bytes: number) => {
if (bytes === 0) return '0 Bytes'
const k = 1024
const sizes = ['Bytes', 'KB', 'MB', 'GB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle>Send Documents via Email</DialogTitle>
<DialogDescription>
선택한 {selectedDocuments.length}개의 문서를 이메일로 전송합니다
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
{/* 수신자 입력 */}
<div className="grid gap-2">
<Label htmlFor="recipients">수신자 (To) *</Label>
<div className="flex gap-2">
<Input
id="recipients"
type="email"
placeholder="이메일 주소 입력"
value={currentEmail}
onChange={(e) => setCurrentEmail(e.target.value)}
onKeyPress={(e) => {
if (e.key === 'Enter') {
e.preventDefault()
handleAddRecipient()
}
}}
/>
<Button
type="button"
size="sm"
onClick={handleAddRecipient}
variant="outline"
>
<Plus className="h-4 w-4" />
</Button>
</div>
<div className="flex flex-wrap gap-2 mt-2">
{recipients.map((email) => (
<Badge key={email} variant="secondary" className="gap-1">
{email}
<X
className="h-3 w-3 cursor-pointer hover:text-destructive"
onClick={() => removeRecipient(email)}
/>
</Badge>
))}
</div>
</div>
{/* CC 입력 */}
<div className="grid gap-2">
<Label htmlFor="cc">참조 (CC)</Label>
<div className="flex gap-2">
<Input
id="cc"
type="email"
placeholder="이메일 주소 입력 (선택사항)"
value={currentCc}
onChange={(e) => setCurrentCc(e.target.value)}
onKeyPress={(e) => {
if (e.key === 'Enter') {
e.preventDefault()
handleAddCc()
}
}}
/>
<Button
type="button"
size="sm"
onClick={handleAddCc}
variant="outline"
>
<Plus className="h-4 w-4" />
</Button>
</div>
<div className="flex flex-wrap gap-2 mt-2">
{ccRecipients.map((email) => (
<Badge key={email} variant="secondary" className="gap-1">
{email}
<X
className="h-3 w-3 cursor-pointer hover:text-destructive"
onClick={() => removeCc(email)}
/>
</Badge>
))}
</div>
</div>
{/* 코멘트 입력 */}
<div className="grid gap-2">
<Label htmlFor="comments">메시지</Label>
<Textarea
id="comments"
placeholder="추가 메시지를 입력하세요 (선택사항)"
value={comments}
onChange={(e) => setComments(e.target.value)}
rows={4}
/>
</div>
{/* 첨부 파일 목록 */}
<div className="grid gap-2">
<Label>첨부 파일 ({selectedDocuments.length}개)</Label>
<ScrollArea className="h-[200px] w-full rounded-md border p-4">
<div className="space-y-2">
{selectedDocuments.map((doc, index) => (
<div key={doc.documentReviewId} className="flex items-center gap-2 p-2 rounded-md bg-muted/50">
<FileText className="h-4 w-4 text-muted-foreground" />
<div className="flex-1 min-w-0">
<p className="text-sm font-medium truncate">{doc.documentName}</p>
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<span>{doc.documentType}</span>
<span>•</span>
<Badge variant={doc.documentSource === "buyer" ? "default" : "secondary"} className="text-xs">
{doc.documentSource}
</Badge>
{doc.reviewStatus && (
<>
<span>•</span>
<span>{doc.reviewStatus}</span>
</>
)}
</div>
</div>
</div>
))}
</div>
</ScrollArea>
</div>
{/* 경고 메시지 */}
{selectedDocuments.some(doc => doc.reviewStatus === "반려") && (
<div className="flex items-start gap-2 p-3 rounded-md bg-destructive/10 text-destructive">
<AlertCircle className="h-4 w-4 mt-0.5" />
<p className="text-sm">
반려된 문서가 포함되어 있습니다. 계속 진행하시겠습니까?
</p>
</div>
)}
</div>
<DialogFooter>
<Button
variant="outline"
onClick={() => onOpenChange(false)}
disabled={isLoading}
>
취소
</Button>
<Button
onClick={handleSendEmail}
disabled={isLoading || recipients.length === 0}
>
{isLoading ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
전송 중...
</>
) : (
<>
<Mail className="mr-2 h-4 w-4" />
이메일 전송
</>
)}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}
|