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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 3x 2x 3x 3x 3x 3x 3x 3x 3x 1x 3x 1x 1x 1x 1x 3x 51x 3x 2x 2x | import CheckIcon from "@mui/icons-material/Check"; import ExpandMoreOutlinedIcon from "@mui/icons-material/ExpandMoreOutlined"; import { Box, FormControl, ListItemIcon, ListItemText, MenuItem, Select, SelectChangeEvent, Stack, styled, useMediaQuery, } from "@mui/material"; import { useAuthContext } from "features/auth/AuthProvider"; import { Empty } from "google-protobuf/google/protobuf/empty_pb"; import { RpcError } from "grpc-web"; import { useTranslation } from "i18n"; import { LANGUAGE_MAP } from "i18n/constants"; import { GLOBAL } from "i18n/namespaces"; import { useRouter } from "next/router"; // we'll use this to reload the components w/ changed languages import { useMutation } from "react-query"; import { service } from "service"; import { theme } from "theme"; interface StyledMuiSelectProps { displayMode?: "round" | "rect"; } const StyledSelect = styled(Select, { shouldForwardProp: (prop) => prop !== "displayMode", })<StyledMuiSelectProps>(({ theme, displayMode }) => ({ borderRadius: displayMode === "round" ? 999 : theme.shape.borderRadius, "& .MuiSelect-icon": { color: theme.palette.text.primary, fontSize: "1.25rem", top: "50%", transform: "translateY(-50%)", right: 10, }, height: 41.25, })); type LanguagePickerSelectProps = { displayMode?: "round" | "rect"; }; export default function LanguagePickerSelect({ displayMode = "round", }: LanguagePickerSelectProps) { const router = useRouter(); const { asPath, locale, pathname } = router; const { authState } = useAuthContext(); const isAuthenticated = authState.authenticated; const isMobile = useMediaQuery(theme.breakpoints.down("md")); const { t } = useTranslation([GLOBAL]); const { mutate: changeLanguageMutation } = useMutation< Empty, RpcError, string >((newLanguage: string) => service.account.changeLanguage(newLanguage)); const handleChange = async (event: SelectChangeEvent<unknown>) => { const newLocale = event.target.value as string; if (isAuthenticated) { await changeLanguageMutation(newLocale); } // Push new route with updated locale, keep the current asPath for display router.push({ pathname }, asPath, { locale: newLocale }); }; const renderFlag = (flagCode: string) => ( <img alt={`${flagCode} flag`} src={`https://cdn.couchers.org/img/language-icons/${flagCode}.svg`} style={{ width: 25 }} /> ); const menuItems: React.ReactNode[] = Object.entries(LANGUAGE_MAP).map( ([languageCode, { flagIconCode }]) => { return ( <MenuItem key={languageCode} value={languageCode} sx={{ display: "flex", alignItems: "center", gap: theme.spacing(1), "& .Mui-selected": { backgroundColor: theme.palette.action.selected, }, "& .Mui-selected:hover": { backgroundColor: theme.palette.action.hover, }, }} > <Stack sx={{ width: "100%" }} direction="row" alignItems="center" justifyContent="space-between" > <Stack direction="row"> <ListItemIcon>{renderFlag(flagIconCode)}</ListItemIcon> <ListItemText sx={{ color: "#666666", fontWeight: "bold", display: "inline", }} > {languageCode.toUpperCase()} </ListItemText> </Stack> <div> {locale === languageCode && ( <CheckIcon fontSize="small" sx={{ color: "#00a69a" }} /> )} </div> </Stack> </MenuItem> ); }, ); // renderValue function for what should be rendered after a selection is made const renderValue = (value: unknown) => { const selected = value as string; const selectedDisplay = ( <Box sx={{ display: "flex", alignItems: "center", gap: 1, pl: 1, color: "#666666", fontWeight: "bold", }} > {renderFlag(LANGUAGE_MAP[selected].flagIconCode)} {selected.toUpperCase()} </Box> ); return selectedDisplay; }; return ( <Box sx={{ minWidth: 40 }}> <FormControl variant="outlined" sx={{ width: displayMode === "round" ? "fit-content" : !isMobile ? "241px" : "100%", }} > {displayMode === "round" ? ( <StyledSelect id="language-select" value={locale || ""} displayMode={displayMode} onChange={handleChange} // Use renderValue to display the selected language in collapsed state renderValue={renderValue} IconComponent={ExpandMoreOutlinedIcon} > {menuItems} </StyledSelect> ) : ( <StyledSelect id="newLanguage" displayMode={displayMode} value={locale} placeholder={t("global:language_preference.select_language")} fullWidth={isMobile} onChange={handleChange} > {menuItems} </StyledSelect> )} </FormControl> </Box> ); } |