|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import React, { useEffect, useState } from 'react'; |
| 4 | +import { |
| 5 | + Box, |
| 6 | + Dialog, |
| 7 | + Typography, |
| 8 | + Grid, |
| 9 | + ButtonBase, |
| 10 | +} from '@mui/material'; |
| 11 | +import type { DialogProps } from '@mui/material/Dialog'; |
| 12 | +import LanguageIcon from '@mui/icons-material/Language'; |
| 13 | +import { useTranslation } from '@shared-lib'; |
| 14 | +import { |
| 15 | + PLP_LANGUAGE_OPTIONS, |
| 16 | + PLP_LANGUAGE_POPUP_DISMISSED_KEY, |
| 17 | +} from './languageOptions'; |
| 18 | + |
| 19 | +export type LanguageSelectionPopupSize = NonNullable<DialogProps['maxWidth']>; |
| 20 | + |
| 21 | +interface LanguageSelectionPopupProps { |
| 22 | + /** MUI Dialog maxWidth — xs (444px), sm (600px), md (900px), etc. */ |
| 23 | + size?: LanguageSelectionPopupSize; |
| 24 | +} |
| 25 | + |
| 26 | +const LanguageSelectionPopup: React.FC<LanguageSelectionPopupProps> = ({ |
| 27 | + size = 'xs', |
| 28 | +}) => { |
| 29 | + const { setLanguage } = useTranslation(); |
| 30 | + const [open, setOpen] = useState(false); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + if (typeof window === 'undefined') return; |
| 34 | + const dismissed = sessionStorage.getItem(PLP_LANGUAGE_POPUP_DISMISSED_KEY); |
| 35 | + if (!dismissed) { |
| 36 | + setOpen(true); |
| 37 | + } |
| 38 | + }, []); |
| 39 | + |
| 40 | + const persistLanguage = (code: string) => { |
| 41 | + setLanguage(code); |
| 42 | + localStorage.setItem('lang', code); |
| 43 | + localStorage.setItem('preferredLanguage', code); |
| 44 | + |
| 45 | + if (localStorage.getItem('isAndroidApp') === 'yes' && window.ReactNativeWebView) { |
| 46 | + window.ReactNativeWebView.postMessage( |
| 47 | + JSON.stringify({ |
| 48 | + type: 'LANGUAGE_CHANGE_EVENT', |
| 49 | + data: { language: code }, |
| 50 | + }) |
| 51 | + ); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + const handleLanguageSelect = (code: string) => { |
| 56 | + persistLanguage(code); |
| 57 | + sessionStorage.setItem(PLP_LANGUAGE_POPUP_DISMISSED_KEY, 'true'); |
| 58 | + setOpen(false); |
| 59 | + }; |
| 60 | + |
| 61 | + return ( |
| 62 | + <Dialog |
| 63 | + open={open} |
| 64 | + maxWidth={size} |
| 65 | + fullWidth |
| 66 | + onClose={(_, reason) => { |
| 67 | + if (reason === 'backdropClick' || reason === 'escapeKeyDown') { |
| 68 | + return; |
| 69 | + } |
| 70 | + }} |
| 71 | + aria-labelledby="language-selection-title" |
| 72 | + aria-describedby="language-selection-description" |
| 73 | + slotProps={{ |
| 74 | + backdrop: { |
| 75 | + sx: { |
| 76 | + backgroundColor: 'rgba(0, 0, 0, 0.45)', |
| 77 | + backdropFilter: 'blur(4px)', |
| 78 | + }, |
| 79 | + }, |
| 80 | + }} |
| 81 | + PaperProps={{ |
| 82 | + sx: { |
| 83 | + borderRadius: '16px', |
| 84 | + boxShadow: '0px 8px 32px rgba(0, 0, 0, 0.12)', |
| 85 | + m: { xs: 2, sm: 3 }, |
| 86 | + }, |
| 87 | + }} |
| 88 | + > |
| 89 | + <Box sx={{ p: { xs: 2.5, sm: 3 } }}> |
| 90 | + <Box |
| 91 | + sx={{ |
| 92 | + display: 'flex', |
| 93 | + flexDirection: 'column', |
| 94 | + alignItems: 'center', |
| 95 | + mb: 2.5, |
| 96 | + }} |
| 97 | + > |
| 98 | + <Box |
| 99 | + sx={{ |
| 100 | + width: 56, |
| 101 | + height: 56, |
| 102 | + borderRadius: '50%', |
| 103 | + bgcolor: '#ffffff', |
| 104 | + display: 'flex', |
| 105 | + alignItems: 'center', |
| 106 | + justifyContent: 'center', |
| 107 | + mb: 1.5, |
| 108 | + }} |
| 109 | + > |
| 110 | + <LanguageIcon sx={{ color: '#1ba4fb', fontSize: 55 }} /> |
| 111 | + </Box> |
| 112 | + <Typography |
| 113 | + id="language-selection-title" |
| 114 | + sx={{ |
| 115 | + fontFamily: 'Poppins, Arial, sans-serif', |
| 116 | + fontWeight: 500, |
| 117 | + fontSize: { xs: '18px', sm: '20px' }, |
| 118 | + color: '#49454F', |
| 119 | + textAlign: 'center', |
| 120 | + }} |
| 121 | + > |
| 122 | + Select your language |
| 123 | + </Typography> |
| 124 | + </Box> |
| 125 | + |
| 126 | + <Grid |
| 127 | + container |
| 128 | + spacing={1.5} |
| 129 | + id="language-selection-description" |
| 130 | + > |
| 131 | + {PLP_LANGUAGE_OPTIONS.map((option) => ( |
| 132 | + <Grid item xs={4} key={option.code}> |
| 133 | + <ButtonBase |
| 134 | + onClick={() => handleLanguageSelect(option.code)} |
| 135 | + sx={{ |
| 136 | + width: '100%', |
| 137 | + border: '1px solid #E0E0E0', |
| 138 | + borderRadius: '12px', |
| 139 | + py: 1.5, |
| 140 | + px: 1, |
| 141 | + display: 'flex', |
| 142 | + flexDirection: 'column', |
| 143 | + alignItems: 'center', |
| 144 | + justifyContent: 'center', |
| 145 | + minHeight: 72, |
| 146 | + transition: 'border-color 0.2s, background-color 0.2s', |
| 147 | + '&:hover': { |
| 148 | + borderColor: '#BDBDBD', |
| 149 | + backgroundColor: '#FAFAFA', |
| 150 | + }, |
| 151 | + }} |
| 152 | + > |
| 153 | + <Typography |
| 154 | + sx={{ |
| 155 | + fontFamily: 'Poppins, Arial, sans-serif', |
| 156 | + fontWeight: option.code === 'en' ? 600 : 500, |
| 157 | + fontSize: option.code === 'en' ? '15px' : '16px', |
| 158 | + color: '#1F1B13', |
| 159 | + lineHeight: 1.3, |
| 160 | + textAlign: 'center', |
| 161 | + }} |
| 162 | + > |
| 163 | + {option.nativeLabel} |
| 164 | + </Typography> |
| 165 | + {option.englishLabel && ( |
| 166 | + <Typography |
| 167 | + sx={{ |
| 168 | + fontFamily: 'Poppins, Arial, sans-serif', |
| 169 | + fontWeight: 400, |
| 170 | + fontSize: '12px', |
| 171 | + color: '#79747E', |
| 172 | + mt: 0.25, |
| 173 | + lineHeight: 1.3, |
| 174 | + textAlign: 'center', |
| 175 | + }} |
| 176 | + > |
| 177 | + {option.englishLabel} |
| 178 | + </Typography> |
| 179 | + )} |
| 180 | + </ButtonBase> |
| 181 | + </Grid> |
| 182 | + ))} |
| 183 | + </Grid> |
| 184 | + </Box> |
| 185 | + </Dialog> |
| 186 | + ); |
| 187 | +}; |
| 188 | + |
| 189 | +export default LanguageSelectionPopup; |
0 commit comments