aboutsummaryrefslogtreecommitdiffstats
path: root/site/src/components/LanguageSwitcher.tsx
blob: 4601c47444dbce8b4b7eba81db76842c107bb66f (plain) (blame)
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
import { useTranslation } from 'react-i18next';
import { useSearchParams } from 'react-router-dom';
import { useRef, useState, useEffect } from 'react';

const languages = [
  { code: 'en', name: 'English' },
  { code: 'ja', name: '日本語' }
];

const LanguageSwitcher = () => {
  const { i18n } = useTranslation();
  const [searchParams] = useSearchParams();
  const isMoe = searchParams.has("moe");
  const [isOpen, setIsOpen] = useState(false);
  const dropdownRef = useRef<HTMLDivElement>(null);

  const currentLanguage = languages.find(lang => lang.code === i18n.language) || languages[0];

  // Close the dropdown when clicking outside
  useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
        setIsOpen(false);
      }
    };

    document.addEventListener('mousedown', handleClickOutside);
    return () => {
      document.removeEventListener('mousedown', handleClickOutside);
    };
  }, []);

  return (
    <div className="relative inline-block" ref={dropdownRef}>
      <button
        onClick={() => setIsOpen(!isOpen)}
        className={`
          flex items-center justify-between
          transition-all duration-200 text-sm rounded
          px-2 py-0.5 min-w-[80px]
          ${isMoe
            ? 'bg-pink-200 text-pink-800 hover:bg-pink-300'
            : 'bg-gray-800 text-gray-300 hover:bg-gray-700 hover:text-white'
          }
        `}
        aria-haspopup="true"
        aria-expanded={isOpen}
      >
        <span>{currentLanguage.name}</span>
        <span className="ml-1">
          <svg
            className={`h-4 w-4 transition-transform ${isOpen ? 'rotate-180' : ''}`}
            fill="none"
            viewBox="0 0 24 24"
            stroke="currentColor"
          >
            <path
              strokeLinecap="round"
              strokeLinejoin="round"
              strokeWidth={2}
              d="M19 9l-7 7-7-7"
            />
          </svg>
        </span>
      </button>

      {isOpen && (
        <div
          className={`
            absolute right-0 mt-1 z-10 shadow-lg rounded-md overflow-hidden w-24
            ${isMoe ? 'bg-pink-100 border border-pink-300' : 'bg-gray-800 border border-gray-700'}
          `}
        >
          <div className="py-1">
            {languages.map((lang) => (
              <button
                key={lang.code}
                onClick={() => {
                  i18n.changeLanguage(lang.code);
                  setIsOpen(false);
                }}
                className={`
                  block w-full text-left px-4 py-2 text-sm
                  ${i18n.language === lang.code
                    ? isMoe
                      ? 'bg-pink-300 text-pink-800 font-medium'
                      : 'bg-purple-700 text-white font-medium'
                    : isMoe
                      ? 'text-pink-800 hover:bg-pink-200'
                      : 'text-gray-300 hover:bg-gray-700 hover:text-white'
                  }
                `}
              >
                {lang.name}
              </button>
            ))}
          </div>
        </div>
      )}
    </div>
  );
}

export default LanguageSwitcher;
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage