import { useEffect, useState } from 'react' import { createMemo, deleteMemo, getMemos, type Org, type WorkMemo } from '../api/client' const CUSTOM_CONTACT_VALUE = '__custom__' const dateFormatter = new Intl.DateTimeFormat('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', }) function formatDateTime(createdAt: number): string { return dateFormatter.format(new Date(createdAt)) } function managerOptionLabel(org: Org): string | null { if (!org.managerName) { return null } return org.managerTitle ? `${org.managerName} ${org.managerTitle}` : org.managerName } /** 기관관리 상세의 업무메모 탭. 통화 등 담당자와의 연락 내용을 기록/조회/삭제한다. */ export default function WorkMemos({ org }: { org: Org }) { const managerLabel = managerOptionLabel(org) const [memos, setMemos] = useState([]) const [loading, setLoading] = useState(false) const [contactChoice, setContactChoice] = useState(managerLabel ?? CUSTOM_CONTACT_VALUE) const [customContactName, setCustomContactName] = useState('') const [body, setBody] = useState('') const [busy, setBusy] = useState(false) const [error, setError] = useState(null) useEffect(() => { let cancelled = false setLoading(true) getMemos(org.id) .then((data) => { if (!cancelled) { setMemos(data) } }) .finally(() => { if (!cancelled) { setLoading(false) } }) return () => { cancelled = true } }, [org.id]) const isCustom = contactChoice === CUSTOM_CONTACT_VALUE const effectiveContactName = (isCustom ? customContactName : contactChoice).trim() const canSubmit = effectiveContactName !== '' && body.trim() !== '' && !busy async function handleSubmit() { if (!canSubmit) { return } setBusy(true) setError(null) try { const created = await createMemo(org.id, { contactName: effectiveContactName, body }) setMemos((prev) => [created, ...prev]) setBody('') } catch (e) { setError(e instanceof Error ? e.message : '메모 등록에 실패했습니다.') } finally { setBusy(false) } } async function handleDelete(memo: WorkMemo) { if (!window.confirm('이 메모를 삭제할까요?')) { return } try { await deleteMemo(org.id, memo.id) setMemos((prev) => prev.filter((m) => m.id !== memo.id)) } catch (e) { setError(e instanceof Error ? e.message : '메모 삭제에 실패했습니다.') } } return (
{isCustom && (
setCustomContactName(e.target.value)} className="rounded-md border border-gray-300 px-2 py-1.5 text-sm" />
)}