Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
import { assertIsPost } from "@carbon/auth";
import { validationError, validator } from "@carbon/form";
import {
Button,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
cn,
HStack,
PrefetchLink,
RadioGroup,
RadioGroupButton,
useMode,
useModePreference,
useThemePreview,
VStack
} from "@carbon/react";
import type { Theme } from "@carbon/utils";
import { redirect, themes } from "@carbon/utils";
import { msg } from "@lingui/core/macro";
import { Trans, useLingui } from "@lingui/react/macro";
import { useEffect, useState } from "react";
import { BiLaptop, BiMoon, BiSun } from "react-icons/bi";
import { RxCheck } from "react-icons/rx";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import {
useFetcher,
useLoaderData,
useNavigation,
useSubmit
} from "react-router";
import { OnboardingCard, OnboardingCardContent } from "~/components";
import { useOnboarding } from "~/hooks";
import type { Theme as ThemeValue } from "~/modules/settings/settings.models";
import { themeValidator } from "~/modules/settings/settings.models";
import type { action as modeAction } from "~/root";
import { getTheme, setTheme } from "~/services/theme.server";
import { ONBOARDING_SHORTCUTS } from "~/shortcuts";
import type { Handle } from "~/utils/handle";
import { path } from "~/utils/path";
export const handle: Handle = {
breadcrumb: msg`Theme`,
to: path.to.theme
};
export async function loader({ request }: LoaderFunctionArgs) {
return {
theme: getTheme(request)
};
}
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
const formData = await request.formData();
const validation = await validator(themeValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
}
const { next, theme } = validation.data;
if (!next) throw new Error("Fatal: next is required");
throw redirect(next || path.to.onboarding.root, {
headers: setTheme(theme, { inApp: false })
});
}
export default function OnboardingTheme() {
const { theme: initialTheme } = useLoaderData<typeof loader>();
const { t } = useLingui();
const mode = useMode();
const modePreference = useModePreference();
const modeFetcher = useFetcher<typeof modeAction>();
const [picked, setPicked] = useState<ThemeValue | null>(null);
const theme = picked ?? initialTheme;
// Kept until the step is left: the pick is saved only on Continue.
const previewTheme = useThemePreview(false);
const applyThemeColors = (t: Theme) => {
previewTheme(mode === "dark" ? t.cssVars.dark : t.cssVars.light);
window.dispatchEvent(
new CustomEvent("onboarding-theme-change", { detail: t.name })
);
};
const onThemeChange = (t: Theme) => {
setPicked(t.name);
applyThemeColors(t);
};
// biome-ignore lint/correctness/useExhaustiveDependencies: suppressed due to migration
useEffect(() => {
const t = themes.find((t) => t.name === theme);
if (t) {
applyThemeColors(t);
}
}, [mode]);
const { next, previous } = useOnboarding();
const submit = useSubmit();
const onSubmit = () => {
const formData = new FormData();
formData.append("theme", theme);
formData.append("next", next);
submit(formData, {
method: "post"
});
};
const transition = useNavigation();
const onModeChange = (nextMode: string) => {
document.body.removeAttribute("style");
modeFetcher.submit(
{ mode: nextMode },
{ method: "post", action: path.to.root }
);
};
return (
<OnboardingCard>
<CardHeader>
<CardTitle>
<Trans>Choose your style</Trans>
</CardTitle>
<CardDescription>
<Trans>
You can change the UI style any time through the theme setting
</Trans>
</CardDescription>
</CardHeader>
<OnboardingCardContent>
<VStack spacing={4}>
<RadioGroup
value={modePreference}
onValueChange={onModeChange}
aria-label={t`Appearance`}
className="flex w-full gap-2"
>
<RadioGroupButton
value="light"
autoFocus={modePreference === "light"}
className={cn(
"flex-1",
modePreference === "light" && "border-2 border-primary"
)}
>
<BiSun />
<Trans>Light</Trans>
</RadioGroupButton>
<RadioGroupButton
value="dark"
autoFocus={modePreference === "dark"}
className={cn(
"flex-1",
modePreference === "dark" && "border-2 border-primary"
)}
>
<BiMoon />
<Trans>Dark</Trans>
</RadioGroupButton>
<RadioGroupButton
value="system"
autoFocus={modePreference === "system"}
className={cn(
"flex-1",
modePreference === "system" && "border-2 border-primary"
)}
>
<BiLaptop />
<Trans>System</Trans>
</RadioGroupButton>
</RadioGroup>
<RadioGroup
value={theme}
onValueChange={(name) => {
const selected = themes.find((entry) => entry.name === name);
if (selected) onThemeChange(selected);
}}
aria-label={t`Theme`}
className="w-full grid grid-cols-3 gap-4"
>
{themes.map((t) => {
const isActive = theme === t.name;
return (
<RadioGroupButton
key={t.name}
value={t.name}
className={cn(
"justify-start",
isActive && "border-2 border-primary"
)}
style={
{
"--theme-primary": `hsl(${
t?.activeColor[mode === "dark" ? "dark" : "light"]
})`,
borderColor: `hsl(${
t?.activeColor[mode === "dark" ? "dark" : "light"]
})`
} as React.CSSProperties
}
>
<span
className={cn(
"mr-1 flex h-5 w-5 shrink-0 -translate-x-1 items-center justify-center rounded-full bg-[var(--theme-primary)]"
)}
>
{isActive && <RxCheck className="h-4 w-4 text-white" />}
</span>
{t.label}
</RadioGroupButton>
);
})}
</RadioGroup>
</VStack>
</OnboardingCardContent>
<CardFooter>
<HStack>
{previous && (
<Button
variant="solid"
isDisabled={!previous}
size="md"
asChild
tabIndex={-1}
>
<PrefetchLink to={previous}>
<Trans>Previous</Trans>
</PrefetchLink>
</Button>
)}
<Button
isLoading={transition.state !== "idle"}
isDisabled={transition.state !== "idle"}
shortcut={ONBOARDING_SHORTCUTS.continue}
onClick={onSubmit}
>
<Trans>Next</Trans>
</Button>
</HStack>
</CardFooter>
</OnboardingCard>
);
}