import { useState, useEffect, useMemo } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { useStore } from '@nanostores/react'; import { Switch } from '@radix-ui/react-switch'; import * as RadixDialog from '@radix-ui/react-dialog'; import { classNames } from '~/utils/classNames'; import { TabManagement } from '~/components/@settings/shared/components/TabManagement'; import { TabTile } from '~/components/@settings/shared/components/TabTile'; 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 { tabConfigurationStore, developerModeStore, setDeveloperMode } from '~/lib/stores/settings'; import { profileStore } from '~/lib/stores/profile'; import type { TabType, TabVisibilityConfig, DevTabConfig, Profile } from './types'; import { TAB_LABELS, DEFAULT_TAB_CONFIG } from './constants'; import { resetTabConfiguration } from '~/lib/stores/settings'; import { DialogTitle } from '~/components/ui/Dialog'; import { AvatarDropdown } from './AvatarDropdown'; // Import all tab components import ProfileTab from '~/components/@settings/tabs/profile/ProfileTab'; import SettingsTab from '~/components/@settings/tabs/settings/SettingsTab'; import NotificationsTab from '~/components/@settings/tabs/notifications/NotificationsTab'; import FeaturesTab from '~/components/@settings/tabs/features/FeaturesTab'; import DataTab from '~/components/@settings/tabs/data/DataTab'; import DebugTab from '~/components/@settings/tabs/debug/DebugTab'; import { EventLogsTab } from '~/components/@settings/tabs/event-logs/EventLogsTab'; import UpdateTab from '~/components/@settings/tabs/update/UpdateTab'; import ConnectionsTab from '~/components/@settings/tabs/connections/ConnectionsTab'; import CloudProvidersTab from '~/components/@settings/tabs/providers/cloud/CloudProvidersTab'; import ServiceStatusTab from '~/components/@settings/tabs/providers/status/ServiceStatusTab'; import LocalProvidersTab from '~/components/@settings/tabs/providers/local/LocalProvidersTab'; import TaskManagerTab from '~/components/@settings/tabs/task-manager/TaskManagerTab'; interface ControlPanelProps { open: boolean; onClose: () => void; } interface TabWithDevType extends TabVisibilityConfig { isExtraDevTab?: 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', 'service-status': 'Monitor cloud LLM service status', 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', 'tab-management': 'Configure visible tabs and their order', }; export const ControlPanel = ({ open, onClose }: ControlPanelProps) => { // State const [activeTab, setActiveTab] = useState(null); const [loadingTab, setLoadingTab] = useState(null); const [showTabManagement, setShowTabManagement] = useState(false); // Store values const tabConfiguration = useStore(tabConfigurationStore); const developerMode = useStore(developerModeStore); const profile = useStore(profileStore) as Profile; // 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(); // Add visibleTabs logic using useMemo const visibleTabs = useMemo(() => { if (!tabConfiguration?.userTabs || !Array.isArray(tabConfiguration.userTabs)) { console.warn('Invalid tab configuration, resetting to defaults'); resetTabConfiguration(); return []; } // In developer mode, show ALL tabs without restrictions if (developerMode) { // Combine all unique tabs from both user and developer configurations const allTabs = new Set([ ...DEFAULT_TAB_CONFIG.map((tab) => tab.id), ...tabConfiguration.userTabs.map((tab) => tab.id), ...(tabConfiguration.developerTabs || []).map((tab) => tab.id), ]); // Create a complete tab list with all tabs visible const devTabs = Array.from(allTabs).map((tabId) => { // Try to find existing configuration for this tab const existingTab = tabConfiguration.developerTabs?.find((t) => t.id === tabId) || tabConfiguration.userTabs?.find((t) => t.id === tabId) || DEFAULT_TAB_CONFIG.find((t) => t.id === tabId); return { id: tabId, visible: true, window: 'developer' as const, order: existingTab?.order || DEFAULT_TAB_CONFIG.findIndex((t) => t.id === tabId), }; }); // Add Tab Management tile for developer mode const tabManagementConfig: DevTabConfig = { id: 'tab-management', visible: true, window: 'developer', order: devTabs.length, isExtraDevTab: true, }; devTabs.push(tabManagementConfig); return devTabs.sort((a, b) => a.order - b.order); } // In user mode, only show visible user tabs const notificationsDisabled = profile?.preferences?.notifications === false; 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 in user preferences if (tab.id === 'notifications' && notificationsDisabled) { return false; } // Only show tabs that are explicitly visible and assigned to the user window return tab.visible && tab.window === 'user'; }) .sort((a, b) => a.order - b.order); }, [tabConfiguration, developerMode, profile?.preferences?.notifications]); // Handlers const handleBack = () => { if (showTabManagement) { setShowTabManagement(false); } else if (activeTab) { setActiveTab(null); } }; const handleDeveloperModeChange = (checked: boolean) => { console.log('Developer mode changed:', checked); setDeveloperMode(checked); }; // Add effect to log developer mode changes useEffect(() => { console.log('Current developer mode:', developerMode); }, [developerMode]); const getTabComponent = (tabId: TabType | 'tab-management') => { if (tabId === 'tab-management') { return ; } switch (tabId) { 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 ; case 'service-status': 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 handleTabClick = (tabId: TabType) => { setLoadingTab(tabId); setActiveTab(tabId); setShowTabManagement(false); // Acknowledge notifications based on tab switch (tabId) { case 'update': acknowledgeUpdate(); break; case 'features': acknowledgeAllFeatures(); break; case 'notifications': markAllAsRead(); break; case 'connection': acknowledgeIssue(); break; case 'debug': acknowledgeAllIssues(); break; } // Clear loading state after a delay setTimeout(() => setLoadingTab(null), 500); }; return (
{/* Header */}
{activeTab || showTabManagement ? ( ) : (
)} {showTabManagement ? 'Tab Management' : activeTab ? TAB_LABELS[activeTab] : 'Control Panel'}
{/* Developer Mode Controls */}
{/* Mode Toggle */}
Toggle developer mode
{/* Avatar and Dropdown */}
{/* Close Button */}
{/* Content */}
{showTabManagement ? ( ) : activeTab ? ( getTabComponent(activeTab) ) : ( {(visibleTabs as TabWithDevType[]).map((tab: TabWithDevType) => ( handleTabClick(tab.id as TabType)} isActive={activeTab === tab.id} hasUpdate={getTabUpdateStatus(tab.id)} statusMessage={getStatusMessage(tab.id)} description={TAB_DESCRIPTIONS[tab.id]} isLoading={loadingTab === tab.id} className="h-full" /> ))} )}
); };