All files / src/components/admin taxonomy-list.tsx

100% Statements 38/38
100% Branches 29/29
100% Functions 15/15
100% Lines 38/38

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                                                                            92x 92x   92x 5x     92x 2x 2x     92x 2x 2x 1x 1x 1x     1x 1x 1x 1x   1x 1x 1x     92x 1x 1x     92x 28x     140x           64x 5x             59x             141x               135x     5x 2x 2x                                             334x                                 3x               3x                       4x                          
import { useState } from "react";
import { Edit2, Trash2, Eye, EyeOff, GripVertical } from "lucide-react";
import { Button } from "../ui/button";
import type { TaxonomyItem } from "../../lib/api";
import {
	Table,
	TableBody,
	TableCaption,
	TableCell,
	TableHead,
	TableHeader,
	TableRow,
} from "../ui/table";
import { StatusBadge } from "../ui/status-badge";
 
type TaxonomyListProps = {
	items: TaxonomyItem[];
	isLoading: boolean;
	onEdit: (item: TaxonomyItem) => void;
	onDelete: (item: TaxonomyItem) => void;
	onToggleActive: (item: TaxonomyItem) => void;
	onReorder?: (items: TaxonomyItem[]) => void;
	columns: Array<{
		key: string;
		label: string;
		render?: (item: TaxonomyItem) => React.ReactNode;
	}>;
};
 
export function TaxonomyList({
	items,
	isLoading,
	onEdit,
	onDelete,
	onToggleActive,
	onReorder,
	columns,
}: TaxonomyListProps) {
	const [draggedIndex, setDraggedIndex] = useState<number | null>(null);
	const [dragOverIndex, setDragOverIndex] = useState<number | null>(null);
 
	const handleDragStart = (index: number) => {
		setDraggedIndex(index);
	};
 
	const handleDragOver = (e: React.DragEvent, index: number) => {
		e.preventDefault();
		setDragOverIndex(index);
	};
 
	const handleDrop = (e: React.DragEvent, dropIndex: number) => {
		e.preventDefault();
		if (draggedIndex === null || draggedIndex === dropIndex || !onReorder) {
			setDraggedIndex(null);
			setDragOverIndex(null);
			return;
		}
 
		const newItems = [...items];
		const draggedItem = newItems[draggedIndex];
		newItems.splice(draggedIndex, 1);
		newItems.splice(dropIndex, 0, draggedItem);
 
		onReorder(newItems);
		setDraggedIndex(null);
		setDragOverIndex(null);
	};
 
	const handleDragEnd = () => {
		setDraggedIndex(null);
		setDragOverIndex(null);
	};
 
	if (isLoading) {
		return (
			<div className="space-y-px">
				{[1, 2, 3, 4, 5].map((i) => (
					<div key={i} className="h-12 animate-pulse bg-background-muted" />
				))}
			</div>
		);
	}
 
	if (items.length === 0) {
		return (
			<p className="py-12 text-center text-sm text-foreground-muted">
				No items found. Create your first item to get started.
			</p>
		);
	}
 
	return (
		<Table density="dense">
			<TableCaption>Taxonomy items, with whether each is active.</TableCaption>
			<TableHeader>
				<TableRow>
					{onReorder && <TableHead className="w-10"></TableHead>}
					{columns.map((col) => (
						<TableHead key={col.key}>{col.label}</TableHead>
					))}
					<TableHead>Status</TableHead>
					<TableHead>Actions</TableHead>
				</TableRow>
			</TableHeader>
			<TableBody>
				{items.map((item, index) => (
					<TableRow
						key={item.id}
						draggable={!!onReorder}
						onDragStart={() => handleDragStart(index)}
						onDragOver={(e) => handleDragOver(e, index)}
						onDrop={(e) => handleDrop(e, index)}
						onDragEnd={handleDragEnd}
						// `TableRow` already rules and tints. Two drag states on top
						// of it: the row being carried gets the −3px `.lift`, which
						// is the ONE place the Job Book allows a lift ("this is
						// pick-up-able"); the row it would land on gets a strong rule
						// along its top edge, which is where the insertion happens.
						className={`${
							dragOverIndex === index
								? "shadow-[inset_0_3px_0_var(--border-strong)]"
								: ""
						} ${
							draggedIndex === index
								? "-translate-y-[3px] opacity-50 shadow-card"
								: ""
						}`}
					>
						{onReorder && (
							<TableCell className="cursor-move">
								<GripVertical className="h-5 w-5 text-foreground-subtle" />
							</TableCell>
						)}
						{columns.map((col) => (
							<TableCell key={col.key} className="text-foreground-default">
								{col.render
									? col.render(item)
									: String((item as Record<string, unknown>)[col.key] ?? "-")}
							</TableCell>
						))}
						<TableCell>
							<StatusBadge
								status={item.isActive ? "Active" : "Inactive"}
								variant={item.isActive ? "success" : "default"}
							/>
						</TableCell>
						<TableCell>
							<div className="flex items-center gap-2">
								<Button
									variant="ghost"
									size="sm"
									onClick={() => onEdit(item)}
									title="Edit"
								>
									<Edit2 className="h-4 w-4" />
								</Button>
								<Button
									variant="ghost"
									size="sm"
									onClick={() => onToggleActive(item)}
									title={item.isActive ? "Deactivate" : "Activate"}
								>
									{item.isActive ? (
										<EyeOff className="h-4 w-4 text-warning" />
									) : (
										<Eye className="h-4 w-4 text-success" />
									)}
								</Button>
								<Button
									variant="ghost"
									size="sm"
									onClick={() => onDelete(item)}
									title="Delete"
								>
									<Trash2 className="h-4 w-4 text-error" />
								</Button>
							</div>
						</TableCell>
					</TableRow>
				))}
			</TableBody>
		</Table>
	);
}