diff --git a/src/components/PostEditor.tsx b/src/components/PostEditor.tsx index d789b97..a4d91c5 100644 --- a/src/components/PostEditor.tsx +++ b/src/components/PostEditor.tsx @@ -29,7 +29,7 @@ function slugify(s: string) { return s.toLowerCase().trim().replace(/[^a-z0-9\s-]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-").slice(0, 80); } -export function PostEditor({ existing, onSaved }: { existing?: PostDraft; onSaved: () => void }) { +export function PostEditor({ existing, onSaved }: { existing?: PostDraft; onSaved: (postId: string, action: "create" | "update") => void }) { const { user } = useAuth(); const [draft, setDraft] = useState(existing ?? empty); const [tagsInput, setTagsInput] = useState((existing?.tags ?? []).join(", ")); @@ -47,12 +47,13 @@ export function PostEditor({ existing, onSaved }: { existing?: PostDraft; onSave const reading_time = Math.max(1, Math.round(draft.content.split(/\s+/).length / 200)); const payload = { ...draft, slug, tags, reading_time, published: publish ?? draft.published, author_id: user.id }; const res = draft.id - ? await supabase.from("posts").update(payload).eq("id", draft.id) - : await supabase.from("posts").insert(payload); + ? await supabase.from("posts").update(payload).eq("id", draft.id).select("id").single() + : await supabase.from("posts").insert(payload).select("id").single(); setBusy(false); if (res.error) return toast.error(res.error.message); toast.success(draft.id ? "Post updated" : "Post created"); - onSaved(); + const savedId = res.data?.id ?? draft.id; + if (savedId) onSaved(savedId, draft.id ? "update" : "create"); }; return ( 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" />