SPB Git

Repository Public MIT

actions.ts 1.1 KB 23 lines typescript

1"use server";2import type { ActionState } from "@/components/action-form";3import { getCtx } from "@/lib/session";4import { createReport } from "@/server/community";5import { ApiError } from "@/server/errors";6import { rateLimit } from "@/server/ratelimit";78export async function submitReport(_prev: ActionState, data: FormData): Promise<ActionState> {9  const ctx = await getCtx();10  try {11    if (String(data.get("website") ?? "") !== "") return { ok: "Thank you. The report was recorded." }; // honeypot12    rateLimit("report", `ip:${ctx.ip}`);13    const r = await createReport(ctx, {14      artifact: String(data.get("artifact") ?? "").trim(), version: String(data.get("version") ?? "").trim() || undefined, category: String(data.get("category") ?? ""),15      details: String(data.get("details") ?? ""), contact: String(data.get("contact") ?? "").trim() || undefined,16    });17    return { ok: `Thank you. Report ${r.id.slice(0, 8)} was recorded and will be reviewed by a moderator.` };18  } catch (e) {19    if (e instanceof ApiError) return { error: e.status === 404 ? "That artifact or version does not exist." : e.message };20    throw e;21  }22}23
Files main
Menu

Theme