All files / src/pages/admin feedback.tsx

100% Statements 35/35
100% Branches 51/51
100% Functions 14/14
100% Lines 34/34

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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388                                                                        1x           337x         337x                   337x                 337x 8x                       329x                     9x                                                                                                                                                           337x   337x         337x       10x                                                       4x 4x                                           2x     16x                         64x 64x 64x   64x           64x   64x 64x 64x     4x 4x 3x       1x       64x                                     3x 3x                           1x 1x                                                                                                     327x                                                        
import { useState } from "react";
import { useQueryClient } from "@tanstack/react-query";
import { MessageSquareText, ChevronDown, ChevronRight } from "lucide-react";
 
import { Card } from "../../components/ui/card";
import { Button } from "../../components/ui/button";
import { Pagination } from "../../components/ui/pagination";
import { EmptyState } from "../../components/ui/empty-state";
import { STATUS_CODE_TYPE } from "../../components/ui/status-ink";
import { cn } from "../../lib/utils";
import { adminApi, type FeedbackEntry } from "../../lib/api";
import { queryKeys } from "../../lib/query-keys";
import {
	useAdminFeedback,
	type FeedbackFilters,
} from "../../hooks/queries/useAdminQueries";
import { Select } from "../../components/ui/select";
import {
	Table,
	TableBody,
	TableCaption,
	TableCell,
	TableHead,
	TableHeader,
	TableRow,
} from "../../components/ui/table";
import { StatusBadge } from "../../components/ui/status-badge";
import type { StatusTone } from "../../components/ui/status-ink";
import { Alert } from "../../components/ui/alert";
import {
	AdminBand,
	AdminPageHead,
	AdminToolbar,
} from "../../components/admin/AdminPageHead";
import { formatDateTime } from "../../lib/utils";
 
const PAGE_SIZE = 20;
 
// ─── Status badge ────────────────────────────────────────────────────────────
 
function FeedbackStatusCode({ status }: { status: string }) {
	// Status ink on its own `-light` tint, from `components/ui/status-ink.ts`.
	const tones: Record<string, StatusTone> = {
		new: "info",
		reviewed: "warning",
		resolved: "success",
	};
	return <StatusBadge status={status} variant={tones[status] ?? "default"} />;
}
 
// ─── Category badge ──────────────────────────────────────────────────────────
 
function CategoryBadge({ category }: { category: string }) {
	// A category is not a status, so only two of these are status ink. Purple has
	// no Job Book equivalent: a feature request takes the field tint
	// (`primary-100`/`primary-900`, the mockup's action-tinted code) which stays
	// distinct from `bug` (error) and `general` (neutral) and inverts in dark.
	const labels: Record<string, string> = {
		general: "General",
		bug: "Bug",
		feature_request: "Feature Request",
	};
	// A category is not a status, so only two of these are status ink. A feature
	// request takes the field tint (`primary-100`/`primary-900`, the mockup's
	// action-tinted code), which stays distinct from `bug` (error) and `general`
	// (neutral) and inverts in dark.
	if (category === "feature_request") {
		return (
			<span
				className={cn(
					"inline-flex items-center rounded-sm px-1.5 py-0.5",
					STATUS_CODE_TYPE,
					"bg-primary-100 text-primary-900",
				)}
			>
				{labels.feature_request}
			</span>
		);
	}
	return (
		<StatusBadge
			status={labels[category] ?? category}
			variant={category === "bug" ? "error" : "default"}
		/>
	);
}
 
// ─── Row detail panel ────────────────────────────────────────────────────────
 
function RowDetail({ entry }: { entry: FeedbackEntry }) {
	return (
		<TableRow>
			<TableCell colSpan={7}>
				<div className="rounded-lg bg-background-subtle border border-border-default p-4 text-sm space-y-3">
					<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
						{entry.browser && (
							<div>
								<span className="text-foreground-muted font-medium">
									Browser:
								</span>{" "}
								<span className="text-foreground-default">{entry.browser}</span>
							</div>
						)}
						{entry.screenResolution && (
							<div>
								<span className="text-foreground-muted font-medium">
									Resolution:
								</span>{" "}
								<span className="text-foreground-default">
									{entry.screenResolution}
								</span>
							</div>
						)}
						{entry.location && (
							<div>
								<span className="text-foreground-muted font-medium">
									Timezone:
								</span>{" "}
								<span className="text-foreground-default">
									{entry.location}
								</span>
							</div>
						)}
						{entry.userId && (
							<div>
								<span className="text-foreground-muted font-medium">
									User ID:
								</span>{" "}
								<span className="font-mono text-xs text-foreground-default">
									{entry.userId}
								</span>
							</div>
						)}
						{entry.proId && (
							<div>
								<span className="text-foreground-muted font-medium">
									Pro ID:
								</span>{" "}
								<span className="font-mono text-xs text-foreground-default">
									{entry.proId}
								</span>
							</div>
						)}
					</div>
 
					<div>
						<p className="text-foreground-muted font-medium mb-1">
							Full Message:
						</p>
						<p className="text-foreground-default whitespace-pre-wrap text-xs bg-background-base rounded p-2">
							{entry.message}
						</p>
					</div>
				</div>
			</TableCell>
		</TableRow>
	);
}
 
// ─── Table row ───────────────────────────────────────────────────────────────
 
