import { atom } from 'jotai'; import { ChatUser, ChatMessage } from './types'; // ------------------------------------------------------------ // Basic UI State Atoms // ------------------------------------------------------------ // Sidebar visibility state export const leftOpenAtom = atom(false); export const rightOpenAtom = atom(false); export const rightExpandedAtom = atom(false); // Track if right sidebar is expanded in web view export const bottomOpenAtom = atom(false); export const topOpenAtom = atom(false); export const isMobileAtom = atom(false); // Chat states export const chatDetailOpenAtom = atom(false); export const selectedUserAtom = atom(null); // Messages storage with optimization for many user export const messagesAtom = atom>({}); // ------------------------------------------------------------ // Derived UI State Atoms // ------------------------------------------------------------ // Check if any mobile sidebar is open export const anyMobileSidebarOpenAtom = atom((get) => { const isMobile = get(isMobileAtom); const leftOpen = get(leftOpenAtom); const rightOpen = get(rightOpenAtom); return isMobile && (leftOpen || rightOpen); }); // Detect fullscreen chat mode export const isChatFullscreenModeAtom = atom((get) => { const isMobile = get(isMobileAtom); const chatDetailOpen = get(chatDetailOpenAtom); const leftOpen = get(leftOpenAtom); return !isMobile && chatDetailOpen && leftOpen; }); // Configuration utility for debugging export const sidebarConfigAtom = atom((get) => { return { leftOpen: get(leftOpenAtom), rightOpen: get(rightOpenAtom), rightExpanded: get(rightExpandedAtom), chatDetailOpen: get(chatDetailOpenAtom), isMobile: get(isMobileAtom), anyMobileSidebarOpen: get(anyMobileSidebarOpenAtom), isChatFullscreenMode: get(isChatFullscreenModeAtom) }; }); // ------------------------------------------------------------ // Action Atoms // ------------------------------------------------------------ // Mobile sidebar priority management export const mobileSidebarPriorityAtom = atom( null, (get, set, wasMobile: boolean) => { const isMobile = get(isMobileAtom); const leftOpen = get(leftOpenAtom); const rightOpen = get(rightOpenAtom); if (!wasMobile && isMobile && leftOpen && rightOpen) { // Keep right sidebar open and close left in mobile view set(leftOpenAtom, false); } } ); // Toggle left sidebar with smart handling of chat detail state export const toggleLeftSidebarAtom = atom( null, (get, set) => { const isMobile = get(isMobileAtom); const currentValue = get(leftOpenAtom); const chatDetailOpen = get(chatDetailOpenAtom); // In mobile, close other sidebars if (isMobile && !currentValue) { set(rightOpenAtom, false); } // When closing the left sidebar, also reset chat detail states if (currentValue && chatDetailOpen) { set(chatDetailOpenAtom, false); set(selectedUserAtom, null); } set(leftOpenAtom, !currentValue); } ); // Toggle right sidebar with mobile awareness export const toggleRightSidebarAtom = atom( null, (get, set) => { const isMobile = get(isMobileAtom); const currentValue = get(rightOpenAtom); // In mobile, close other sidebars if (isMobile && !currentValue) { set(leftOpenAtom, false); set(chatDetailOpenAtom, false); } set(rightOpenAtom, !currentValue); // Reset expanded state when closing if (!isMobile && !currentValue === false) { set(rightExpandedAtom, false); } } ); // Toggle right sidebar expanded state in web view export const toggleRightExpandedAtom = atom( null, (get, set) => { const isMobile = get(isMobileAtom); const currentValue = get(rightExpandedAtom); // Only toggle in web view if (!isMobile) { set(rightExpandedAtom, !currentValue); } } ); // Chat user selection action export const selectUserAtom = atom( null, (get, set, user: ChatUser) => { set(selectedUserAtom, user); set(chatDetailOpenAtom, true); } ); // Return to user list action export const goBackToListAtom = atom( null, (get, set) => { set(chatDetailOpenAtom, false); set(selectedUserAtom, null); set(messagesAtom, {}); } ); // Add a message to the current chat export const addMessageAtom = atom( null, (get, set, message: ChatMessage) => { const selectedUser = get(selectedUserAtom); if (!selectedUser) return; const allMessages = get(messagesAtom); const userMessages = allMessages[selectedUser.id] || []; set(messagesAtom, { ...allMessages, [selectedUser.id]: [...userMessages, message] }); } );