'use client' import { KriDialog } from '@/components/common/KriDialog' import { Button } from '@/components/ui/button' import { useState, useRef } from 'react' import {Download, Upload, X} from 'lucide-react' import { useDialog } from '@/hooks/useDialog' import type { DialogId } from '@/store/jotai/dialog' export type UserUploadDialogProps = { /** jotai로 관리할 dialog id */ dialogId?: DialogId /** 필요한 경우 부모에서 직접 제어 */ open?: boolean onOpenChange?: (open: boolean) => void onUpload: (file: File) => void | Promise onDownloadTemplate?: () => void | Promise } const UserUploadDialog = ({ dialogId = 'userUploadDialog', open, onOpenChange, onUpload, onDownloadTemplate, }: UserUploadDialogProps) => { const { isDialogOpen, showDialog, hideDialog } = useDialog(); const [submitting, setSubmitting] = useState(false); const [selectedFile, setSelectedFile] = useState(null); const fileInputRef = useRef(null); // 실제 Dialog에 전달할 open 값 const resolvedOpen = open ?? isDialogOpen(dialogId); // 부모 콜백과 jotai 상태를 함께 처리 const handleOpenChange = (nextOpen: boolean) => { onOpenChange?.(nextOpen) if (nextOpen) { showDialog(dialogId) } else { hideDialog(dialogId) } } const handleFileSelect = (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (file) { const allowedTypes = [ 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'text/csv', ] if ( allowedTypes.includes(file.type) || file.name.endsWith('.xlsx') || file.name.endsWith('.xls') || file.name.endsWith('.csv') ) { setSelectedFile(file) } else { alert("엑셀 파일만 업로드 가능합니다. (.xlsx, .xls, .csv)") } } } const handleRemoveFile = () => { setSelectedFile(null) if (fileInputRef.current) { fileInputRef.current.value = '' } } const handleUploadClick = () => { fileInputRef.current?.click() } const handleSubmit = async () => { if (!selectedFile) { alert("파일을 선택해주세요."); return; } setSubmitting(true); try { await onUpload(selectedFile); handleRemoveFile(); handleOpenChange(false); } catch (e) { console.error(e); } finally { setSubmitting(false); } }; // Dialog가 닫힐 때 파일 선택 상태 초기화 const handleDialogOpenChange = (nextOpen: boolean) => { if (!nextOpen) { handleRemoveFile() } handleOpenChange(nextOpen) } return ( User upload} contentClassName="sm:max-w-[600px]" bodyClassName="space-y-6 py-4" footer={
} >
{selectedFile && ( )}
) } export default UserUploadDialog