apps/web: Next.js 16 (App Router), Tailwind 4 and shadcn/ui, light and dark from the system. Pages render on the server and call the API with Eden Treaty (typed by @t5de/api), forwarding the visitor's cookie; forms use server actions; sign-up, sign-in and sign-out use Better Auth's React client. /api is rewritten to the API so its cookies are first-party. Who's online follows the API's event stream (Next's gzip is off: it held the stream back). Standalone output with a Dockerfile; deploys to Vercel as is. Co-Authored-By: Claude Opus 5.5 <[email protected]>
23 lines
756 B
TypeScript
23 lines
756 B
TypeScript
// Server actions answer forms with an ActionState: an error to show, or a notice.
|
|
import "server-only";
|
|
import { ApiError } from "./api";
|
|
|
|
export type ActionState = { error?: string; notice?: string } | null;
|
|
|
|
/** Runs fn, turning the API's errors into a message for the form; fn may return a notice. */
|
|
export async function attempt(fn: () => Promise<string | void>): Promise<ActionState> {
|
|
try {
|
|
const notice = await fn();
|
|
return notice ? { notice } : {};
|
|
} catch (e) {
|
|
if (e instanceof ApiError) return { error: e.message };
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
/** A form field as a string ("" when missing). */
|
|
export function field(form: FormData, name: string): string {
|
|
const v = form.get(name);
|
|
return typeof v === "string" ? v.trim() : "";
|
|
}
|