All files / app/features/auth DarkModeSettings.tsx

0% Statements 0/35
0% Branches 0/13
0% Functions 0/6
0% Lines 0/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91                                                                                                                                                                                     
import {
  DarkModeOutlined,
  LightModeOutlined,
  SettingsBrightnessOutlined,
} from "@mui/icons-material";
import { Box, Button, styled, Typography } from "@mui/material";
import { useColorScheme } from "@mui/material/styles";
import { useTranslation } from "i18n";
import { AUTH } from "i18n/namespaces";
import { useEffect, useRef, useState } from "react";
 
const ModeDisplay = styled(Box)(({ theme }) => ({
  display: "flex",
  alignItems: "center",
  gap: theme.spacing(1),
  marginTop: theme.spacing(1),
  marginBottom: theme.spacing(2),
}));
 
export default function DarkModeSettings() {
  const { t } = useTranslation(AUTH);
  const { mode, setMode } = useColorScheme();
  const [isTransitioning, setIsTransitioning] = useState(false);
  const previousModeRef = useRef(mode);
 
  useEffect(() => {
    Iif (mode && previousModeRef.current !== mode) {
      setIsTransitioning(false);
      previousModeRef.current = mode;
    }
  }, [mode]);
 
  Iif (!mode) {
    return null;
  }
 
  const handleToggle = () => {
    setIsTransitioning(true);
    if (mode === "light") {
      setMode("dark");
    } else if (mode === "dark") {
      setMode("system");
    } else {
      setMode("light");
    }
  };
 
  const getModeIcon = () => {
    Iif (mode === "system") {
      return <SettingsBrightnessOutlined />;
    }
    return mode === "dark" ? <DarkModeOutlined /> : <LightModeOutlined />;
  };
 
  const getModeText = () => {
    Iif (mode === "light")
      return t("account_settings_page.appearance_section.modes.light");
    Iif (mode === "dark")
      return t("account_settings_page.appearance_section.modes.dark");
    return t("account_settings_page.appearance_section.modes.system");
  };
 
  return (
    <div>
      <Typography variant="h2">
        {t("account_settings_page.appearance_section.title")}
      </Typography>
      <Typography variant="body1">
        {t("account_settings_page.appearance_section.current_mode")}
      </Typography>
      <ModeDisplay>
        {getModeIcon()}
        <Typography variant="body1" component="span" fontWeight="bold">
          {getModeText()}
        </Typography>
      </ModeDisplay>
      <Typography variant="body1" sx={{ mb: 2 }}>
        {t("account_settings_page.appearance_section.click_to_change")}
      </Typography>
      <Button
        variant="contained"
        onClick={handleToggle}
        disabled={isTransitioning}
        startIcon={getModeIcon()}
      >
        {t("account_settings_page.appearance_section.change_mode")}
      </Button>
    </div>
  );
}