All files / src/features/account AccountPage.tsx

100% Statements 27/27
100% Branches 8/8
100% Functions 11/11
100% Lines 27/27

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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259                                                                      2x 17x 17x 17x                   17x 17x 17x     3x 1x   2x 2x 2x 2x   1x   1x 1x 1x       17x                             51x 51x                   3x                                                                         14x 14x                                                                       14x   14x                                                                                                           3x                                               2x                  
// /account (Design canvas: TiliKevyt): the display name and address, the
// language and signing out (openspec: account).
import Box from '@mui/material/Box'
import Button from '@mui/material/Button'
import Link from '@mui/material/Link'
import Stack from '@mui/material/Stack'
import Typography from '@mui/material/Typography'
import { useId, useState, type ReactNode } from 'react'
import { ReasonAlert } from '#/components/ReasonAlert'
import { SupportLink } from '#/components/SupportLink'
import { clientLog } from '#/lib/client-log'
import { guideUrl } from '#/lib/guide'
import { failureReason } from '#/lib/network-error'
import type { AlertReason } from '#/lib/reasons'
import { m } from '#/paraglide/messages.js'
import { getLocale, locales, type Locale } from '#/paraglide/runtime.js'
import { colors, radii } from '#/theme/tokens'
import { AccountAvatar } from './AccountAvatar'
import { setAccountLanguage } from './functions'
import { NameDialog } from './NameDialog'
 
interface Props {
  name: string
  email: string
  locale: Locale
  /** The bottom navigation bar. */
  bottomNav?: ReactNode
  onSignOut: () => void
  /** Reloads the signed-in user after the name was changed. */
  onNameSaved: () => Promise<void>
  /** Loads the app again in the new language; it is already stored. */
  onLanguageChanged: (locale: Locale) => void
}
 
// Each language's name in itself.
const names: Record<Locale, () => string> = {
  fi: () => m.language_fi(),
  sv: () => m.language_sv(),
  en: () => m.language_en(),
}
 
function LanguageChoice({
  current,
  onChanged,
}: {
  current: Locale
  onChanged: (locale: Locale) => void
}) {
  const headingId = useId()
  const [saving, setSaving] = useState<Locale>()
  const [alert, setAlert] = useState<AlertReason>()
 
  async function choose(locale: Locale) {
    if (locale === current) {
      return
    }
    setAlert(undefined)
    setSaving(locale)
    try {
      await setAccountLanguage({ data: { locale } })
      // The page reloads; the buttons stay disabled until then.
      onChanged(locale)
    } catch (error) {
      clientLog.error('account.language_save_failed', {}, error)
      setAlert(failureReason(error))
      setSaving(undefined)
    }
  }
 
  return (
    <Stack component="section" sx={{ gap: 1 }}>
      <Typography id={headingId} variant="h3" component="h2">
        {m.account_language()}
      </Typography>
      <Box
        role="group"
        aria-labelledby={headingId}
        sx={{
          display: 'grid',
          gridTemplateColumns: `repeat(${String(locales.length)}, minmax(0, 1fr))`,
          gap: 1,
        }}
      >
        {locales.map((locale) => {
          const pressed = locale === current
          return (
            <Button
              key={locale}
              type="button"
              variant="outlined"
              lang={locale}
              aria-pressed={pressed}
              disabled={saving !== undefined}
              loading={saving === locale}
              onClick={() => {
                void choose(locale)
              }}
              // Like ChoiceField: the pressed one in plum.
              sx={{
                minWidth: 0,
                minHeight: 48,
                paddingInline: 1,
                borderRadius: `${String(radii.input)}px`,
                fontSize: 15,
                fontWeight: 900,
                ...(pressed
                  ? {
                      bgcolor: 'primary.main',
                      borderColor: 'primary.main',
                      color: 'primary.contrastText',
                      '&:hover': { bgcolor: 'primary.main' },
                    }
                  : {
                      bgcolor: 'background.paper',
                      '&:hover': { bgcolor: colors.sand },
                    }),
              }}
            >
              {names[locale]()}
            </Button>
          )
        })}
      </Box>
      <Typography variant="body2" color="text.secondary">
        {m.account_language_help()}
      </Typography>
      {alert === undefined ? null : <ReasonAlert reason={alert} />}
    </Stack>
  )
}
 
function SupportCard() {
  const headingId = useId()
  return (
    <Stack
      component="section"
      aria-labelledby={headingId}
      sx={{
        gap: 0.5,
        bgcolor: 'background.paper',
        border: `1px solid ${colors.cardBorder}`,
        borderRadius: `${String(radii.card)}px`,
        p: 2,
      }}
    >
      <Typography id={headingId} variant="h3" component="h2">
        {m.account_support_title()}
      </Typography>
      <Typography>
        <Link
          href={guideUrl(getLocale())}
          target="_blank"
          rel="noopener"
          sx={{ fontWeight: 800 }}
        >
          {m.account_guide_link()}
        </Link>
      </Typography>
      <Typography variant="body2" color="text.secondary">
        {m.account_support_text()}
      </Typography>
      <Typography sx={{ overflowWrap: 'anywhere' }}>
        <SupportLink />
      </Typography>
    </Stack>
  )
}
 
export function AccountPage(props: Props) {
  const [naming, setNaming] = useState(false)
 
  return (
    <Stack sx={{ minHeight: '100dvh' }}>
      <Box
        component="header"
        sx={{
          display: 'grid',
          gridTemplateColumns: '44px 1fr 44px',
          alignItems: 'center',
          px: 1.5,
          pt: 2,
          pb: 0.5,
        }}
      >
        <span />
        <Typography
          variant="h3"
          component="h1"
          sx={{ fontSize: 18, textAlign: 'center' }}
        >
          {m.account_title()}
        </Typography>
      </Box>
      <Stack component="main" sx={{ gap: 2, px: 2.5, pt: 1.25, pb: 2.5 }}>
        <Stack
          component="section"
          direction="row"
          sx={{
            alignItems: 'center',
            gap: 1.75,
            bgcolor: 'background.paper',
            border: `1px solid ${colors.cardBorder}`,
            borderRadius: `${String(radii.card)}px`,
            py: 1.75,
            pl: 2,
            pr: 1,
          }}
        >
          <AccountAvatar name={props.name} size={52} />
          <Stack sx={{ flex: 1, minWidth: 0 }}>
            <Typography sx={{ fontSize: 17, fontWeight: 900 }}>
              {props.name}
            </Typography>
            <Typography
              variant="body2"
              color="text.secondary"
              sx={{ overflowWrap: 'anywhere' }}
            >
              {props.email}
            </Typography>
          </Stack>
          <Button
            variant="outlined"
            size="small"
            onClick={() => {
              setNaming(true)
            }}
            sx={{ flexShrink: 0, bgcolor: 'background.paper' }}
          >
            {m.account_change_name()}
          </Button>
        </Stack>
        <LanguageChoice
          current={props.locale}
          onChanged={props.onLanguageChanged}
        />
        <SupportCard />
        <Button
          variant="text"
          onClick={props.onSignOut}
          sx={{ alignSelf: 'center', textDecoration: 'underline' }}
        >
          {m.sign_out()}
        </Button>
      </Stack>
      {naming ? (
        <NameDialog
          name={props.name}
          onClose={() => {
            setNaming(false)
          }}
          onSaved={props.onNameSaved}
        />
      ) : null}
      {props.bottomNav}
    </Stack>
  )
}