All files / src/components/admin ProProjectsSection.tsx

95.23% Statements 20/21
87.5% Branches 7/8
100% Functions 8/8
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185                                                                        138x 138x       138x   138x 6x 6x 6x 6x 5x 5x   1x 1x   6x       138x             13x                                                 244x                                                                                       1x       1x                                       8x                     1x                              
import { useState } from "react";
import { notify } from "../../lib/notify";
import { Link } from "@tanstack/react-router";
import { Plus } from "lucide-react";
import { AdminBand } from "./AdminPageHead";
import { Card } from "../ui/card";
import { Button } from "../ui/button";
import { DrawerDialog } from "../ui/drawer-dialog";
import { Field, Label } from "../ui/field";
import { Input } from "../ui/input";
import { formatDate } from "../../lib/utils";
import { getErrorMessage, type Project } from "../../lib/api";
import { Textarea } from "../ui/textarea";
import {
	Table,
	TableBody,
	TableCaption,
	TableCell,
	TableHead,
	TableHeader,
	TableRow,
} from "../ui/table";
import { StatusBadge } from "../ui/status-badge";
 
interface ProProjectsSectionProps {
	projects: Project[];
	onCreateProject: (project: {
		title: string;
		description: string;
	}) => Promise<void>;
}
 
export function ProProjectsSection({
	projects,
	onCreateProject,
}: ProProjectsSectionProps) {
	const [showNewProject, setShowNewProject] = useState(false);
	const [newProject, setNewProject] = useState({
		title: "",
		description: "",
	});
	const [isCreatingProject, setIsCreatingProject] = useState(false);
 
	const handleCreateProject = async () => {
		Iif (!newProject.title.trim()) return;
		setIsCreatingProject(true);
		try {
			await onCreateProject(newProject);
			setShowNewProject(false);
			setNewProject({ title: "", description: "" });
		} catch (err) {
			console.error("Failed to create project:", err);
			notify.error(getErrorMessage(err));
		} finally {
			setIsCreatingProject(false);
		}
	};
 
	return (
		<>
			<section className="space-y-2">
				<AdminBand
					title="Projects"
					count={projects.length}
					actions={
						<Button size="sm" onClick={() => setShowNewProject(true)}>
							<Plus aria-hidden="true" />
							Add Project
						</Button>
					}
				/>
				{projects.length === 0 ? (
					<p className="py-8 text-center text-sm text-foreground-muted">
						No projects yet. Click "Add Project" to create one.
					</p>
				) : (
					<Card className="overflow-hidden">
						<Table density="dense">
							<TableCaption>
								This studio's projects, with their publish status.
							</TableCaption>
							<TableHeader>
								<TableRow>
									<TableHead>Title</TableHead>
									<TableHead>Status</TableHead>
									<TableHead numeric>Created</TableHead>
								</TableRow>
							</TableHeader>
							<TableBody>
								{projects.map((project) => (
									<TableRow key={project.id}>
										<TableCell>
											<Link
												to={`/admin/projects/${project.id}`}
												className="text-primary-600 hover:underline"
											>
												{project.title}
											</Link>
										</TableCell>
										<TableCell>
											<StatusBadge
												status={project.status}
												variant={
													project.status === "published"
														? "success"
														: project.status === "draft"
															? "warning"
															: "default"
												}
											/>
										</TableCell>
										{/* `en-IN` from one place. */}
										<TableCell
											numeric
											className="whitespace-nowrap text-foreground-muted"
										>
											{formatDate(project.dateCreated)}
										</TableCell>
									</TableRow>
								))}
							</TableBody>
						</Table>
					</Card>
				)}
			</section>
 
			{/*
			 * New Project — on the DrawerDialog primitive. It used to be a
			 * hand-rolled fixed overlay with no focus trap, no Escape handler and
			 * no dialog role; `DrawerDialog` carries all three via
			 * `use-modal-a11y`, and gives the sheet form on a phone for free.
			 */}
			<DrawerDialog
				open={showNewProject}
				onClose={() => setShowNewProject(false)}
				title="Create New Project"
				footer={
					<div className="flex justify-end gap-2">
						<Button variant="outline" onClick={() => setShowNewProject(false)}>
							Cancel
						</Button>
						<Button
							onClick={handleCreateProject}
							isLoading={isCreatingProject}
							disabled={!newProject.title.trim()}
						>
							Create Project
						</Button>
					</div>
				}
			>
				<div className="space-y-4">
					<Field controlId="project-title" required>
						<Label>Title</Label>
						<Input
							id="project-title"
							value={newProject.title}
							onChange={(e) =>
								setNewProject({ ...newProject, title: e.target.value })
							}
							placeholder="Project title"
						/>
					</Field>
					<Field controlId="project-description">
						<Label>Description</Label>
						<Textarea
							id="project-description"
							value={newProject.description}
							onChange={(e) =>
								setNewProject({
									...newProject,
									description: e.target.value,
								})
							}
							rows={3}
							className="w-full"
							placeholder="Brief description..."
						/>
					</Field>
				</div>
			</DrawerDialog>
		</>
	);
}