Organize/components/projects/create-project-dialog.tsx

83 lines
2.5 KiB
TypeScript

"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { Plus } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { useProjects } from "@/components/projects/projects-context";
const TITLE_MAX = 60;
/** The "+ New project" tile on the /projects list -- creates, then jumps
* straight into the new project's (empty) board. */
export function CreateProjectDialog() {
const { addProject } = useProjects();
const router = useRouter();
const [open, setOpen] = useState(false);
const [title, setTitle] = useState("");
const [pending, setPending] = useState(false);
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
const trimmed = title.trim();
if (!trimmed) return;
setPending(true);
const project = await addProject(trimmed);
setPending(false);
if (!project) return;
setTitle("");
setOpen(false);
router.push(`/projects/${project.id}`);
}
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger
render={
<button
className="flex h-32 flex-col items-center justify-center gap-2 rounded-xl border-2 border-dashed border-border text-muted-foreground transition-colors hover:border-primary hover:text-primary"
aria-label="New project"
>
<Plus className="size-6" />
<span className="text-sm font-medium">New Project</span>
</button>
}
/>
<DialogContent>
<DialogHeader>
<DialogTitle>New project</DialogTitle>
</DialogHeader>
<form onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="project-title">Title</Label>
<Input
id="project-title"
value={title}
maxLength={TITLE_MAX}
autoFocus
onChange={(e) => setTitle(e.target.value)}
placeholder="e.g. Kitchen Remodel"
/>
</div>
<DialogFooter>
<Button type="submit" disabled={pending || !title.trim()}>
{pending ? "Creating…" : "Create project"}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}