|
| 1 | +import { CHAT_THREADS, INITIAL_STATUS_BY_THREAD, THREAD_CONTENT } from '@shared/mocks/data/chat'; |
| 2 | +import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; |
| 3 | +import type { ThreadContent } from '@shared/types/chat'; |
| 4 | + |
| 5 | +type StatusOption = (typeof INITIAL_STATUS_BY_THREAD)[string]; |
| 6 | + |
| 7 | +const formatMetaLabel = (date: Date) => { |
| 8 | + const hours = date.getHours(); |
| 9 | + const minutes = date.getMinutes(); |
| 10 | + const meridiem = hours >= 12 ? '오후' : '오전'; |
| 11 | + const hourLabel = `${hours % 12 === 0 ? 12 : hours % 12}`.padStart(2, '0'); |
| 12 | + const minuteLabel = `${minutes}`.padStart(2, '0'); |
| 13 | + return `읽음 · ${meridiem} ${hourLabel}:${minuteLabel}`; |
| 14 | +}; |
| 15 | + |
| 16 | +export const useChatState = () => { |
| 17 | + const [selectedThreadId, setSelectedThreadId] = useState<string | null>(null); |
| 18 | + const [message, setMessage] = useState(''); |
| 19 | + const [unreadByThread, setUnreadByThread] = useState<Record<string, boolean>>(() => |
| 20 | + CHAT_THREADS.reduce<Record<string, boolean>>((acc, thread) => { |
| 21 | + if (thread.hasAlert) { |
| 22 | + acc[thread.id] = true; |
| 23 | + } |
| 24 | + return acc; |
| 25 | + }, {}) |
| 26 | + ); |
| 27 | + const [threadContent, setThreadContent] = useState<Record<string, ThreadContent>>(THREAD_CONTENT); |
| 28 | + const [statusByThread, setStatusByThread] = useState<Record<string, StatusOption>>(INITIAL_STATUS_BY_THREAD); |
| 29 | + const messageListRef = useRef<HTMLDivElement | null>(null); |
| 30 | + |
| 31 | + const hasSelection = selectedThreadId !== null; |
| 32 | + const activeThread = useMemo( |
| 33 | + () => (selectedThreadId ? threadContent[selectedThreadId] : null), |
| 34 | + [selectedThreadId, threadContent] |
| 35 | + ); |
| 36 | + |
| 37 | + const markThreadReadIfNeeded = useCallback( |
| 38 | + (threadId: string | null) => { |
| 39 | + if (!threadId) { |
| 40 | + return; |
| 41 | + } |
| 42 | + if (!unreadByThread[threadId]) { |
| 43 | + return; |
| 44 | + } |
| 45 | + setUnreadByThread((prev) => ({ ...prev, [threadId]: false })); |
| 46 | + }, |
| 47 | + [unreadByThread] |
| 48 | + ); |
| 49 | + |
| 50 | + const handleSend = (nextMessage: string) => { |
| 51 | + if (!selectedThreadId || !nextMessage.trim()) { |
| 52 | + setMessage(''); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const now = new Date(); |
| 57 | + const metaLabel = formatMetaLabel(now); |
| 58 | + |
| 59 | + setThreadContent((prev) => { |
| 60 | + const current = prev[selectedThreadId]; |
| 61 | + if (!current) { |
| 62 | + return prev; |
| 63 | + } |
| 64 | + return { |
| 65 | + ...prev, |
| 66 | + [selectedThreadId]: { |
| 67 | + ...current, |
| 68 | + timeline: [ |
| 69 | + ...current.timeline, |
| 70 | + { |
| 71 | + type: 'message', |
| 72 | + id: `local-${Date.now()}`, |
| 73 | + role: 'sender', |
| 74 | + message: nextMessage.trim(), |
| 75 | + meta: metaLabel, |
| 76 | + metaDateTime: now.toISOString(), |
| 77 | + }, |
| 78 | + ], |
| 79 | + }, |
| 80 | + }; |
| 81 | + }); |
| 82 | + setMessage(''); |
| 83 | + }; |
| 84 | + |
| 85 | + const scrollToBottom = () => { |
| 86 | + const container = messageListRef.current; |
| 87 | + if (!container) { |
| 88 | + return; |
| 89 | + } |
| 90 | + container.scrollTop = container.scrollHeight; |
| 91 | + }; |
| 92 | + |
| 93 | + const handleScroll = useCallback(() => { |
| 94 | + if (!selectedThreadId) { |
| 95 | + return; |
| 96 | + } |
| 97 | + const container = messageListRef.current; |
| 98 | + if (!container) { |
| 99 | + return; |
| 100 | + } |
| 101 | + const isAtBottom = |
| 102 | + container.scrollHeight <= container.clientHeight + 1 || |
| 103 | + container.scrollTop + container.clientHeight >= container.scrollHeight - 2; |
| 104 | + if (isAtBottom) { |
| 105 | + markThreadReadIfNeeded(selectedThreadId); |
| 106 | + } |
| 107 | + }, [selectedThreadId, markThreadReadIfNeeded]); |
| 108 | + |
| 109 | + useEffect(() => { |
| 110 | + if (!selectedThreadId) { |
| 111 | + return; |
| 112 | + } |
| 113 | + const handle = window.requestAnimationFrame(() => { |
| 114 | + scrollToBottom(); |
| 115 | + handleScroll(); |
| 116 | + }); |
| 117 | + return () => window.cancelAnimationFrame(handle); |
| 118 | + }, [selectedThreadId, activeThread?.timeline.length, handleScroll]); |
| 119 | + |
| 120 | + return { |
| 121 | + activeThread, |
| 122 | + hasSelection, |
| 123 | + message, |
| 124 | + messageListRef, |
| 125 | + selectedThreadId, |
| 126 | + setMessage, |
| 127 | + setSelectedThreadId, |
| 128 | + unreadByThread, |
| 129 | + statusByThread, |
| 130 | + setStatusByThread, |
| 131 | + handleSend, |
| 132 | + handleScroll, |
| 133 | + }; |
| 134 | +}; |
0 commit comments