import { useState } from "react"; import { createFileRoute } from "@tanstack/react-router"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { trpc } from "@/utils/trpc"; import { Button } from "@pram/ui/components/button"; import { toast } from "sonner"; import { getErrorMessage } from "@/lib/error-utils"; import { DataTable } from "@/components/admin/DataTable"; import type { ColumnDef } from "@/components/admin/DataTable"; export const Route = createFileRoute("/admin/moderation")({ component: AdminModeration, }); type QuestionRow = { id: string; questionText: string; passageText: string | null; examTypeId: string; format: string; isPublic: boolean; source: string; createdAt: string | Date; }; function formatDate(dateStr: string | Date | null) { if (!dateStr) return "-"; return new Date(dateStr).toLocaleDateString("id-ID", { day: "numeric", month: "short", hour: "2-digit", minute: "2-digit" }); } function AdminModeration() { const [page, setPage] = useState(1); const limit = 30; const queryClient = useQueryClient(); const questions = useQuery( trpc.admin.listLatestQuestions.queryOptions({ limit, offset: (page - 1) * limit }), ); const toggleMutation = useMutation( trpc.admin.togglePublicAny.mutationOptions({ onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: trpc.admin.listLatestQuestions.queryKey() }); toast.success(data.isPublic ? "Made public" : "Made private"); }, onError: (e: unknown) => toast.error(getErrorMessage(e)), }), ); const columns: ColumnDef[] = [ { id: "question", header: "Question", size: "w-[35%]", cell: ({ row }) => (

{row.questionText}

{row.passageText && (

{row.passageText.slice(0, 80)}{row.passageText.length > 80 ? "..." : ""}

)}
), }, { id: "exam", header: "Exam", accessorKey: "examTypeId" }, { id: "format", header: "Format", cell: ({ value }) => {value as string}, }, { id: "visibility", header: "Visibility", cell: ({ row }) => ( {row.isPublic ? "Public" : "Private"} ), }, { id: "source", header: "Source", cell: ({ value }) => {value as string}, }, { id: "created", header: "Created", accessorFn: (q) => {formatDate(q.createdAt)}, }, ]; const questionData = questions.data?.questions ?? []; const hasMore = questionData.length >= limit; return (

Content Moderation

Review latest questions and manage visibility.

q.id} page={page} totalPages={page + 1} onPageChange={setPage} actions={(q) => ( )} /> {hasMore && (
Page {page}
)}
); }