Migrate tickets and CFH lists to DataTable; confirm destructive mod actions.
Local Build and Deploy / deploy (push) Successful in 1m9s
Local Build and Deploy / deploy (push) Successful in 1m9s
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
cbdea70f83
commit
06610e9ae3
7 files changed
+622
-414
No files matched your search
+122
-221
@@ -1,250 +1,151 @@
|
||||
import {
|
||||
AlertCircle,
|
||||
CheckCircle2,
|
||||
Clock,
|
||||
Loader2,
|
||||
Ticket,
|
||||
} from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import type { Prisma } from "@/generated/prisma/client";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { TicketsTable, type TicketRow } from "./tickets-table";
|
||||
|
||||
const STATUS_CONFIG = {
|
||||
open: {
|
||||
label: "Open",
|
||||
icon: AlertCircle,
|
||||
class: "bg-[var(--admin-info-subtle)] text-[var(--admin-info)]",
|
||||
},
|
||||
in_progress: {
|
||||
label: "In Progress",
|
||||
icon: Loader2,
|
||||
class: "bg-[var(--admin-warning-subtle)] text-[var(--admin-warning)]",
|
||||
},
|
||||
waiting: {
|
||||
label: "Waiting",
|
||||
icon: Clock,
|
||||
class: "bg-[var(--admin-warning-subtle)] text-[var(--admin-warning)]",
|
||||
},
|
||||
closed: {
|
||||
label: "Closed",
|
||||
icon: CheckCircle2,
|
||||
class: "bg-muted text-muted-foreground",
|
||||
},
|
||||
} as const;
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const PRIORITY_COLORS = {
|
||||
low: "",
|
||||
normal: "text-[var(--admin-info)]",
|
||||
high: "text-[var(--admin-warning)]",
|
||||
urgent: "text-[var(--admin-error)] font-bold",
|
||||
};
|
||||
|
||||
interface PageProps {
|
||||
searchParams: Promise<{ status?: string; page?: string }>;
|
||||
function fromDate(d: Date | null | undefined): string {
|
||||
return d ? d.toISOString().slice(0, 10) : "—";
|
||||
}
|
||||
|
||||
export default async function AdminTicketsPage({ searchParams }: PageProps) {
|
||||
export default async function AdminTicketsPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<Record<string, string>>;
|
||||
}) {
|
||||
const { session, permissions } = await getAdminContext();
|
||||
if (!canAccess(permissions, PERMS.TICKETS_VIEW, session.user.rank)) {
|
||||
redirect("/admin");
|
||||
}
|
||||
|
||||
const sp = await searchParams;
|
||||
const statusFilter = sp.status || "active";
|
||||
const page = Math.max(1, Number(sp.page) || 1);
|
||||
const perPage = 20;
|
||||
const t = await getTranslations("pages.admin.tickets");
|
||||
const raw = await searchParams;
|
||||
const parsed = parseListParams(new URLSearchParams(raw));
|
||||
const statusFilter = raw.filter_status || raw.status || "active";
|
||||
|
||||
const where =
|
||||
statusFilter === "active"
|
||||
? { status: { not: "closed" } }
|
||||
: statusFilter === "all"
|
||||
? {}
|
||||
: { status: statusFilter };
|
||||
const conditions: Prisma.WebsiteTicketWhereInput[] = [];
|
||||
|
||||
const [tickets, total, stats] = await Promise.all([
|
||||
prisma.websiteTicket.findMany({
|
||||
if (statusFilter === "active") {
|
||||
conditions.push({ status: { not: "closed" } });
|
||||
} else if (statusFilter && statusFilter !== "all") {
|
||||
conditions.push({ status: statusFilter });
|
||||
}
|
||||
|
||||
if (parsed.search.trim()) {
|
||||
const q = parsed.search.trim();
|
||||
const asId = Number(q);
|
||||
conditions.push({
|
||||
OR: [
|
||||
{ subject: { contains: q } },
|
||||
{ category: { contains: q } },
|
||||
{ creator: { username: { contains: q } } },
|
||||
{ assignee: { username: { contains: q } } },
|
||||
...(Number.isFinite(asId) && asId > 0 ? [{ id: asId }] : []),
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
const where: Prisma.WebsiteTicketWhereInput =
|
||||
conditions.length === 0
|
||||
? {}
|
||||
: conditions.length === 1
|
||||
? conditions[0]!
|
||||
: { AND: conditions };
|
||||
|
||||
const SORT_MAP: Record<string, Prisma.WebsiteTicketOrderByWithRelationInput> =
|
||||
{
|
||||
subject: { subject: "asc" },
|
||||
status: { status: "asc" },
|
||||
priority: { priority: "asc" },
|
||||
assignee: { assigneeId: "asc" },
|
||||
date: { createdAt: "desc" },
|
||||
id: { id: "desc" },
|
||||
};
|
||||
|
||||
const baseOrder = SORT_MAP[parsed.sort ?? "date"] ?? { createdAt: "desc" };
|
||||
const orderField = Object.keys(baseOrder)[0] as keyof typeof baseOrder;
|
||||
const orderBy = {
|
||||
[orderField]: parsed.order,
|
||||
} as Prisma.WebsiteTicketOrderByWithRelationInput;
|
||||
|
||||
const [total, openCount, inProgressCount, waitingCount, closedCount] =
|
||||
await Promise.all([
|
||||
prisma.websiteTicket.count({ where }).catch(() => 0),
|
||||
prisma.websiteTicket.count({ where: { status: "open" } }).catch(() => 0),
|
||||
prisma.websiteTicket
|
||||
.count({ where: { status: "in_progress" } })
|
||||
.catch(() => 0),
|
||||
prisma.websiteTicket
|
||||
.count({ where: { status: "waiting" } })
|
||||
.catch(() => 0),
|
||||
prisma.websiteTicket.count({ where: { status: "closed" } }).catch(() => 0),
|
||||
]);
|
||||
|
||||
const pagination = calcPagination(total, parsed.page, parsed.perPage);
|
||||
|
||||
const tickets = await prisma.websiteTicket
|
||||
.findMany({
|
||||
where,
|
||||
orderBy: [{ createdAt: "desc" }],
|
||||
skip: (page - 1) * perPage,
|
||||
take: perPage,
|
||||
orderBy,
|
||||
skip: pagination.offset,
|
||||
take: pagination.perPage,
|
||||
include: {
|
||||
creator: { select: { id: true, username: true } },
|
||||
assignee: { select: { id: true, username: true } },
|
||||
_count: { select: { messages: true } },
|
||||
},
|
||||
}),
|
||||
prisma.websiteTicket.count({ where }),
|
||||
Promise.all([
|
||||
prisma.websiteTicket.count({ where: { status: "open" } }),
|
||||
prisma.websiteTicket.count({ where: { status: "in_progress" } }),
|
||||
prisma.websiteTicket.count({ where: { status: "waiting" } }),
|
||||
prisma.websiteTicket.count({ where: { status: "closed" } }),
|
||||
]),
|
||||
]);
|
||||
})
|
||||
.catch(() => []);
|
||||
|
||||
const [openCount, inProgressCount, waitingCount, closedCount] = stats;
|
||||
const totalPages = Math.ceil(total / perPage);
|
||||
const rows: TicketRow[] = tickets.map((ticket) => ({
|
||||
id: ticket.id,
|
||||
subject: ticket.subject,
|
||||
creator: ticket.creator.username,
|
||||
creatorId: ticket.creatorId,
|
||||
category: ticket.category,
|
||||
messages: ticket._count.messages,
|
||||
status: ticket.status,
|
||||
priority: ticket.priority,
|
||||
assignee: ticket.assignee?.username ?? "—",
|
||||
date: fromDate(ticket.createdAt),
|
||||
}));
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Stats */}
|
||||
<div className="grid gap-4 grid-cols-2 lg:grid-cols-4">
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-sm font-medium text-[var(--admin-info)]">
|
||||
Open
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{openCount}</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-sm font-medium text-[var(--admin-warning)]">
|
||||
In Progress
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{inProgressCount}</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-sm font-medium text-[var(--admin-warning)]">
|
||||
Waiting
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{waitingCount}</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-sm font-medium text-muted-foreground">
|
||||
Closed
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{closedCount}</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<div className="grid grid-cols-[repeat(auto-fit,minmax(180px,1fr))] gap-3.5">
|
||||
<StatusCard label={t("statusOpen")} value={openCount} icon="🎫" />
|
||||
<StatusCard
|
||||
label={t("statusInProgress")}
|
||||
value={inProgressCount}
|
||||
state={inProgressCount > 0 ? "warn" : "neutral"}
|
||||
icon="⏳"
|
||||
/>
|
||||
<StatusCard
|
||||
label={t("statusWaiting")}
|
||||
value={waitingCount}
|
||||
state={waitingCount > 0 ? "warn" : "neutral"}
|
||||
icon="🕒"
|
||||
/>
|
||||
<StatusCard label={t("statusClosed")} value={closedCount} icon="✅" />
|
||||
</div>
|
||||
|
||||
{/* Filter tabs */}
|
||||
<div className="flex gap-2">
|
||||
{[
|
||||
{ key: "active", label: "Active" },
|
||||
{ key: "open", label: "Open" },
|
||||
{ key: "in_progress", label: "In Progress" },
|
||||
{ key: "waiting", label: "Waiting" },
|
||||
{ key: "closed", label: "Closed" },
|
||||
{ key: "all", label: "All" },
|
||||
].map(({ key, label }) => (
|
||||
<Link key={key} href={`/admin/tickets?status=${key}`}>
|
||||
<Badge
|
||||
variant={statusFilter === key ? "default" : "outline"}
|
||||
className="cursor-pointer"
|
||||
>
|
||||
{label}
|
||||
</Badge>
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Ticket list */}
|
||||
<div className="rounded-lg border">
|
||||
<div className="grid grid-cols-[1fr_auto_auto_auto_auto] gap-4 p-3 bg-muted/50 text-xs font-medium text-muted-foreground border-b">
|
||||
<span>Ticket</span>
|
||||
<span>Status</span>
|
||||
<span>Priority</span>
|
||||
<span>Assigned</span>
|
||||
<span>Date</span>
|
||||
</div>
|
||||
|
||||
{tickets.length === 0 ? (
|
||||
<div className="p-8 text-center text-muted-foreground">
|
||||
<Ticket className="h-8 w-8 mx-auto mb-2" />
|
||||
No tickets found.
|
||||
</div>
|
||||
) : (
|
||||
tickets.map((ticket) => {
|
||||
const status =
|
||||
STATUS_CONFIG[ticket.status as keyof typeof STATUS_CONFIG] ??
|
||||
STATUS_CONFIG.open;
|
||||
const StatusIcon = status.icon;
|
||||
|
||||
return (
|
||||
<Link
|
||||
key={ticket.id}
|
||||
href={`/admin/tickets/${ticket.id}`}
|
||||
className="grid grid-cols-[1fr_auto_auto_auto_auto] gap-4 p-3 items-center hover:bg-accent/50 transition-all duration-150 border-b last:border-0 hover:pl-4"
|
||||
>
|
||||
<div className="min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-xs text-muted-foreground font-mono">
|
||||
#{ticket.id}
|
||||
</span>
|
||||
<span className="font-medium truncate">
|
||||
{ticket.subject}
|
||||
</span>
|
||||
</div>
|
||||
<div className="text-xs text-muted-foreground">
|
||||
by {ticket.creator.username} ·{" "}
|
||||
{ticket._count.messages} msg · {ticket.category}
|
||||
</div>
|
||||
</div>
|
||||
<span
|
||||
className={`inline-flex items-center gap-1 text-xs font-medium px-2 py-0.5 rounded-full whitespace-nowrap ${status.class}`}
|
||||
>
|
||||
<StatusIcon className="h-3 w-3" />
|
||||
{status.label}
|
||||
</span>
|
||||
<span
|
||||
className={`text-xs capitalize whitespace-nowrap ${PRIORITY_COLORS[ticket.priority as keyof typeof PRIORITY_COLORS] ?? ""}`}
|
||||
>
|
||||
{ticket.priority}
|
||||
</span>
|
||||
<span className="text-xs text-muted-foreground whitespace-nowrap">
|
||||
{ticket.assignee?.username ?? "—"}
|
||||
</span>
|
||||
<span className="text-xs text-muted-foreground whitespace-nowrap">
|
||||
{new Date(ticket.createdAt).toLocaleDateString()}
|
||||
</span>
|
||||
</Link>
|
||||
);
|
||||
})
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Pagination */}
|
||||
{totalPages > 1 && (
|
||||
<div className="flex items-center justify-center gap-2">
|
||||
{page > 1 && (
|
||||
<Link
|
||||
href={`/admin/tickets?status=${statusFilter}&page=${page - 1}`}
|
||||
>
|
||||
<Badge variant="outline" className="cursor-pointer">
|
||||
Previous
|
||||
</Badge>
|
||||
</Link>
|
||||
)}
|
||||
<span className="text-sm text-muted-foreground">
|
||||
Page {page} of {totalPages}
|
||||
</span>
|
||||
{page < totalPages && (
|
||||
<Link
|
||||
href={`/admin/tickets?status=${statusFilter}&page=${page + 1}`}
|
||||
>
|
||||
<Badge variant="outline" className="cursor-pointer">
|
||||
Next
|
||||
</Badge>
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
{total === 0 && !parsed.search && statusFilter === "active" ? (
|
||||
<div className="admin-empty">{t("noTickets")}</div>
|
||||
) : (
|
||||
<TicketsTable
|
||||
data={{
|
||||
rows,
|
||||
total,
|
||||
page: pagination.page,
|
||||
perPage: pagination.perPage,
|
||||
lastPage: pagination.lastPage,
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { DataTable } from "@/components/admin/data-table";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import type { DataTableColumn, PaginatedResult } from "@/types";
|
||||
|
||||
export interface TicketRow {
|
||||
id: number;
|
||||
subject: string;
|
||||
creator: string;
|
||||
creatorId: number;
|
||||
category: string;
|
||||
messages: number;
|
||||
status: string;
|
||||
priority: string;
|
||||
assignee: string;
|
||||
date: string;
|
||||
}
|
||||
|
||||
const STATUS_VARIANT: Record<
|
||||
string,
|
||||
"default" | "secondary" | "destructive" | "outline"
|
||||
> = {
|
||||
open: "default",
|
||||
in_progress: "secondary",
|
||||
waiting: "outline",
|
||||
closed: "secondary",
|
||||
};
|
||||
|
||||
export function TicketsTable({ data }: { data: PaginatedResult<TicketRow> }) {
|
||||
const t = useTranslations("pages.admin.tickets");
|
||||
|
||||
const statusLabel: Record<string, string> = {
|
||||
open: t("statusOpen"),
|
||||
in_progress: t("statusInProgress"),
|
||||
waiting: t("statusWaiting"),
|
||||
closed: t("statusClosed"),
|
||||
};
|
||||
|
||||
const columns: DataTableColumn<TicketRow>[] = [
|
||||
{
|
||||
key: "subject",
|
||||
label: t("colTicket"),
|
||||
sortable: true,
|
||||
render: (_value, row) => (
|
||||
<div className="min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-xs text-muted-foreground font-mono">
|
||||
#{row.id}
|
||||
</span>
|
||||
<Link
|
||||
href={`/admin/tickets/${row.id}`}
|
||||
className="font-medium truncate hover:underline"
|
||||
>
|
||||
{row.subject}
|
||||
</Link>
|
||||
</div>
|
||||
<div className="text-xs text-muted-foreground">
|
||||
{t("meta", {
|
||||
user: row.creator,
|
||||
count: row.messages,
|
||||
category: row.category,
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
key: "status",
|
||||
label: t("colStatus"),
|
||||
sortable: true,
|
||||
filterKey: "filter_status",
|
||||
filterOptions: [
|
||||
{ label: t("statusOpen"), value: "open" },
|
||||
{ label: t("statusInProgress"), value: "in_progress" },
|
||||
{ label: t("statusWaiting"), value: "waiting" },
|
||||
{ label: t("statusClosed"), value: "closed" },
|
||||
],
|
||||
render: (value) => {
|
||||
const status = String(value);
|
||||
return (
|
||||
<Badge
|
||||
variant={STATUS_VARIANT[status] ?? "secondary"}
|
||||
className="text-[0.7rem]"
|
||||
>
|
||||
{statusLabel[status] ?? status.replace("_", " ")}
|
||||
</Badge>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
key: "priority",
|
||||
label: t("colPriority"),
|
||||
sortable: true,
|
||||
render: (value) => {
|
||||
const p = String(value);
|
||||
const className =
|
||||
p === "urgent"
|
||||
? "text-[var(--admin-error)] font-bold"
|
||||
: p === "high"
|
||||
? "text-[var(--admin-warning)]"
|
||||
: p === "normal"
|
||||
? "text-[var(--admin-info)]"
|
||||
: "";
|
||||
return <span className={`text-xs capitalize ${className}`}>{p}</span>;
|
||||
},
|
||||
},
|
||||
{ key: "assignee", label: t("colAssigned"), sortable: true },
|
||||
{ key: "date", label: t("colDate"), sortable: true },
|
||||
];
|
||||
|
||||
return (
|
||||
<DataTable
|
||||
data={data}
|
||||
columns={columns}
|
||||
searchPlaceholder={t("searchPlaceholder")}
|
||||
presets={[
|
||||
{ label: t("presetActive"), params: { filter_status: "active" } },
|
||||
{ label: t("statusOpen"), params: { filter_status: "open" } },
|
||||
{
|
||||
label: t("statusInProgress"),
|
||||
params: { filter_status: "in_progress" },
|
||||
},
|
||||
{ label: t("statusWaiting"), params: { filter_status: "waiting" } },
|
||||
{ label: t("statusClosed"), params: { filter_status: "closed" } },
|
||||
]}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user