All files / src/components LanguageMenu.tsx

88.23% Statements 15/17
83.33% Branches 5/6
88.88% Functions 8/9
88.23% Lines 15/17

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                                      2x 28x 28x 56x       28x                                   28x 28x 28x 28x   28x                     1x                             84x         1x   1x 1x                        
// Chooses the language before sign-in: Suomi, Svenska, English. Paraglide's
// setLocale() stores the choice in the kotivalo_locale cookie and reloads,
// so the server renders the page, and later a new account, in it. The choice
// is also remembered for this tab, so signing in stores it on an existing
// account too (spec: "Choosing the language before sign-in").
import Button from '@mui/material/Button'
import Menu from '@mui/material/Menu'
import MenuItem from '@mui/material/MenuItem'
import { useId, useState } from 'react'
import { rememberChosenLocale } from '#/lib/chosen-locale'
import { m } from '#/paraglide/messages.js'
import {
  getLocale,
  locales,
  setLocale,
  type Locale,
} from '#/paraglide/runtime.js'
 
// Each language's name in itself.
const names: Record<Locale, () => string> = {
  fi: () => m.language_fi(),
  sv: () => m.language_sv(),
  en: () => m.language_en(),
}
 
function GlobeIcon() {
  return (
    <svg
      width="16"
      height="16"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2"
      strokeLinecap="round"
      aria-hidden="true"
    >
      <circle cx="12" cy="12" r="9" />
      <path d="M3 12 H21 M12 3 C15 6 15 18 12 21 C9 18 9 6 12 3" />
    </svg>
  )
}
 
export function LanguageMenu() {
  const [anchor, setAnchor] = useState<HTMLElement | null>(null)
  const menuId = useId()
  const current = getLocale()
  const currentName = names[current]()
 
  return (
    <>
      <Button
        variant="outlined"
        color="primary"
        aria-label={m.language_menu_label({ language: currentName })}
        aria-haspopup="menu"
        aria-controls={anchor ? menuId : undefined}
        aria-expanded={anchor ? 'true' : undefined}
        startIcon={<GlobeIcon />}
        onClick={(e) => {
          setAnchor(e.currentTarget)
        }}
        sx={{ bgcolor: 'background.paper', fontSize: 13, px: 1.5 }}
      >
        {currentName}
      </Button>
      <Menu
        id={menuId}
        anchorEl={anchor}
        open={anchor !== null}
        onClose={() => {
          setAnchor(null)
        }}
      >
        {locales.map((locale) => (
          <MenuItem
            key={locale}
            lang={locale}
            selected={locale === current}
            onClick={() => {
              setAnchor(null)
              // Choosing the current language counts too: it is a choice.
              rememberChosenLocale(locale)
              Iif (locale !== current) {
                void setLocale(locale)
              }
            }}
          >
            {names[locale]()}
          </MenuItem>
        ))}
      </Menu>
    </>
  )
}