import * as RadixDialog from '@radix-ui/react-dialog'; import * as DropdownMenu from '@radix-ui/react-dropdown-menu'; import { motion } from 'framer-motion'; import React, { useState, useEffect, useMemo } from 'react'; import { classNames } from '~/utils/classNames'; import { DialogTitle } from '~/components/ui/Dialog'; import { Switch } from '~/components/ui/Switch'; import type { TabType, TabVisibilityConfig } from '~/components/settings/settings.types'; import { TAB_LABELS } from '~/components/settings/settings.types'; import { DeveloperWindow } from '~/components/settings/developer/DeveloperWindow'; import { TabTile } from '~/components/settings/shared/TabTile'; import { useStore } from '@nanostores/react'; import { DndProvider, useDrag, useDrop } from 'react-dnd'; import { HTML5Backend } from 'react-dnd-html5-backend'; import ProfileTab from '~/components/settings/profile/ProfileTab'; import SettingsTab from '~/components/settings/settings/SettingsTab'; import NotificationsTab from '~/components/settings/notifications/NotificationsTab'; import FeaturesTab from '~/components/settings/features/FeaturesTab'; import DataTab from '~/components/settings/data/DataTab'; import DebugTab from '~/components/settings/debug/DebugTab'; import { EventLogsTab } from '~/components/settings/event-logs/EventLogsTab'; import UpdateTab from '~/components/settings/update/UpdateTab'; import ConnectionsTab from '~/components/settings/connections/ConnectionsTab'; import { useUpdateCheck } from '~/lib/hooks/useUpdateCheck'; import { useFeatures } from '~/lib/hooks/useFeatures'; import { useNotifications } from '~/lib/hooks/useNotifications'; import { useConnectionStatus } from '~/lib/hooks/useConnectionStatus'; import { useDebugStatus } from '~/lib/hooks/useDebugStatus'; import CloudProvidersTab from '~/components/settings/providers/CloudProvidersTab'; import LocalProvidersTab from '~/components/settings/providers/LocalProvidersTab'; import TaskManagerTab from '~/components/settings/task-manager/TaskManagerTab'; import { tabConfigurationStore, resetTabConfiguration, updateTabConfiguration, developerModeStore, setDeveloperMode, } from '~/lib/stores/settings'; interface DraggableTabTileProps { tab: TabVisibilityConfig; index: number; moveTab: (dragIndex: number, hoverIndex: number) => void; onClick: () => void; isActive: boolean; hasUpdate: boolean; statusMessage: string; description: string; isLoading?: boolean; } const TAB_DESCRIPTIONS: Record = { profile: 'Manage your profile and account settings', settings: 'Configure application preferences', notifications: 'View and manage your notifications', features: 'Explore new and upcoming features', data: 'Manage your data and storage', 'cloud-providers': 'Configure cloud AI providers and models', 'local-providers': 'Configure local AI providers and models', connection: 'Check connection status and settings', debug: 'Debug tools and system information', 'event-logs': 'View system events and logs', update: 'Check for updates and release notes', 'task-manager': 'Monitor system resources and processes', }; const DraggableTabTile = ({ tab, index, moveTab, onClick, isActive, hasUpdate, statusMessage, description, isLoading, }: DraggableTabTileProps) => { const [{ isDragging }, drag] = useDrag({ type: 'tab', item: { index }, collect: (monitor) => ({ isDragging: monitor.isDragging(), }), }); const [, drop] = useDrop({ accept: 'tab', hover: (item: { index: number }) => { if (item.index === index) { return; } moveTab(item.index, index); item.index = index; }, }); const dragDropRef = (node: HTMLDivElement | null) => { if (node) { drag(drop(node)); } }; return (
); }; interface UsersWindowProps { open: boolean; onClose: () => void; } export const UsersWindow = ({ open, onClose }: UsersWindowProps) => { const [activeTab, setActiveTab] = useState(null); const [loadingTab, setLoadingTab] = useState(null); const tabConfiguration = useStore(tabConfigurationStore); const developerMode = useStore(developerModeStore); const [showDeveloperWindow, setShowDeveloperWindow] = useState(false); const [profile, setProfile] = useState(() => { const saved = localStorage.getItem('bolt_user_profile'); return saved ? JSON.parse(saved) : { avatar: null, notifications: true }; }); // Status hooks const { hasUpdate, currentVersion, acknowledgeUpdate } = useUpdateCheck(); const { hasNewFeatures, unviewedFeatures, acknowledgeAllFeatures } = useFeatures(); const { hasUnreadNotifications, unreadNotifications, markAllAsRead } = useNotifications(); const { hasConnectionIssues, currentIssue, acknowledgeIssue } = useConnectionStatus(); const { hasActiveWarnings, activeIssues, acknowledgeAllIssues } = useDebugStatus(); // Listen for profile changes useEffect(() => { const handleStorageChange = (e: StorageEvent) => { if (e.key === 'bolt_user_profile') { const newProfile = e.newValue ? JSON.parse(e.newValue) : { avatar: null, notifications: true }; setProfile(newProfile); } }; window.addEventListener('storage', handleStorageChange); return () => window.removeEventListener('storage', handleStorageChange); }, []); // Listen for settings toggle event useEffect(() => { const handleToggleSettings = () => { if (!open) { // Open settings panel setActiveTab('settings'); onClose(); // Close any other open panels } }; document.addEventListener('toggle-settings', handleToggleSettings); return () => document.removeEventListener('toggle-settings', handleToggleSettings); }, [open, onClose]); // Ensure tab configuration is properly initialized useEffect(() => { if (!tabConfiguration || !tabConfiguration.userTabs || !tabConfiguration.developerTabs) { console.warn('Tab configuration is invalid, resetting to defaults'); resetTabConfiguration(); } else { // Validate tab configuration structure const isValid = tabConfiguration.userTabs.every( (tab) => tab && typeof tab.id === 'string' && typeof tab.visible === 'boolean' && typeof tab.window === 'string' && typeof tab.order === 'number', ) && tabConfiguration.developerTabs.every( (tab) => tab && typeof tab.id === 'string' && typeof tab.visible === 'boolean' && typeof tab.window === 'string' && typeof tab.order === 'number', ); if (!isValid) { console.warn('Tab configuration is malformed, resetting to defaults'); resetTabConfiguration(); } } }, [tabConfiguration]); // Handle developer mode changes const handleDeveloperModeChange = (checked: boolean) => { setDeveloperMode(checked); if (checked) { setShowDeveloperWindow(true); } }; // Handle developer window close const handleDeveloperWindowClose = () => { setShowDeveloperWindow(false); setDeveloperMode(false); }; const handleBack = () => { setActiveTab(null); }; // Only show tabs that are assigned to the user window AND are visible const visibleUserTabs = useMemo(() => { console.log('Filtering user tabs with configuration:', tabConfiguration); if (!tabConfiguration?.userTabs || !Array.isArray(tabConfiguration.userTabs)) { console.warn('Invalid tab configuration, using empty array'); return []; } return tabConfiguration.userTabs .filter((tab) => { if (!tab || typeof tab.id !== 'string') { console.warn('Invalid tab entry:', tab); return false; } // Hide notifications tab if notifications are disabled if (tab.id === 'notifications' && !profile.notifications) { console.log('Hiding notifications tab due to disabled notifications'); return false; } // Ensure the tab has the required properties if (typeof tab.visible !== 'boolean' || typeof tab.window !== 'string' || typeof tab.order !== 'number') { console.warn('Tab missing required properties:', tab); return false; } // Only show tabs that are explicitly visible and assigned to the user window const isVisible = tab.visible && tab.window === 'user'; console.log(`Tab ${tab.id} visibility:`, isVisible); return isVisible; }) .sort((a: TabVisibilityConfig, b: TabVisibilityConfig) => { const orderA = typeof a.order === 'number' ? a.order : 0; const orderB = typeof b.order === 'number' ? b.order : 0; return orderA - orderB; }); }, [tabConfiguration, profile.notifications]); console.log('Filtered visible user tabs:', visibleUserTabs); const moveTab = (dragIndex: number, hoverIndex: number) => { const draggedTab = visibleUserTabs[dragIndex]; const targetTab = visibleUserTabs[hoverIndex]; console.log('Moving tab:', { draggedTab, targetTab }); // Update the order of the dragged and target tabs const updatedDraggedTab = { ...draggedTab, order: targetTab.order }; const updatedTargetTab = { ...targetTab, order: draggedTab.order }; // Update both tabs in the store updateTabConfiguration(updatedDraggedTab); updateTabConfiguration(updatedTargetTab); }; const handleTabClick = async (tabId: TabType) => { setLoadingTab(tabId); setActiveTab(tabId); // Acknowledge the status based on tab type switch (tabId) { case 'update': await acknowledgeUpdate(); break; case 'features': await acknowledgeAllFeatures(); break; case 'notifications': await markAllAsRead(); break; case 'connection': acknowledgeIssue(); break; case 'debug': await acknowledgeAllIssues(); break; } // Simulate loading time (remove this in production) await new Promise((resolve) => setTimeout(resolve, 1000)); setLoadingTab(null); }; const getTabComponent = () => { switch (activeTab) { case 'profile': return ; case 'settings': return ; case 'notifications': return ; case 'features': return ; case 'data': return ; case 'cloud-providers': return ; case 'local-providers': return ; case 'connection': return ; case 'debug': return ; case 'event-logs': return ; case 'update': return ; case 'task-manager': return ; default: return null; } }; const getTabUpdateStatus = (tabId: TabType): boolean => { switch (tabId) { case 'update': return hasUpdate; case 'features': return hasNewFeatures; case 'notifications': return hasUnreadNotifications; case 'connection': return hasConnectionIssues; case 'debug': return hasActiveWarnings; default: return false; } }; const getStatusMessage = (tabId: TabType): string => { switch (tabId) { case 'update': return `New update available (v${currentVersion})`; case 'features': return `${unviewedFeatures.length} new feature${unviewedFeatures.length === 1 ? '' : 's'} to explore`; case 'notifications': return `${unreadNotifications.length} unread notification${unreadNotifications.length === 1 ? '' : 's'}`; case 'connection': return currentIssue === 'disconnected' ? 'Connection lost' : currentIssue === 'high-latency' ? 'High latency detected' : 'Connection issues detected'; case 'debug': { const warnings = activeIssues.filter((i) => i.type === 'warning').length; const errors = activeIssues.filter((i) => i.type === 'error').length; return `${warnings} warning${warnings === 1 ? '' : 's'}, ${errors} error${errors === 1 ? '' : 's'}`; } default: return ''; } }; const renderHeader = () => (
{activeTab ? ( ) : ( )} {activeTab ? TAB_LABELS[activeTab] : 'Bolt Control Panel'}
handleTabClick('profile')} >
Profile handleTabClick('settings')} >
Settings {profile.notifications && ( <> handleTabClick('notifications')} >
Notifications {hasUnreadNotifications && ( {unreadNotifications.length} )} )}
Close
); return ( <>
{/* Header */} {renderHeader()} {/* Content */}
{activeTab ? ( getTabComponent() ) : (
{visibleUserTabs.map((tab: TabVisibilityConfig, index: number) => ( handleTabClick(tab.id)} isActive={activeTab === tab.id} hasUpdate={getTabUpdateStatus(tab.id)} statusMessage={getStatusMessage(tab.id)} description={TAB_DESCRIPTIONS[tab.id]} isLoading={loadingTab === tab.id} /> ))}
)}
); };