import { useState, useCallback } from 'react'; import { useNavigate } from '@tanstack/react-router'; import { cn } from '@/lib/utils'; import { useAppStore, type ThemeMode } from '@/store/app-store'; import type { Project } from '@/lib/electron'; import { ProjectSwitcher } from './project-switcher'; import { PinnedProjects } from './pinned-projects'; import { TopBarActions } from './top-bar-actions'; import { OnboardingWizard } from '@/components/dialogs/onboarding-wizard'; import { getElectronAPI } from '@/lib/electron'; import { initializeProject, hasAppSpec, hasAutomakerDir } from '@/lib/project-init'; import { toast } from 'sonner'; export function TopBar() { const navigate = useNavigate(); const { currentProject, projects, pinnedProjectIds, trashedProjects, theme: globalTheme, upsertAndSetCurrentProject, } = useAppStore(); const [isDropdownOpen, setIsDropdownOpen] = useState(false); const [showOnboarding, setShowOnboarding] = useState(false); const [onboardingMode, setOnboardingMode] = useState<'new' | 'existing'>('new'); const [pendingProjectPath, setPendingProjectPath] = useState(undefined); const pinnedProjects = projects.filter((p) => pinnedProjectIds.includes(p.id)); const handleLogoClick = useCallback(() => { navigate({ to: '/dashboard' }); }, [navigate]); const handleNewProject = useCallback(() => { setPendingProjectPath(undefined); setOnboardingMode('new'); setShowOnboarding(true); }, []); const handleOpenFolder = useCallback(async () => { const api = getElectronAPI(); const result = await api.openDirectory(); if (!result.canceled && result.filePaths[0]) { const path = result.filePaths[0]; const name = path.split(/[/\\]/).filter(Boolean).pop() || 'Untitled Project'; try { const hadAutomakerDir = await hasAutomakerDir(path); const initResult = await initializeProject(path); if (!initResult.success) { toast.error('Failed to initialize project', { description: initResult.error || 'Unknown error occurred', }); return; } const trashedProject = trashedProjects.find((p) => p.path === path); const effectiveTheme = (trashedProject?.theme as ThemeMode | undefined) || (currentProject?.theme as ThemeMode | undefined) || globalTheme; upsertAndSetCurrentProject(path, name, effectiveTheme); const specExists = await hasAppSpec(path); if (!hadAutomakerDir || !specExists) { setPendingProjectPath(path); setOnboardingMode(hadAutomakerDir ? 'existing' : 'new'); setShowOnboarding(true); } else { navigate({ to: '/board' }); toast.success('Project opened', { description: `Opened ${name}` }); } } catch (error) { toast.error('Failed to open project', { description: error instanceof Error ? error.message : 'Unknown error', }); } } }, [trashedProjects, currentProject, globalTheme, upsertAndSetCurrentProject, navigate]); return (
{/* Logo */} {/* Pinned Projects */} {/* Project Dropdown */} {/* Spacer */}
{/* Actions */} {/* Onboarding Wizard */}
); }