All files / src/pages/admin imports.tsx

89.18% Statements 33/37
85.71% Branches 36/42
100% Functions 10/10
100% Lines 29/29

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 209 210 211 212 213 214 215                                                                1x                 10x 10x 10x 10x 9x 9x   8x       9x 9x 9x   9x 10x     9x   9x 1x 2x   9x 1x 1x 1x 1x   9x 1x 1x 1x     9x                                                                                                                                                           10x               1x                                                                                                    
import { Link } from "@tanstack/react-router";
import { useState } from "react";
import { Plus } from "lucide-react";
 
import { AdminBand, AdminPageHead } from "../../components/admin/AdminPageHead";
import { Alert } from "../../components/ui/alert";
import { Button } from "../../components/ui/button";
import { Card } from "../../components/ui/card";
import { StatusBadge } from "../../components/ui/status-badge";
import type { StatusTone } from "../../components/ui/status-ink";
import { formatDate } from "../../lib/utils";
import {
	useAdminImports,
	useBulkApproveImports,
} from "../../hooks/queries/useAdminImports";
import { Checkbox } from "../../components/ui/checkbox";
import {
	Table,
	TableBody,
	TableCaption,
	TableCell,
	TableHead,
	TableHeader,
	TableRow,
} from "../../components/ui/table";
 
/**
 * An import's state is a printed code. The tone is all this page decides —
 * `StatusBadge` owns the shape (2px radius, `.label` caps) and the ink/tint
 * pair, so a status here cannot drift from a status anywhere else, and the
 * WCAG AA argument stays in `status-ink.ts`.
 */
const STATUS_TONES: Record<string, StatusTone> = {
	queued: "default",
	running: "info",
	completed: "success",
	failed: "error",
	cancelled: "default",
};
 
function formatRelativeTime(ts: number): string {
	const diff = Date.now() - ts;
	const minutes = Math.round(diff / 60_000);
	Iif (minutes < 1) return "just now";
	if (minutes < 60) return `${minutes}m ago`;
	const hours = Math.round(minutes / 60);
	if (hours < 24) return `${hours}h ago`;
	// `en-IN` from one place once the relative window runs out.
	return formatDate(new Date(ts));
}
 
export function AdminImportsPage() {
	const { data: imports, isLoading, error } = useAdminImports();
	const [selected, setSelected] = useState<Set<string>>(new Set());
	const bulkApprove = useBulkApproveImports();
 
	const approvable = (imports ?? []).filter(
		(r) => r.status === "completed" && !r.approvedAt,
	);
	const allChecked =
		approvable.length > 0 && approvable.every((r) => selected.has(r.id));
 
	const toggleAll = () => {
		Iif (allChecked) setSelected(new Set());
		else setSelected(new Set(approvable.map((r) => r.id)));
	};
	const toggleOne = (id: string) => {
		const next = new Set(selected);
		Iif (next.has(id)) next.delete(id);
		else next.add(id);
		setSelected(next);
	};
	const onBulkApprove = async () => {
		Iif (selected.size === 0) return;
		await bulkApprove.mutateAsync(Array.from(selected));
		setSelected(new Set());
	};
 
	return (
		<div className="space-y-6">
			<AdminPageHead
				title="Pro Imports"
				description="Scrape pro profiles and projects from a website or Houzz.in URL, then review and approve before publishing."
				actions={
					<Button asChild>
						<Link to="/admin/imports/new">
							<Plus aria-hidden="true" />
							New import
						</Link>
					</Button>
				}
			/>
 
			{isLoading && (
				<p className="text-xs font-semibold uppercase tracking-wider text-foreground-muted">
					Loading…
				</p>
			)}
			{error && (
				<Alert variant="error">
					Failed to load imports: {(error as Error).message}
				</Alert>
			)}
 
			{imports && imports.length === 0 && (
				<p className="text-sm text-foreground-muted">
					No imports yet. Click "New import" to start scraping a pro.
				</p>
			)}
 
			{imports && imports.length > 0 && (
				<section className="space-y-2">
					<AdminBand title="Imports" count={imports.length} quiet />
					<Card className="overflow-hidden">
						{selected.size > 0 && (
							// The selection strip is ruled, not boxed, and the figure is
							// tabular — it is a count, so it reads as one.
							<div className="flex items-center justify-between gap-3 border-b border-border-strong bg-background-muted px-4 py-2">
								<span className="text-xs font-semibold uppercase tracking-wider tabular-nums text-foreground-default">
									{selected.size} import(s) selected
								</span>
								<Button
									size="sm"
									onClick={onBulkApprove}
									disabled={bulkApprove.isPending}
								>
									{bulkApprove.isPending
										? "Approving…"
										: `Bulk approve (${selected.size})`}
								</Button>
							</div>
						)}
						<Table density="dense">
							<TableCaption>
								Import jobs, with their status and approval state.
							</TableCaption>
							<TableHeader>
								<TableRow>
									<TableHead>
										<Checkbox
											aria-label="Select all approvable imports"
											checked={allChecked}
											disabled={approvable.length === 0}
											onChange={toggleAll}
										/>
									</TableHead>
									<TableHead>Status</TableHead>
									<TableHead>Pro</TableHead>
									<TableHead>Source URL</TableHead>
									<TableHead>Projects / Photos</TableHead>
									<TableHead>Created</TableHead>
									<TableHead></TableHead>
								</TableRow>
							</TableHeader>
							<TableBody>
								{imports.map((row) => (
									<TableRow key={row.id}>
										<TableCell>
											<Checkbox
												aria-label={`Select import ${row.id}`}
												checked={selected.has(row.id)}
												disabled={
													row.status !== "completed" || !!row.approvedAt
												}
												onChange={() => toggleOne(row.id)}
											/>
										</TableCell>
										<TableCell>
											<StatusBadge
												status={row.status}
												variant={STATUS_TONES[row.status] ?? "default"}
											/>
										</TableCell>
										<TableCell className="text-sm text-foreground-default">
											{row.proBusinessName ?? (
												<span className="font-mono text-foreground-muted">
													{row.proId}
												</span>
											)}
										</TableCell>
										<TableCell className="text-sm text-foreground-muted max-w-xs truncate">
											{row.sourceUrl}
										</TableCell>
										<TableCell numeric className="text-foreground-muted">
											{row.statsJson?.projectsFound ?? "—"} /{" "}
											{row.statsJson?.photosDownloaded ?? "—"}
										</TableCell>
										<TableCell
											numeric
											className="whitespace-nowrap text-foreground-muted"
										>
											{formatRelativeTime(row.createdAt)}
										</TableCell>
										<TableCell className="text-right">
											<Link
												to="/admin/imports/$importId"
												params={{ importId: row.id }}
												className="text-primary-600 hover:text-primary-700 text-sm font-medium"
											>
												View
											</Link>
										</TableCell>
									</TableRow>
								))}
							</TableBody>
						</Table>
					</Card>
				</section>
			)}
		</div>
	);
}
 
export default AdminImportsPage;