import { useEffect, useState } from 'react'; import { Link, Outlet, useLocation, useNavigate } from 'react-router-dom'; import { useAuthStore, type UserStatus } from '../stores/auth.ts'; import { useWebSocketStore } from '../stores/ws.ts'; import { useServerStore } from '../stores/server.ts'; import { ServerBar } from './ServerBar.tsx'; import { ChannelList } from './ChannelList.tsx'; import { ConversationList } from './ConversationList.tsx'; import { MemberList } from './MemberList.tsx'; import { VoicePanel } from './VoicePanel.tsx'; import { ServerSettingsModal } from './ServerSettingsModal.tsx'; const STATUS_CYCLE: UserStatus[] = ['online', 'idle', 'dnd', 'offline']; function statusColor(status: UserStatus): string { switch (status) { case 'online': return 'bg-gb-green'; case 'idle': return 'bg-gb-yellow'; case 'dnd': return 'bg-gb-red'; default: return 'bg-gb-gray'; } } function statusLabel(status: UserStatus): string { return status.toUpperCase(); } export function Layout() { const isAuthenticated = useAuthStore((state) => state.isAuthenticated); const isLoading = useAuthStore((state) => state.isLoading); const user = useAuthStore((state) => state.user); const fetchMe = useAuthStore((state) => state.fetchMe); const logout = useAuthStore((state) => state.logout); const updateProfile = useAuthStore((state) => state.updateProfile); const wsConnect = useWebSocketStore((s) => s.connect); const wsDisconnect = useWebSocketStore((s) => s.disconnect); const setActiveServer = useServerStore((s) => s.setActiveServer); const navigate = useNavigate(); const location = useLocation(); const [showStatusMenu, setShowStatusMenu] = useState(false); const [dmMode, setDmMode] = useState(false); const [showServerSettings, setShowServerSettings] = useState(false); const activeServerId = useServerStore((s) => s.activeServerId); useEffect(() => { fetchMe(); wsConnect(); return () => { wsDisconnect(); }; }, [fetchMe, wsConnect, wsDisconnect]); useEffect(() => { if (!isLoading && !isAuthenticated && location.pathname !== '/login') { navigate('/login', { replace: true }); } }, [isLoading, isAuthenticated, location.pathname, navigate]); const handleStatusChange = async (status: UserStatus) => { setShowStatusMenu(false); try { await updateProfile({ status }); } catch (err) { // Error is surfaced via auth store; menu closes optimistically. } }; const currentStatus = user?.status ?? 'offline'; if (isLoading) { return (