sendou.ink/app/modules/theme/action.server.ts
Remmy Cat Stock 19fbd85f8f
Add auto theme option for detecting the theme from system/browser preferences (#1083)
* Add "auto" option to theme switcher

* Add labels to language and user menu buttons

* Update translation-progress.md
2022-11-03 01:44:56 +01:00

33 lines
872 B
TypeScript

import type { ActionFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import { getThemeSession } from "./session.server";
import { isTheme } from "./provider";
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 json(
{ success: true },
{ headers: { "Set-Cookie": await themeSession.destroy() } }
);
}
if (!isTheme(theme)) {
return json({
success: false,
message: `theme value of ${theme ?? "null"} is not a valid theme`,
});
}
themeSession.setTheme(theme);
return json(
{ success: true },
{ headers: { "Set-Cookie": await themeSession.commit() } }
);
};