"use client" import { $fetch } from "@repo/lib/api" import { DEFAULT_PROJECT_ID } from "@repo/lib/constants" import { Button } from "@repo/ui/components/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@repo/ui/components/dialog" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@repo/ui/components/dropdown-menu" import { Label } from "@repo/ui/components/label" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@repo/ui/components/select" import { useQuery } from "@tanstack/react-query" import { ChevronDown, FolderIcon, Loader2, MoreHorizontal, Plus, Trash2, } from "lucide-react" import { AnimatePresence, motion } from "motion/react" import { useState } from "react" import { useProjectMutations } from "@/hooks/use-project-mutations" import { useProjectName } from "@/hooks/use-project-name" import { useProject } from "@/stores" import { CreateProjectDialog } from "./create-project-dialog" interface Project { id: string name: string containerTag: string createdAt: string updatedAt: string isExperimental?: boolean } export function ProjectSelector() { const [isOpen, setIsOpen] = useState(false) const [showCreateDialog, setShowCreateDialog] = useState(false) const { selectedProject } = useProject() const projectName = useProjectName() const { switchProject, deleteProjectMutation } = useProjectMutations() const [deleteDialog, setDeleteDialog] = useState<{ open: boolean project: null | { id: string; name: string; containerTag: string } action: "move" | "delete" targetProjectId: string }>({ open: false, project: null, action: "move", targetProjectId: DEFAULT_PROJECT_ID, }) const { data: projects = [], isLoading } = useQuery({ queryKey: ["projects"], queryFn: async () => { const response = await $fetch("@get/projects") if (response.error) { throw new Error(response.error?.message || "Failed to load projects") } return response.data?.projects || [] }, staleTime: 30 * 1000, }) const handleProjectSelect = (containerTag: string) => { switchProject(containerTag) setIsOpen(false) } const handleCreateNewProject = () => { setIsOpen(false) setShowCreateDialog(true) } return (
{isOpen && ( <> setIsOpen(false)} />
{/* User Projects */} {projects .filter((p: Project) => p.containerTag !== DEFAULT_PROJECT_ID) .map((project: Project) => (
{ e.stopPropagation() setDeleteDialog({ open: true, project: { id: project.id, name: project.name, containerTag: project.containerTag, }, action: "move", targetProjectId: "", }) setIsOpen(false) }} > Delete Project
))} New Project
)}
{/* Delete Project Dialog */} {deleteDialog.open && deleteDialog.project && ( setDeleteDialog((prev) => ({ ...prev, open })) } open={deleteDialog.open} > Delete Project Are you sure you want to delete "{deleteDialog.project.name} "? Choose what to do with the documents in this project.
setDeleteDialog((prev) => ({ ...prev, action: "move", })) } type="radio" />
{deleteDialog.action === "move" && ( )}
setDeleteDialog((prev) => ({ ...prev, action: "delete", })) } type="radio" />
{deleteDialog.action === "delete" && ( ⚠️ This action cannot be undone. All documents will be permanently deleted. )}
)}
) }