import { useLocation, useMatches, useSearchParams, useSubmit } from "react-router"; import { useRootData } from "~/utils/data/useRootData"; import AnalyticsHelper from "~/utils/helpers/AnalyticsHelper"; import { Button } from "../button"; import { cn } from "~/lib/utils"; import { Fragment } from "react/jsx-runtime"; import { useTranslation } from "react-i18next"; interface Props { className?: string; disabled?: boolean; } export default function DarkModeToggle({ className, disabled }: Props) { const { userSession } = useRootData(); const { t } = useTranslation(); let location = useLocation(); const [searchParams] = useSearchParams(); const submit = useSubmit(); const rootData = useRootData(); const matches = useMatches(); const toggle = async () => { const form = new FormData(); form.set("action", "toggleLightOrDarkMode"); form.set("redirect", location.pathname + "?" + searchParams.toString()); if (rootData.csrf) { form.set("csrf", rootData.csrf); } submit(form, { method: "post", action: "/preferences", preventScrollReset: true }); const routeMatch = matches.find((m) => m.pathname == location.pathname); const isDarkMode = userSession?.lightOrDarkMode === "dark"; AnalyticsHelper.addEvent({ url: location.pathname, route: routeMatch?.id, rootData, action: "toggleLightOrDarkMode", category: "user", label: "lightOrDarkMode", value: isDarkMode ? "light" : "dark", }); }; return ( ); } export function DarkModeSvgs({ className, withLabel, filled }: { className?: string; withLabel?: boolean; filled?: boolean }) { const { userSession } = useRootData(); const { t } = useTranslation(); const isDarkMode = userSession?.lightOrDarkMode === "dark"; return (
{!isDarkMode ? ( {!filled ? ( ) : ( )} ) : ( {!filled ? ( ) : ( )} )} {withLabel && {t(isDarkMode ? "components.themeToggle.modeLabel.light" : "components.themeToggle.modeLabel.dark")}}
); }