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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | 152x 152x 152x 152x 152x 152x 152x 152x 152x 152x 55x 55x 2x 1x 55x 55x 152x 152x 3x 3x 2x 1x 152x 4x 4x 2x 2x 1x 1x 3x 1x 152x 152x 7x 14x 11x 6x 1x 405x 2x 1x 141x 2x | import { useState, useCallback } from "react";
import { useQueryClient } from "@tanstack/react-query";
import { useSearch } from "@tanstack/react-router";
import { Search, Plus, Store } 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 { Input } from "../../components/ui/input";
import {
CreateProModal,
ProTableRow,
} from "../../components/admin/pros";
import { adminApi, type Pro } from "../../lib/api";
import { queryKeys } from "../../lib/query-keys";
import { useAdminProsList } from "../../hooks/queries/useAdminQueries";
import { useSortable } from "../../hooks/useSortable";
import { SortableHeader } from "../../components/ui/sortable-header";
export function AdminProsPage() {
const queryClient = useQueryClient();
// Issue #613: dashboard "Pros pending publish" tile deep-links here with
// ?status=draft so admins land directly on the review queue. Read it once
// at mount via strict:false (route doesn't declare a typed search schema).
const searchParams = useSearch({ strict: false }) as { status?: string };
const [search, setSearch] = useState("");
const [statusFilter, setStatusFilter] = useState<string>(
searchParams.status ?? "",
);
// Issue #593: hide partially-created onboarding records by default so the
// admin list isn't cluttered with rows that have no business name.
const [showIncomplete, setShowIncomplete] = useState(false);
const [showNewPro, setShowNewPro] = useState(false);
const {
data,
isLoading,
isFetchingNextPage: isLoadingMore,
hasNextPage: hasMore,
fetchNextPage,
} = useAdminProsList({
status: statusFilter,
search,
includeIncomplete: showIncomplete,
});
const pros = data?.pages?.flatMap((p) => p.data || []) ?? [];
const total = data?.pages?.[0]?.meta?.total ?? 0;
const loadMoreRef = useCallback(
(node: HTMLDivElement | null) => {
Iif (!node) return;
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting && hasMore && !isLoadingMore) {
fetchNextPage();
}
},
{ threshold: 0.1 },
);
observer.observe(node);
return () => observer.disconnect();
},
[hasMore, isLoadingMore, fetchNextPage],
);
const { sorted: sortedPros, sortKey, sortOrder, toggleSort } = useSortable(
pros,
"businessName" as keyof Pro,
"asc",
);
const toggleFeatured = async (pro: Pro) => {
try {
await adminApi.setProFeatured(pro.id, !pro.isFeatured);
queryClient.invalidateQueries({ queryKey: queryKeys.admin.pros.all });
} catch (err) {
console.error("Failed to toggle featured:", err);
}
};
const updateStatus = async (
pro: Pro,
status: "draft" | "published" | "archived",
) => {
try {
if (status === "published") {
await adminApi.enablePro(pro.id);
} else if (status === "archived") {
await adminApi.disablePro(pro.id);
} else {
await adminApi.updatePro(pro.id, { status });
}
queryClient.invalidateQueries({ queryKey: queryKeys.admin.pros.all });
} catch (err) {
console.error("Failed to update status:", err);
}
};
const handleSearch = () => {
// Filters are reactive via useAdminProsList — search state change triggers refetch
};
return (
<div className="space-y-6">
{/* Header */}
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
<div>
<h1 className="text-2xl font-bold text-foreground-default">
Pros
</h1>
<p className="text-foreground-muted">
Manage all pros on the platform.
</p>
</div>
<Button onClick={() => setShowNewPro(true)}>
<Plus className="h-4 w-4 mr-2" />
Add Pro
</Button>
</div>
{/* Filters */}
<Card>
<CardContent className="pt-6">
<div className="flex flex-col sm:flex-row gap-4">
<div className="relative flex-1">
<Search className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-foreground-subtle" />
<Input
placeholder="Search pros..."
value={search}
onChange={(e) => setSearch(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && handleSearch()}
className="pl-10"
/>
</div>
<select
value={statusFilter}
onChange={(e) => setStatusFilter(e.target.value)}
className="h-10 rounded-md border border-border-default bg-background-elevated px-3 text-sm focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
>
<option value="">All publish statuses</option>
<option value="draft">Draft (waiting for approval)</option>
<option value="published">Published</option>
<option value="archived">Archived</option>
</select>
<Button onClick={handleSearch}>Search</Button>
</div>
<p className="mt-3 text-xs text-foreground-muted">
To approve a pro's website, find their row below and change
their <strong>Publish status</strong> from{" "}
<em>Draft</em> to <em>Published</em>.
</p>
<label className="mt-4 flex items-center gap-2 text-sm text-foreground-muted cursor-pointer select-none">
<input
type="checkbox"
checked={showIncomplete}
onChange={(e) => setShowIncomplete(e.target.checked)}
className="h-4 w-4 rounded border-border-default text-primary-600 focus:ring-primary-500/40"
/>
Show incomplete profiles (no name set)
</label>
</CardContent>
</Card>
{/* Table */}
<Card>
<CardHeader>
<CardTitle className="text-lg">{total} Pros</CardTitle>
</CardHeader>
<CardContent>
{isLoading ? (
<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>
) : pros.length === 0 ? (
<EmptyState
icon={Store}
title="No pros found"
description="Try adjusting your search or filters."
/>
) : (
<div className="overflow-x-auto -mx-6 px-6">
<table className="w-full min-w-[700px]">
<thead>
<tr className="border-b text-left text-sm text-foreground-muted">
<SortableHeader
label="Business Name"
sortKey="businessName"
currentSortKey={sortKey as string}
sortOrder={sortOrder}
onSort={(key) => toggleSort(key as keyof Pro)}
/>
<th className="pb-3 font-medium">Location</th>
<SortableHeader
label="Publish status"
sortKey="status"
currentSortKey={sortKey as string}
sortOrder={sortOrder}
onSort={(key) => toggleSort(key as keyof Pro)}
/>
<th className="pb-3 font-medium">Featured</th>
<th className="pb-3 font-medium">Actions</th>
</tr>
</thead>
<tbody>
{sortedPros.map((pro) => (
<ProTableRow
key={pro.id}
pro={pro}
onToggleFeatured={toggleFeatured}
onUpdateStatus={updateStatus}
/>
))}
</tbody>
</table>
{/* Infinite scroll trigger */}
<div
ref={loadMoreRef}
className="h-10 flex items-center justify-center"
>
{isLoadingMore && (
<div className="animate-spin rounded-full h-6 w-6 border-b-2 border-primary-600" />
)}
{!hasMore && pros.length > 0 && (
<p className="text-sm text-foreground-muted">
No more pros
</p>
)}
</div>
</div>
)}
</CardContent>
</Card>
<CreateProModal
isOpen={showNewPro}
onClose={() => setShowNewPro(false)}
/>
</div>
);
}
|