212 lines
8.0 KiB
TypeScript
212 lines
8.0 KiB
TypeScript
'use client'
|
|
|
|
import { useMemo, useState } from 'react'
|
|
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
|
import { Button } from '@/components/ui/button'
|
|
import { useDialog } from '@/hooks/useDialog'
|
|
import type { DialogId } from '@/store/jotai/dialog'
|
|
|
|
export type MessageHistoryType = 'sms' | 'email'
|
|
export type MessageHistoryDetailType = 'template' | 'target' | 'success' | 'fail' | null
|
|
|
|
export type MessageHistoryRow = {
|
|
id: number
|
|
type: MessageHistoryType
|
|
title: string
|
|
templateName: string
|
|
sendInfo: string
|
|
targetHeader: string
|
|
targetCount: number
|
|
successCount: number
|
|
failCount: number
|
|
createdAt: string
|
|
sendAt: string
|
|
status: string
|
|
templatePreview?: string
|
|
targetPreview?: string[]
|
|
successPreview?: string[]
|
|
failPreview?: string[]
|
|
}
|
|
|
|
export type MessageHistoryDialogProps = {
|
|
dialogId?: DialogId
|
|
open?: boolean
|
|
onOpenChange?: (open: boolean) => void
|
|
rows: MessageHistoryRow[]
|
|
onDownload?: (row: MessageHistoryRow, detailType: Exclude<MessageHistoryDetailType, null>) => void | Promise<void>
|
|
}
|
|
|
|
const tabLabelMap: Record<MessageHistoryType, string> = {
|
|
sms: '문자',
|
|
email: '이메일',
|
|
}
|
|
|
|
const MessageHistoryDialog = ({
|
|
dialogId = 'messageHistoryDialog',
|
|
open,
|
|
onOpenChange,
|
|
rows,
|
|
onDownload,
|
|
}: MessageHistoryDialogProps) => {
|
|
const { isDialogOpen, showDialog, hideDialog } = useDialog()
|
|
const [tab, setTab] = useState<MessageHistoryType>('sms')
|
|
const [selectedId, setSelectedId] = useState<number | null>(null)
|
|
const [detailType, setDetailType] = useState<MessageHistoryDetailType>(null)
|
|
|
|
const resolvedOpen = open ?? isDialogOpen(dialogId)
|
|
|
|
const visibleRows = useMemo(() => rows.filter((row) => row.type === tab), [rows, tab])
|
|
|
|
const selectedRow = useMemo(
|
|
() => visibleRows.find((row) => row.id === selectedId) ?? null,
|
|
[visibleRows, selectedId],
|
|
)
|
|
|
|
const handleOpenChange = (nextOpen: boolean) => {
|
|
onOpenChange?.(nextOpen)
|
|
if (nextOpen) {
|
|
showDialog(dialogId)
|
|
return
|
|
}
|
|
hideDialog(dialogId)
|
|
setSelectedId(null)
|
|
setDetailType(null)
|
|
}
|
|
|
|
const handlePickDetail = (rowId: number, nextType: Exclude<MessageHistoryDetailType, null>) => {
|
|
setSelectedId(rowId)
|
|
setDetailType(nextType)
|
|
}
|
|
|
|
const detailContent = useMemo(() => {
|
|
if (!selectedRow || !detailType) return null
|
|
if (detailType === 'template') return selectedRow.templatePreview ?? '(미리보기 데이터 없음)'
|
|
if (detailType === 'target') return (selectedRow.targetPreview ?? []).join('\n')
|
|
if (detailType === 'success') return (selectedRow.successPreview ?? []).join('\n')
|
|
if (detailType === 'fail') return (selectedRow.failPreview ?? []).join('\n')
|
|
return null
|
|
}, [selectedRow, detailType])
|
|
|
|
return (
|
|
<Dialog open={resolvedOpen} onOpenChange={handleOpenChange}>
|
|
<DialogContent className="sm:max-w-[1200px]">
|
|
<DialogHeader>
|
|
<DialogTitle className="content-text-2xl font-bold">발송 이력</DialogTitle>
|
|
</DialogHeader>
|
|
|
|
<div className="flex gap-2">
|
|
<Button type="button" variant={tab === 'sms' ? 'default' : 'outline'} onClick={() => setTab('sms')}>
|
|
{tabLabelMap.sms}
|
|
</Button>
|
|
<Button type="button" variant={tab === 'email' ? 'default' : 'outline'} onClick={() => setTab('email')}>
|
|
{tabLabelMap.email}
|
|
</Button>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 xl:grid-cols-[1.7fr_1fr] gap-4">
|
|
<div className="rounded-md border overflow-auto">
|
|
<table className="w-full text-sm">
|
|
<thead className="sticky top-0 bg-white border-b">
|
|
<tr>
|
|
<th className="px-2 h-9 text-left">회차</th>
|
|
<th className="px-2 h-9 text-left">제목</th>
|
|
<th className="px-2 h-9 text-left">템플릿</th>
|
|
<th className="px-2 h-9 text-left">발송 정보</th>
|
|
<th className="px-2 h-9 text-left">타겟 헤더</th>
|
|
<th className="px-2 h-9 text-right">대상수</th>
|
|
<th className="px-2 h-9 text-right">성공</th>
|
|
<th className="px-2 h-9 text-right">실패</th>
|
|
<th className="px-2 h-9 text-left">등록 일시</th>
|
|
<th className="px-2 h-9 text-left">발송 일시</th>
|
|
<th className="px-2 h-9 text-left">상태</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{visibleRows.length === 0 && (
|
|
<tr>
|
|
<td colSpan={11} className="h-12 text-center text-muted-foreground">
|
|
발송 이력이 없습니다.
|
|
</td>
|
|
</tr>
|
|
)}
|
|
{visibleRows.map((row) => (
|
|
<tr key={`${row.type}-${row.id}`} className="border-b last:border-b-0">
|
|
<td className="px-2 h-10">{row.id}</td>
|
|
<td className="px-2 h-10">{row.title}</td>
|
|
<td className="px-2 h-10">
|
|
<button type="button" className="underline" onClick={() => handlePickDetail(row.id, 'template')}>
|
|
{row.templateName}
|
|
</button>
|
|
</td>
|
|
<td className="px-2 h-10">{row.sendInfo}</td>
|
|
<td className="px-2 h-10">
|
|
<button type="button" className="underline" onClick={() => handlePickDetail(row.id, 'target')}>
|
|
{row.targetHeader}
|
|
</button>
|
|
</td>
|
|
<td className="px-2 h-10 text-right">{row.targetCount}</td>
|
|
<td className="px-2 h-10 text-right">
|
|
<button type="button" className="underline" onClick={() => handlePickDetail(row.id, 'success')}>
|
|
{row.successCount}
|
|
</button>
|
|
</td>
|
|
<td className="px-2 h-10 text-right">
|
|
<button type="button" className="underline" onClick={() => handlePickDetail(row.id, 'fail')}>
|
|
{row.failCount}
|
|
</button>
|
|
</td>
|
|
<td className="px-2 h-10">{row.createdAt}</td>
|
|
<td className="px-2 h-10">{row.sendAt}</td>
|
|
<td className="px-2 h-10">{row.status}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<div className="rounded-md border p-4 space-y-3 min-h-80">
|
|
<p className="content-text-lg font-semibold">
|
|
{detailType === null && '상세 이력'}
|
|
{detailType === 'template' && '미리보기'}
|
|
{detailType === 'target' && '수신 대상'}
|
|
{detailType === 'success' && '성공 이력'}
|
|
{detailType === 'fail' && '실패 이력'}
|
|
</p>
|
|
|
|
{detailType === null && (
|
|
<p className="text-sm text-muted-foreground">
|
|
템플릿, 대상수, 성공, 실패 항목을 선택하면 상세 영역이 열립니다.
|
|
</p>
|
|
)}
|
|
|
|
{detailType !== null && (
|
|
<>
|
|
<pre className="rounded border p-3 text-sm whitespace-pre-wrap min-h-56">{detailContent}</pre>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => {
|
|
if (!selectedRow || !detailType) return
|
|
onDownload?.(selectedRow, detailType)
|
|
}}
|
|
disabled={!selectedRow || !detailType || !onDownload}
|
|
>
|
|
다운로드
|
|
</Button>
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<DialogFooter>
|
|
<Button type="button" onClick={() => handleOpenChange(false)}>
|
|
닫기
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|
|
|
|
export default MessageHistoryDialog
|