|
| 1 | +import { useState, useRef, useEffect } from 'react'; |
| 2 | +import { ChevronDown, Folder, Check } from 'lucide-react'; |
| 3 | +import { useProject } from '../contexts'; |
| 4 | +import { cn } from '../lib/utils'; |
| 5 | + |
| 6 | +export function ProjectSwitcher() { |
| 7 | + const { currentProject, availableProjects, loading, switchProject } = useProject(); |
| 8 | + const [open, setOpen] = useState(false); |
| 9 | + const [switching, setSwitching] = useState(false); |
| 10 | + const dropdownRef = useRef<HTMLDivElement>(null); |
| 11 | + |
| 12 | + // Close dropdown when clicking outside |
| 13 | + useEffect(() => { |
| 14 | + function handleClickOutside(event: MouseEvent) { |
| 15 | + if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { |
| 16 | + setOpen(false); |
| 17 | + } |
| 18 | + } |
| 19 | + document.addEventListener('mousedown', handleClickOutside); |
| 20 | + return () => document.removeEventListener('mousedown', handleClickOutside); |
| 21 | + }, []); |
| 22 | + |
| 23 | + const handleSwitch = async (projectId: string) => { |
| 24 | + if (projectId === currentProject?.id || switching) return; |
| 25 | + |
| 26 | + setSwitching(true); |
| 27 | + try { |
| 28 | + await switchProject(projectId); |
| 29 | + setOpen(false); |
| 30 | + // Refresh the page to reload all data with new project |
| 31 | + window.location.reload(); |
| 32 | + } catch { |
| 33 | + // Error is handled by context |
| 34 | + } finally { |
| 35 | + setSwitching(false); |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + if (loading && !currentProject) { |
| 40 | + return ( |
| 41 | + <div className="flex items-center gap-2 px-3 py-1.5 text-sm text-muted-foreground"> |
| 42 | + <Folder className="w-4 h-4" /> |
| 43 | + <span>Loading...</span> |
| 44 | + </div> |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + const allProjects = availableProjects; |
| 49 | + const hasMultipleProjects = allProjects.length > 1; |
| 50 | + |
| 51 | + return ( |
| 52 | + <div className="relative" ref={dropdownRef}> |
| 53 | + <button |
| 54 | + onClick={() => hasMultipleProjects && setOpen(!open)} |
| 55 | + disabled={!hasMultipleProjects || switching} |
| 56 | + className={cn( |
| 57 | + 'flex items-center gap-2 px-3 py-1.5 text-sm rounded-md border transition-colors', |
| 58 | + hasMultipleProjects |
| 59 | + ? 'hover:bg-secondary cursor-pointer' |
| 60 | + : 'cursor-default', |
| 61 | + switching && 'opacity-50' |
| 62 | + )} |
| 63 | + > |
| 64 | + <Folder className="w-4 h-4 text-primary" /> |
| 65 | + <span className="max-w-[150px] truncate font-medium"> |
| 66 | + {currentProject?.name || 'No project'} |
| 67 | + </span> |
| 68 | + {hasMultipleProjects && ( |
| 69 | + <ChevronDown className={cn('w-4 h-4 transition-transform', open && 'rotate-180')} /> |
| 70 | + )} |
| 71 | + </button> |
| 72 | + |
| 73 | + {open && hasMultipleProjects && ( |
| 74 | + <div className="absolute top-full left-0 mt-1 w-64 bg-background border rounded-md shadow-lg z-50"> |
| 75 | + <div className="p-2 border-b"> |
| 76 | + <span className="text-xs font-medium text-muted-foreground uppercase"> |
| 77 | + Switch Project |
| 78 | + </span> |
| 79 | + </div> |
| 80 | + <div className="max-h-64 overflow-y-auto"> |
| 81 | + {allProjects.map((project) => { |
| 82 | + const isCurrent = project.id === currentProject?.id; |
| 83 | + return ( |
| 84 | + <button |
| 85 | + key={project.id} |
| 86 | + onClick={() => handleSwitch(project.id)} |
| 87 | + disabled={isCurrent || switching} |
| 88 | + className={cn( |
| 89 | + 'w-full flex items-center gap-3 px-3 py-2 text-left hover:bg-secondary transition-colors', |
| 90 | + isCurrent && 'bg-secondary/50' |
| 91 | + )} |
| 92 | + > |
| 93 | + <Folder className="w-4 h-4 flex-shrink-0 text-muted-foreground" /> |
| 94 | + <div className="flex-1 min-w-0"> |
| 95 | + <div className="text-sm font-medium truncate">{project.name}</div> |
| 96 | + <div className="text-xs text-muted-foreground truncate">{project.path}</div> |
| 97 | + </div> |
| 98 | + {isCurrent && <Check className="w-4 h-4 text-primary flex-shrink-0" />} |
| 99 | + </button> |
| 100 | + ); |
| 101 | + })} |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + )} |
| 105 | + </div> |
| 106 | + ); |
| 107 | +} |
0 commit comments