All files / src/pages/admin room-categories.tsx

100% Statements 25/25
94.28% Branches 33/35
100% Functions 9/9
100% Lines 21/21

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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208                                  7x 8x 8x 8x   3x         16x 9x 6x       13x 4x           9x 3x           6x               9x   9x 13x   9x                                                           6x                                                                   13x                                 13x   13x                                                                                                                                
import { Link } from "@tanstack/react-router";
import { Camera } from "lucide-react";
 
import {
	Card,
	CardContent,
	CardHeader,
	CardTitle,
} from "../../components/ui/card";
import { Button } from "../../components/ui/button";
import { EmptyState } from "../../components/ui/empty-state";
import { getImageUrl } from "../../lib/api";
import type { RoomCategoryWithStats } from "../../lib/api";
import { useAdminRoomCategories } from "../../hooks/queries/useAdminQueries";
 
// D1B: sort order — needs-attention first (null cover + photoCount > 5), then curated, then empty
function sortCategories(categories: RoomCategoryWithStats[]): RoomCategoryWithStats[] {
	return [...categories].sort((a, b) => {
		const rankA = getSortRank(a);
		const rankB = getSortRank(b);
		if (rankA !== rankB) return rankA - rankB;
		// Within each group, sort by taxonomy sortOrder
		return a.sortOrder - b.sortOrder;
	});
}
 
function getSortRank(cat: RoomCategoryWithStats): number {
	if (cat.coverMediaId === null && cat.photoCount > 5) return 0; // needs attention
	if (cat.coverMediaId !== null) return 1;                        // curated
	return 2;                                                        // empty / few photos
}
 
function CoverSourceBadge({ source }: { source: RoomCategoryWithStats["coverSource"] }) {
	if (source === "curated") {
		return (
			<span className="text-xs font-semibold text-green-700">
				● Curated
			</span>
		);
	}
	if (source === "algorithmic") {
		return (
			<span className="text-xs text-amber-700">
				○ Algorithmic
			</span>
		);
	}
	return (
		<span className="text-xs text-foreground-subtle">
			— {source === "empty" ? "Empty" : "Pending"}
		</span>
	);
}
 
export function AdminRoomCategoriesPage() {
	const { data: rawCategories, isLoading, isError } = useAdminRoomCategories();
 
	const categories = rawCategories ? sortCategories(rawCategories) : [];
	const curatedCount = categories.filter((c) => c.coverSource === "curated").length;
 
	return (
		<div className="space-y-6">
			{/* Header */}
			<div>
				<div className="flex items-baseline gap-3">
					<h1 className="text-2xl font-bold text-foreground-default">
						Room categories
					</h1>
					{!isLoading && !isError && (
						<span className="text-sm text-foreground-subtle">
							{categories.length} categories · {curatedCount} with curated covers
						</span>
					)}
				</div>
				<p className="mt-1 text-sm text-foreground-muted max-w-xl">
					Pick the cover image shown for each room category on the marketplace
					homepage and rooms index. Categories without a curated cover fall back
					to algorithmic selection.
				</p>
			</div>
 
			{/* Table card */}
			<Card>
				<CardHeader>
					<CardTitle className="text-lg">All room categories</CardTitle>
				</CardHeader>
				<CardContent>
					{isLoading ? (
						<div className="space-y-3">
							{[1, 2, 3, 4, 5, 6].map((i) => (
								<div
									key={i}
									className="h-16 bg-background-muted rounded animate-pulse"
								/>
							))}
						</div>
					) : isError ? (
						<div className="rounded-md bg-error-light border border-error/20 p-4 text-sm text-error">
							Failed to load room categories. Please refresh to try again.
						</div>
					) : categories.length === 0 ? (
						<EmptyState
							icon={Camera}
							title="No room categories found"
							description="Room categories are seeded with the platform taxonomy."
						/>
					) : (
						<div className="overflow-x-auto -mx-6 px-6">
							<table className="w-full min-w-[600px]">
								<thead>
									<tr className="border-b text-left text-xs uppercase tracking-wider text-foreground-subtle">
										<th className="pb-3 font-semibold" style={{ width: 120 }}>Cover</th>
										<th className="pb-3 font-semibold">Category</th>
										<th className="pb-3 font-semibold hidden md:table-cell" style={{ width: 160 }}>
											Photos available
										</th>
										<th className="pb-3 font-semibold" style={{ width: 130 }}>Source</th>
										<th className="pb-3 font-semibold text-right" style={{ width: 130 }}>
											Action
										</th>
									</tr>
								</thead>
								<tbody>
									{categories.map((cat) => (
										<RoomCategoryListRow key={cat.code} category={cat} />
									))}
								</tbody>
							</table>
						</div>
					)}
				</CardContent>
			</Card>
		</div>
	);
}
 
type RoomCategoryListRowProps = {
	category: RoomCategoryWithStats;
};
 
function RoomCategoryListRow({ category }: RoomCategoryListRowProps) {
	const hasPhotos = category.photoCount > 0;
 
	return (
		<tr className="border-b last:border-0 hover:bg-background-muted/50 cursor-pointer">
			{/* Cover thumbnail */}
			<td className="py-4 pr-4">
				{category.coverMediaUrl ? (
					<img
						src={getImageUrl(category.coverMediaUrl)}
						alt={category.coverMediaAlt || category.label}
						className="w-[88px] h-16 rounded object-cover border border-border-default"
						loading="lazy"
					/>
				) : (
					<div
						className="w-[88px] h-16 rounded border border-border-default flex items-center justify-center"
						style={{
							background:
								"repeating-linear-gradient(45deg, #f4f3f1, #f4f3f1 8px, #fafaf9 8px, #fafaf9 16px)",
						}}
					>
						<Camera className="h-5 w-5 text-foreground-subtle" />
					</div>
				)}
			</td>
 
			{/* Category name + project count */}
			<td className="py-4">
				<div className="font-semibold text-foreground-default">{category.label}</div>
				<div className="text-sm text-foreground-muted mt-0.5">
					{category.projectCount} projects
				</div>
			</td>
 
			{/* Photos available (hidden on small screens) */}
			<td className="py-4 text-sm text-foreground-muted hidden md:table-cell">
				{category.photoCount > 0 ? `${category.photoCount} photos` : "—"}
			</td>
 
			{/* Source badge */}
			<td className="py-4">
				<CoverSourceBadge source={category.coverSource} />
			</td>
 
			{/* Action */}
			<td className="py-4 text-right">
				{hasPhotos ? (
					<Link to={`/admin/room-categories/${category.code}`}>
						<Button variant="outline" size="sm">
							{category.coverMediaId ? "Edit cover" : "Pick cover"}
						</Button>
					</Link>
				) : (
					<Button
						variant="ghost"
						size="sm"
						disabled
						title="No photos available for this category yet"
					>
						Pick cover
					</Button>
				)}
			</td>
		</tr>
	);
}