mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-19 09:49:11 -05:00
38 lines
926 B
TypeScript
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 });
|