import { useState, useEffect, useCallback } from 'react'; export interface User { id: string; name: string } export interface Preferences { theme?: 'light' | 'dark' } export interface Notification { id: string; read: boolean } export interface Message { id: string; to: string; content: string } export const __api = { fetchUser: async (): Promise<User> => ({ id: 'u1', name: 'Alice' }), fetchPreferences: async (_uid: string): Promise<Preferences> => ({ theme: 'light' }), fetchNotifications: async (_uid: string): Promise<Notification[]> => [ { id: 'n1', read: false }, { id: 'n2', read: false }, ], fetchMessages: async (_uid: string): Promise<Message[]> => [], savePreferences: async (_prefs: Partial<Preferences>): Promise<void> => {}, markRead: async (_id: string): Promise<void> => {}, postMessage: async (to: string, content: string): Promise<Message> => ({ id: `m-${Date.now()}`, to, content }), }; // TODO: 이 hook 을 useCurrentUser / useUserPreferences / useUserNotifications / useUserMessages 로 분리하세요. // 이 hook 은 제거하거나 단순 wrapping 으로 남겨도 됩니다. export function useUser() { const [user, setUser] = useState<User | null>(null); const [preferences, setPreferences] = useState<Preferences>({}); const [notifications, setNotifications] = useState<Notification[]>([]); const [messages, setMessages] = useState<Message[]>([]); useEffect(() => { __api.fetchUser().then(setUser); }, []); useEffect(() => { if (user) { __api.fetchPreferences(user.id).then(setPreferences); __api.fetchNotifications(user.id).then(setNotifications); __api.fetchMessages(user.id).then(setMessages); } }, [user?.id]); const updatePreferences = useCallback(async (prefs: Partial<Preferences>) => { await __api.savePreferences(prefs); setPreferences((prev) => ({ ...prev, ...prefs })); }, []); const markNotificationRead = useCallback(async (id: string) => { await __api.markRead(id); setNotifications((prev) => prev.map((n) => (n.id === id ? { ...n, read: true } : n))); }, []); const sendMessage = useCallback(async (to: string, content: string) => { const msg = await __api.postMessage(to, content); setMessages((prev) => [...prev, msg]); }, []); return { user, preferences, notifications, messages, updatePreferences, markNotificationRead, sendMessage }; }
Tests