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                                                        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";
 
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-4">
				{[1, 2, 3, 4, 5].map((i) => (
					<div
						key={i}
						className="h-16 bg-background-muted rounded animate-pulse"
					/>
				))}
			</div>
		);
	}
 
	if (items.length === 0) {
		return (
			<div className="text-center py-12 text-foreground-muted">
				No items found. Create your first item to get started.
			</div>
		);
	}
 
	return (
		<div className="overflow-x-auto">
			<table className="w-full">
				<thead>
					<tr className="border-b border-border-default text-left text-sm text-foreground-muted">
						{onReorder && <th className="pb-3 font-medium w-10"></th>}
						{columns.map((col) => (
							<th key={col.key} className="pb-3 font-medium">
								{col.label}
							</th>
						))}
						<th className="pb-3 font-medium">Status</th>
						<th className="pb-3 font-medium">Actions</th>
					</tr>
				</thead>
				<tbody>
					{items.map((item, index) => (
						<tr
							key={item.id}
							draggable={!!onReorder}
							onDragStart={() => handleDragStart(index)}
							onDragOver={(e) => handleDragOver(e, index)}
							onDrop={(e) => handleDrop(e, index)}
							onDragEnd={handleDragEnd}
							className={`border-b border-border-default last:border-0 hover:bg-background-muted ${
								dragOverIndex === index ? "bg-primary-50" : ""
							} ${draggedIndex === index ? "opacity-50" : ""}`}
						>
							{onReorder && (
								<td className="py-4 cursor-move">
									<GripVertical className="h-5 w-5 text-foreground-subtle" />
								</td>
							)}
							{columns.map((col) => (
								<td key={col.key} className="py-4 text-foreground-default">
									{col.render
										? col.render(item)
										: String((item as Record<string, unknown>)[col.key] ?? "-")}
								</td>
							))}
							<td className="py-4">
								<span
									className={`inline-block px-2 py-1 text-xs font-medium rounded-full ${
										item.isActive
											? "bg-success-light text-success"
											: "bg-background-muted text-foreground-muted"
									}`}
								>
									{item.isActive ? "Active" : "Inactive"}
								</span>
							</td>
							<td className="py-4">
								<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>
							</td>
						</tr>
					))}
				</tbody>
			</table>
		</div>
	);
}