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