From dc2bab489f5042f6c53e38a6b8dd1a0e84587509 Mon Sep 17 00:00:00 2001 From: B SIMAK AHMED <1NZ24CS030@newhorizonindia.edu> Date: Tue, 22 Sep 2026 21:22:16 +0530 Subject: [PATCH 1/2] fix: connect admin audit activity to durable audit log --- src/routes/_authenticated/admin.tsx | 60 ++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/src/routes/_authenticated/admin.tsx b/src/routes/_authenticated/admin.tsx index 3964c69..94f6d10 100644 --- a/src/routes/_authenticated/admin.tsx +++ b/src/routes/_authenticated/admin.tsx @@ -41,6 +41,7 @@ import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { PostEditor, type PostDraft } from "@/components/PostEditor"; import { SecurityTab } from "@/components/SecurityTab"; +import { getAdminAuditEntries, recordAdminAudit } from "@/lib/admin-audit"; import { useAuth } from "@/hooks/use-auth"; import { toast } from "sonner"; @@ -102,7 +103,7 @@ function Admin() { const media = useQuery({ queryKey: ["admin-media"], queryFn: async () => { const { data, error } = await supabase.storage.from("site-media").list("", { limit: 100, sortBy: { column: "updated_at", order: "desc" } }); if (error) throw error; return (data ?? []) as MediaFile[]; } }); const refreshAll = () => { void Promise.all([messages.refetch(), posts.refetch(), projects.refetch(), education.refetch(), experience.refetch(), comments.refetch(), settings.refetch(), media.refetch()]); }; const stats: AdminStats = useMemo(() => ({ posts: posts.data?.length ?? 0, published: posts.data?.filter((p) => p.published).length ?? 0, projects: projects.data?.length ?? 0, messages: messages.data?.length ?? 0, unread: messages.data?.filter((m) => m.status === "unread").length ?? 0, comments: comments.data?.length ?? 0, pendingComments: comments.data?.filter((c) => !c.approved).length ?? 0, views: posts.data?.reduce((sum, p) => sum + (p.views ?? 0), 0) ?? 0 }), [posts.data, projects.data, messages.data, comments.data]); - const recordActivity = async (action: string, resource: string, resourceId?: string) => { if (!user) return; console.debug("CodeForge admin activity", { action, resource, resourceId, userId: user.id }); }; + const recordActivity = async (action: string, resource: string, resourceId?: string) => { if (!user) return; await recordAdminAudit(action, resource, resourceId); }; const selectTab = (value: string) => setTab(value as AdminTab); return
@@ -120,15 +121,15 @@ function Admin() { {tab === "settings" && } {tab === "media" && } {tab === "security" && } - {tab === "activity" && } + {tab === "activity" && } {tab === "analytics" && } - {tab === "comments" && } + {tab === "comments" && } {tab === "engagement" && } {tab === "bookmarks" && } {tab === "search" && } {tab === "health" && } {tab === "export" && } - {tab === "trash" && } + {tab === "trash" && } {tab === "profile" && }
; @@ -152,7 +153,7 @@ function Messages({ messages, refetch, onActivity }: { messages: ContactMessage[ function Posts({ posts, refetch, onActivity }: { posts: Array>; refetch: () => Promise; onActivity: (a: string, r: string, id?: string) => Promise; }) { const [editing, setEditing] = useState(); const [creating, setCreating] = useState(false); const typedPosts = posts as Array; const remove = async (id: string) => { if (!window.confirm("Delete this post? This cannot be undone.")) return; const { error } = await supabase.from("posts").delete().eq("id", id); if (error) return toast.error(error.message); await onActivity("delete", "post", id); toast.success("Post deleted"); await refetch(); }; - if (creating || editing) return { setCreating(false); setEditing(undefined); void refetch(); }} />; + if (creating || editing) return { void onActivity(action, "post", id); setCreating(false); setEditing(undefined); void refetch(); }} />; return
{typedPosts.map((p) =>
{p.title}
{p.published ? "Published" : "Draft"} · {p.views} views · {new Date(p.created_at).toLocaleDateString()}
{p.published && }
)}{typedPosts.length === 0 && }
; } @@ -166,7 +167,7 @@ function Projects({ projects, refetch, onActivity }: { projects: Project[]; refe function TimelineManager({ title, table, items, refetch, onActivity }: { title: string; table: "education" | "experience"; items: TimelineItem[]; refetch: () => Promise; onActivity: (a: string, r: string, id?: string) => Promise; }) { const empty: TimelineItem = { id: "", year: "", title: "", org: "", description: "", order_index: 0 }; const [draft, setDraft] = useState(null); const save = async () => { if (!draft?.year.trim() || !draft.title.trim() || !draft.org.trim() || !draft.description.trim()) return toast.error("All fields are required"); const payload = { year: draft.year.trim(), title: draft.title.trim(), org: draft.org.trim(), description: draft.description.trim(), order_index: Number(draft.order_index) || 0 }; const result = draft.id ? await supabase.from(table).update(payload).eq("id", draft.id) : await supabase.from(table).insert(payload); if (result.error) return toast.error(result.error.message); await onActivity(draft.id ? "update" : "create", table, draft.id || undefined); toast.success(draft.id ? `${title} updated` : `${title} added`); setDraft(null); await refetch(); }; - const remove = async (id: string) => { if (!window.confirm(`Delete this ${title.toLowerCase()} entry?`)) return; const { error } = await supabase.from(table).delete().eq("id", id); if (error) return toast.error(error.message); toast.success("Entry deleted"); await refetch(); }; + const remove = async (id: string) => { if (!window.confirm(`Delete this ${title.toLowerCase()} entry?`)) return; const { error } = await supabase.from(table).delete().eq("id", id); if (error) return toast.error(error.message); await onActivity("delete", table, id); toast.success("Entry deleted"); await refetch(); }; return {draft ?
setDraft({ ...draft, year: e.target.value })} className="rounded-xl" /> setDraft({ ...draft, title: e.target.value })} className="rounded-xl" /> setDraft({ ...draft, org: e.target.value })} className="rounded-xl" /> setDraft({ ...draft, order_index: Number(e.target.value) })} className="rounded-xl" />