# All files / src/components ThemeSelector.tsx

0% Statements 0/46
0% Branches 0/16
0% Functions 0/7
0% Lines 0/45

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
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131

     'use client';

    import { useState, useEffect } from 'react';
    import { useTranslation } from 'react-i18next';
    import { Button } from '@/components/ui/button';
    import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
    import { Palette, Monitor, Moon, Sun, Zap, Globe } from 'lucide-react';
    import { THEME, STORAGE_KEYS } from '@/constants/app';

    const themeOptions = [
      { value: THEME.LIGHT, labelKey: 'theme.light', icon: Sun },
      { value: THEME.DARK, labelKey: 'theme.dark', icon: Moon },
      { value: THEME.MIDNIGHT, labelKey: 'theme.midnight', icon: Zap },
      { value: THEME.DARK_BLUE, labelKey: 'theme.darkBlue', icon: Palette },
      { value: THEME.SYSTEM, labelKey: 'theme.system', icon: Monitor },
    ];

    const languageOptions = [
      { value: 'en', labelKey: 'language.english' },
      { value: 'es', labelKey: 'language.spanish' },
      { value: 'fr', labelKey: 'language.french' },
      { value: 'pt', labelKey: 'language.portuguese' },
      { value: 'ru', labelKey: 'language.russian' },
      { value: 'tr', labelKey: 'language.turkish' },
      { value: 'sk', labelKey: 'language.slovak' },
      { value: 'zh', labelKey: 'language.chinese' },
    ];

    function applyTheme(theme: string) {
      const root = document.documentElement;
      // Remove previous theme classes
      root.classList.remove('dark', 'theme-midnight', 'theme-darkblue');

      switch (theme) {
        case THEME.DARK:
          root.classList.add('dark');
          break;
        case THEME.MIDNIGHT:
          root.classList.add('dark');
          root.classList.add('theme-midnight');
          break;
        case THEME.DARK_BLUE:
          root.classList.add('dark');
          root.classList.add('theme-darkblue');
          break;
        case THEME.SYSTEM: {
          const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
          if (prefersDark) root.classList.add('dark');
          break;
        }
        case THEME.LIGHT:
        default:
          // Light: no extra class (variables from :root)
          break;
      }
    }

    export default function ThemeSelector() {
      const { t, i18n } = useTranslation();
      const [currentTheme, setCurrentTheme] = useState<string>(THEME.SYSTEM);
      const [currentLanguage, setCurrentLanguage] = useState<string>('en');

      useEffect(() => {
        const storedTheme = (typeof window !== 'undefined' && localStorage.getItem(STORAGE_KEYS.THEME)) || THEME.SYSTEM;
        setCurrentTheme(storedTheme);

        const storedLanguage = (typeof window !== 'undefined' && localStorage.getItem(STORAGE_KEYS.LANGUAGE)) || 'en';
        setCurrentLanguage(storedLanguage);
        i18n.changeLanguage(storedLanguage);
      }, [i18n]);

      const handleThemeChange = (theme: string) => {
        setCurrentTheme(theme);
        try {
          localStorage.setItem(STORAGE_KEYS.THEME, theme);
          applyTheme(theme);
        } catch (_) {
          // ignore
        }
      };

      const handleLanguageChange = (language: string) => {
        setCurrentLanguage(language);
        i18n.changeLanguage(language);
        try {
          localStorage.setItem(STORAGE_KEYS.LANGUAGE, language);
        } catch (_) {
          // ignore
        }
      };

      return (
        <div className="flex items-center space-x-2">
          <Select value={currentTheme} onValueChange={handleThemeChange}>
            <SelectTrigger className="w-[160px] h-8 border-border bg-card">
              <SelectValue placeholder={t('theme.placeholder')} />
            </SelectTrigger>
            <SelectContent>
              {themeOptions.map((option) => {
                const OptionIcon = option.icon;
                return (
                  <SelectItem key={option.value} value={option.value}>
                    <div className="flex items-center space-x-2">
                      <OptionIcon className="h-4 w-4" />
                      <span>{t(option.labelKey)}</span>
                    </div>
                  </SelectItem>
                );
              })}
            </SelectContent>
          </Select>

          <Select value={currentLanguage} onValueChange={handleLanguageChange}>
            <SelectTrigger className="w-[140px] h-8 border-border bg-card">
              <SelectValue placeholder={t('language.placeholder')} />
            </SelectTrigger>
            <SelectContent>
              {languageOptions.map((option) => (
                <SelectItem key={option.value} value={option.value}>
                  <div className="flex items-center space-x-2">
                    <Globe className="h-4 w-4" />
                    <span>{t(option.labelKey)}</span>
                  </div>
                </SelectItem>
              ))}
            </SelectContent>
          </Select>
        </div>
      );
    }