sendou.ink/app/features/theme/routes/theme.ts
2026-01-11 22:33:19 +01:00

38 lines
926 B
TypeScript

import {
type ActionFunction,
data,
type LoaderFunction,
redirect,
} from "react-router";
import { isTheme } from "../core/provider";
import { getThemeSession } from "../core/theme-session.server";
export const action: ActionFunction = async ({ request }) => {
const themeSession = await getThemeSession(request);
const requestText = await request.text();
const form = new URLSearchParams(requestText);
const theme = form.get("theme");
if (theme === "auto") {
return data(
{ success: true },
{ headers: { "Set-Cookie": await themeSession.destroy() } },
);
}
if (!isTheme(theme)) {
return {
success: false,
message: `theme value of ${theme ?? "null"} is not a valid theme`,
};
}
themeSession.setTheme(theme);
return data(
{ success: true },
{ headers: { "Set-Cookie": await themeSession.commit() } },
);
};
export const loader: LoaderFunction = () => redirect("/", { status: 404 });