import { useState, useEffect } from 'react'; import { createPortal } from 'react-dom'; import { X, Save, User, Calendar } from 'lucide-react'; import { NoteItem } from '@/types'; import { comms } from '@/communication'; interface NoteEditModalProps { isOpen: boolean; editingItem: NoteItem | null; processing: boolean; onClose: () => void; onSave: (formData: { pdate: string; title: string; uid: string; description: string; share: boolean; guid: string; }) => void; initialEditMode?: boolean; } export function NoteEditModal({ isOpen, editingItem, processing, onClose, onSave, }: NoteEditModalProps) { const [pdate, setPdate] = useState(''); const [title, setTitle] = useState(''); const [uid, setUid] = useState(''); const [description, setDescription] = useState(''); const [share, setShare] = useState(false); const [guid, setGuid] = useState(''); // 현재 로그인 사용자 정보 로드 const [currentUserId, setCurrentUserId] = useState(''); const [currentUserLevel, setCurrentUserLevel] = useState(0); useEffect(() => { const loadUserInfo = async () => { try { const loginStatus = await comms.checkLoginStatus(); if (loginStatus.Success && loginStatus.IsLoggedIn && loginStatus.User) { setCurrentUserId(loginStatus.User.Id); setCurrentUserLevel(loginStatus.User.Level); } } catch (error) { console.error('로그인 정보 로드 오류:', error); } }; loadUserInfo(); }, []); // 모달이 열릴 때 폼 데이터 초기화 useEffect(() => { if (isOpen) { if (editingItem) { // 기존 메모 setPdate(editingItem.pdate ? editingItem.pdate.split('T')[0] : ''); setTitle(editingItem.title || ''); setUid(editingItem.uid || currentUserId); setDescription(editingItem.description || ''); setShare(editingItem.share || false); setGuid(editingItem.guid || ''); } else { // 신규 메모 setPdate(new Date().toISOString().split('T')[0]); setTitle(''); setUid(currentUserId); setDescription(''); setShare(false); setGuid(''); } } }, [isOpen, editingItem, currentUserId]); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); onSave({ pdate, title, uid, description, share, guid }); }; if (!isOpen) return null; // 권한 체크: 자신의 메모이거나 관리자만 수정 가능 const canEdit = !editingItem || editingItem.uid === currentUserId || currentUserLevel >= 5; const canChangeUser = currentUserLevel >= 5; const canChangeShare = !editingItem || editingItem.uid === currentUserId; return createPortal(