| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 1 | import { ChangeEvent, useCallback, useEffect, useState } from "react"; |
| 2 | import { Project, useProjectId, useStateStore } from "./lib/state"; |
| 3 | import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./components/ui/select"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 4 | import { Input } from "./components/ui/input"; |
| 5 | import { Button } from "./components/ui/button"; |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 6 | import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "./components/ui/dialog"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 7 | import { useToast } from "@/hooks/use-toast"; |
| gio | bc47f9f | 2025-05-12 08:31:07 +0000 | [diff] [blame] | 8 | import { Separator } from "./components/ui/separator"; |
| 9 | import { Plus } from "lucide-react"; |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 10 | import { z } from "zod"; |
| gio | 8cadbc7 | 2025-05-16 07:51:02 +0000 | [diff] [blame^] | 11 | import { cn } from "./lib/utils"; |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 12 | |
| 13 | const createNewSchema = z.object({ |
| 14 | id: z.string().min(1), |
| 15 | }); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 16 | |
| gio | 8cadbc7 | 2025-05-16 07:51:02 +0000 | [diff] [blame^] | 17 | export function ProjectSelect({ className }: { className?: string }) { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 18 | const { toast } = useToast(); |
| 19 | const store = useStateStore(); |
| 20 | const [projects, setProjects] = useState<Project[]>([]); |
| gio | 7461e50 | 2025-05-12 10:11:55 +0000 | [diff] [blame] | 21 | const projectId = useProjectId(); |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 22 | // Track the selected project ID locally to ensure UI consistency |
| 23 | const [selectedId, setSelectedId] = useState<string | undefined>(projectId); |
| gio | 7461e50 | 2025-05-12 10:11:55 +0000 | [diff] [blame] | 24 | |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 25 | // Keep local state in sync with global state |
| gio | 7461e50 | 2025-05-12 10:11:55 +0000 | [diff] [blame] | 26 | useEffect(() => { |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 27 | setSelectedId(projectId); |
| 28 | }, [projectId]); |
| 29 | |
| 30 | const refreshProjects = useCallback( |
| 31 | async (id?: string) => { |
| 32 | console.log("refreshProjects", id); |
| 33 | try { |
| 34 | const resp = await fetch("/api/project"); |
| 35 | const projectList = await resp.json(); |
| 36 | const sortedProjects = [...projectList].sort((a, b) => |
| 37 | a.name.localeCompare(b.name, undefined, { sensitivity: "base" }), |
| 38 | ); |
| 39 | setProjects(sortedProjects); |
| 40 | if (id && !sortedProjects.some((p) => p.id === id)) { |
| 41 | throw new Error("MUST NOT REACH!"); |
| 42 | } |
| 43 | if (id == null) { |
| 44 | id = sortedProjects[0].id; |
| 45 | } |
| 46 | if (id !== selectedId) { |
| 47 | setSelectedId(id); |
| 48 | store.setProject(id); |
| 49 | } |
| 50 | } catch (e) { |
| 51 | console.log(e); |
| 52 | } |
| 53 | }, |
| 54 | [selectedId, store, setProjects, setSelectedId], |
| 55 | ); |
| 56 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 57 | useEffect(() => { |
| 58 | refreshProjects(); |
| gio | 359a685 | 2025-05-14 03:38:24 +0000 | [diff] [blame] | 59 | }, [refreshProjects]); |
| gio | 7461e50 | 2025-05-12 10:11:55 +0000 | [diff] [blame] | 60 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 61 | const [createNewOpen, setCreateNewOpen] = useState(false); |
| 62 | const onSelect = useCallback( |
| 63 | (projectId: string) => { |
| 64 | if (projectId === "create-new") { |
| 65 | setCreateNewOpen(true); |
| 66 | } else { |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 67 | setSelectedId(projectId); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 68 | store.setProject(projectId); |
| 69 | } |
| 70 | }, |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 71 | [store, setSelectedId], |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 72 | ); |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 73 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 74 | const [name, setName] = useState<string | undefined>(undefined); |
| 75 | const updateName = useCallback( |
| 76 | (e: ChangeEvent<HTMLInputElement>) => { |
| 77 | setName(e.target.value); |
| 78 | }, |
| 79 | [setName], |
| 80 | ); |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 81 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 82 | const createNew = useCallback(() => { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 83 | if (!name) { |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 84 | toast({ |
| 85 | variant: "destructive", |
| 86 | title: "Name is required", |
| 87 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | fetch("/api/project", { |
| 91 | method: "POST", |
| 92 | headers: { |
| 93 | "Content-Type": "application/json", |
| 94 | }, |
| 95 | body: JSON.stringify({ |
| 96 | name: name, |
| 97 | }), |
| 98 | }) |
| 99 | .then(async (resp) => { |
| 100 | if (!resp.ok) { |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | const result = createNewSchema.safeParse(await resp.json()); |
| 104 | if (!result.success) { |
| 105 | toast({ |
| 106 | variant: "destructive", |
| 107 | title: `Failed to create project: ${name}`, |
| 108 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 109 | return; |
| 110 | } |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 111 | const { id } = result.data; |
| 112 | await refreshProjects(id); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 113 | setCreateNewOpen(false); |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 114 | setName(undefined); // Clear the input for next time |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 115 | toast({ |
| 116 | title: `Created project: ${name}`, |
| 117 | }); |
| 118 | }) |
| 119 | .catch((e) => { |
| 120 | console.log(e); |
| 121 | toast({ |
| 122 | variant: "destructive", |
| 123 | title: `Failed to create project: ${name}`, |
| 124 | }); |
| 125 | }); |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 126 | }, [name, setCreateNewOpen, toast, refreshProjects]); |
| 127 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 128 | return ( |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 129 | <> |
| 130 | <Select onValueChange={onSelect} value={selectedId}> |
| gio | 8cadbc7 | 2025-05-16 07:51:02 +0000 | [diff] [blame^] | 131 | <SelectTrigger |
| 132 | className={cn("!border-none", "!shadow-none", "!focus:ring-0", "!focus:ring-offset-0", className)} |
| 133 | > |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 134 | <SelectValue placeholder="Choose Project" /> |
| 135 | </SelectTrigger> |
| 136 | <SelectContent> |
| 137 | {projects.map((p) => ( |
| 138 | <SelectItem key={p.id} value={p.id}> |
| 139 | {p.name} |
| 140 | </SelectItem> |
| 141 | ))} |
| 142 | <Separator /> |
| 143 | <SelectItem value={"create-new"}> |
| 144 | <div className="flex flex-row items-center gap-1"> |
| gio | bc47f9f | 2025-05-12 08:31:07 +0000 | [diff] [blame] | 145 | <Plus /> |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 146 | <div>New project</div> |
| 147 | </div> |
| 148 | </SelectItem> |
| 149 | </SelectContent> |
| 150 | </Select> |
| 151 | <Dialog open={createNewOpen} onOpenChange={setCreateNewOpen}> |
| 152 | <DialogContent> |
| 153 | <DialogHeader> |
| 154 | <DialogTitle>New project</DialogTitle> |
| 155 | </DialogHeader> |
| 156 | <Input |
| 157 | type="text" |
| 158 | placeholder="Name" |
| 159 | onChange={updateName} |
| 160 | value={name || ""} |
| 161 | onKeyDown={(e) => { |
| 162 | if (e.key === "Enter") { |
| 163 | createNew(); |
| 164 | } |
| 165 | }} |
| 166 | /> |
| 167 | <DialogFooter> |
| 168 | <Button onClick={createNew}>Create</Button> |
| 169 | </DialogFooter> |
| 170 | </DialogContent> |
| 171 | </Dialog> |
| 172 | </> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 173 | ); |
| 174 | } |