import { useEffect, useState } from 'react' import { createMemo, deleteMemo, getMemos, updateMemo, type Org, type WorkMemo } from '../api/client' 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)) } /** * 기관관리 상세의 업무메모 탭. 통화 등 담당자와의 연락 내용을 기록/수정/삭제한다. * * 작성자는 화면에서 고르지 않는다 - 요청자 확정대로 로그인한 사용자를 서버가 직접 * 기록하므로, 목록에 보이는 작성자는 서버가 채워 준 값이다. */ export default function WorkMemos({ org }: { org: Org }) { const [memos, setMemos] = useState([]) const [loading, setLoading] = useState(false) const [body, setBody] = useState('') const [busy, setBusy] = useState(false) const [error, setError] = useState(null) /** 수정 중인 메모 id. null이면 새 메모 작성 모드다. */ const [editingId, setEditingId] = useState(null) const [editingBody, setEditingBody] = useState('') 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 canSubmit = body.trim() !== '' && !busy async function handleSubmit() { if (!canSubmit) { return } setBusy(true) setError(null) try { const created = await createMemo(org.id, { body }) setMemos((prev) => [created, ...prev]) setBody('') } catch (e) { setError(e instanceof Error ? e.message : '메모 등록에 실패했습니다.') } finally { setBusy(false) } } async function handleUpdate(memo: WorkMemo) { if (editingBody.trim() === '' || busy) { return } setBusy(true) setError(null) try { const saved = await updateMemo(org.id, memo.id, { body: editingBody }) setMemos((prev) => prev.map((m) => (m.id === memo.id ? saved : m))) setEditingId(null) setEditingBody('') } 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 (

작성자는 로그인한 사용자로 자동 기록됩니다.