import { desc, eq } from "drizzle-orm"; import { headers } from "next/headers"; import { redirect } from "next/navigation"; import { auth } from "@/lib/auth/server"; import { db } from "@/lib/db/client"; import { webhookConnections, webhookEventLog } from "@/lib/db/schema"; import { getSetting } from "@/lib/services/settings"; import { APP_VERSION, GIT_COMMIT } from "@/lib/version"; import { AccountSection } from "./_components/account-section"; import { IntegrationsSection } from "./_components/integrations-section"; import { ServerSection } from "./_components/server-section"; import { SettingsShell } from "./_components/settings-shell"; export default async function SettingsPage() { const session = await auth.api.getSession({ headers: await headers() }); if (!session?.user) redirect("/login"); const isAdmin = session.user.role === "admin"; const connections = db .select() .from(webhookConnections) .where(eq(webhookConnections.userId, session.user.id)) .all() .map((conn) => { const events = db .select() .from(webhookEventLog) .where(eq(webhookEventLog.connectionId, conn.id)) .orderBy(desc(webhookEventLog.receivedAt)) .limit(10) .all(); return { id: conn.id, provider: conn.provider, token: conn.token, mediaServerUsername: conn.mediaServerUsername, enabled: conn.enabled, lastEventAt: conn.lastEventAt?.toISOString() ?? null, recentEvents: events.map((e) => ({ id: e.id, eventType: e.eventType, mediaType: e.mediaType, mediaTitle: e.mediaTitle, status: e.status, receivedAt: e.receivedAt.toISOString(), })), }; }); const registrationOpen = isAdmin ? getSetting("registrationOpen") === "true" : false; const repoUrl = "https://github.com/jakejarvis/sofa"; return (

Sofa {" "} v{APP_VERSION} {GIT_COMMIT && ( <> {" "} ( {GIT_COMMIT} ) )}

} > {isAdmin && }
); }