function FeedbackRow({
	entry,
	onStatusChange,
}: {
	entry: FeedbackEntry;
	onStatusChange: (id: number, status: string) => void;
}) {
	const [expanded, setExpanded] = useState(false);
 
	const nextStatus: Record<string, string> = {
		new: "reviewed",
		reviewed: "resolved",
	};
 
	return (
		<>
			<TableRow
				className="cursor-pointer"
				onClick={() => setExpanded((v) => !v)}
			>
				{/* `en-IN` from one place. */}
				<TableCell className="whitespace-nowrap text-xs tabular-nums text-foreground-muted">
					{formatDateTime(entry.createdAt)}
				</TableCell>
				<TableCell>
					<CategoryBadge category={entry.category} />
				</TableCell>
				<TableCell className="text-sm text-foreground-default max-w-xs truncate">
					{entry.message}
				</TableCell>
				<TableCell className="text-sm text-foreground-muted font-mono text-xs">
					{entry.userEmail ?? "—"}
				</TableCell>
				<TableCell>
					<FeedbackStatusCode status={entry.status} />
				</TableCell>
				<TableCell className="text-sm text-foreground-muted max-w-50 truncate">
					{entry.pageUrl ?? "—"}
				</TableCell>
				<TableCell>
					<div className="flex items-center gap-2">
						{nextStatus[entry.status] && (
							<Button
								variant="outline"
								size="sm"
								onClick={(e) => {
									e.stopPropagation();
									onStatusChange(entry.id, nextStatus[entry.status]);
								}}
							>
								Mark {nextStatus[entry.status]}
							</Button>
						)}
						{expanded ? (
							<ChevronDown className="h-4 w-4 text-foreground-subtle" />
						) : (
							<ChevronRight className="h-4 w-4 text-foreground-subtle" />
						)}
					</div>
				</TableCell>
			</TableRow>
			{expanded && <RowDetail entry={entry} />}
		</>
	);
}
 
// ─── Skeleton rows ───────────────────────────────────────────────────────────
 
function SkeletonRows() {
	return (
		<>
			{[1, 2, 3, 4, 5, 6, 7, 8].map((i) => (
				<TableRow key={i}>
					<TableCell colSpan={7}>
						<div className="h-5 bg-background-muted rounded animate-pulse" />
					</TableCell>
				</TableRow>
			))}
		</>
	);
}
 
// ─── Main page ───────────────────────────────────────────────────────────────
 
export function AdminFeedbackPage() {
	const queryClient = useQueryClient();
	const [statusFilter, setStatusFilter] = useState("");
	const [page, setPage] = useState(1);
 
	const filters: FeedbackFilters = {
		page,
		limit: PAGE_SIZE,
		status: statusFilter || undefined,
	};
 
	const { data, isLoading, error } = useAdminFeedback(filters);
 
	const items: FeedbackEntry[] = data?.data ?? [];
	const total = data?.meta?.total ?? 0;
	const totalPages = data?.meta?.totalPages ?? 1;
 
	async function handleStatusChange(id: number, newStatus: string) {
		try {
			await adminApi.updateFeedbackStatus(id, newStatus);
			queryClient.invalidateQueries({
				queryKey: queryKeys.admin.feedback.all,
			});
		} catch (err) {
			console.error("Failed to update feedback status:", err);
		}
	}
 
	return (
		<div className="space-y-6">
			<AdminPageHead
				title="Feedback"
				description="View and manage user feedback submissions."
				actions={
					isLoading ? (
						<span className="text-xs font-semibold uppercase tracking-wider text-foreground-muted">
							Loading...
						</span>
					) : undefined
				}
			/>
 
			<AdminToolbar aria-label="Feedback filters">
				<Select
					aria-label="Filter by status"
					value={statusFilter}
					onChange={(e) => {
						setStatusFilter(e.target.value);
						setPage(1);
					}}
					className="sm:w-48"
				>
					<option value="">All Statuses</option>
					<option value="new">New</option>
					<option value="reviewed">Reviewed</option>
					<option value="resolved">Resolved</option>
				</Select>
 
				{statusFilter && (
					<Button
						variant="outline"
						onClick={() => {
							setStatusFilter("");
							setPage(1);
						}}
					>
						Clear
					</Button>
				)}
			</AdminToolbar>
 
			<section className="space-y-2">
				<AdminBand
					title="Feedback entries"
					count={isLoading ? undefined : total}
					quiet
				/>
 
				{error ? (
					<Alert variant="error">
						Failed to load feedback. Please refresh the page.
					</Alert>
				) : (
					<Card className="overflow-hidden">
						<Table density="dense">
							<TableCaption>
								Feedback entries, newest first. Expand a row for its detail.
							</TableCaption>
							<TableHeader>
								<TableRow>
									<TableHead>Date</TableHead>
									<TableHead>Category</TableHead>
									<TableHead>Message</TableHead>
									<TableHead>Email</TableHead>
									<TableHead>Status</TableHead>
									<TableHead>Page URL</TableHead>
									<TableHead className="w-8" />
								</TableRow>
							</TableHeader>
							<TableBody>
								{isLoading ? (
									<SkeletonRows />
								) : items.length === 0 ? (
									<TableRow>
										<TableCell colSpan={7}>
											<EmptyState
												icon={MessageSquareText}
												title="No feedback found"
												description="Try adjusting your filters or check back later."
											/>
										</TableCell>
									</TableRow>
								) : (
									items.map((entry) => (
										<FeedbackRow
											key={entry.id}
											entry={entry}
											onStatusChange={handleStatusChange}
										/>
									))
								)}
							</TableBody>
						</Table>
 
						{/* Pagination — the primitive owns the status line and the pager. */}
						{!isLoading && total > PAGE_SIZE && (
							<Pagination
								className="border-t border-border-default px-4 py-3"
								density="dense"
								label="Feedback pages"
								page={page}
								totalPages={totalPages}
								total={total}
								onPageChange={setPage}
							/>
						)}
					</Card>
				)}
			</section>
		</div>
	);
}