|
| 1 | +import { Box, Divider, FormControl, FormControlLabel, FormLabel, ListSubheader, Menu, MenuItem, Radio, RadioGroup, Typography } from '@mui/material' |
| 2 | +import useCurrentUser from '../../hooks/useCurrentUser' |
| 3 | +import { useTranslation } from 'react-i18next' |
| 4 | +import { OutlineButtonBlack } from './general/Buttons' |
| 5 | +import { ArrowUpward, KeyboardReturn, Settings } from '@mui/icons-material' |
| 6 | +import { usePreferencesUpdateMutation } from '../../hooks/usePreferencesUpdateMutation' |
| 7 | +import { UserPreferencesSchema } from '../../../shared/user' |
| 8 | +import { useSnackbar } from 'notistack' |
| 9 | +import { useState } from 'react' |
| 10 | + |
| 11 | +export const SendPreferenceConfiguratorModal = ({ open, onClose, anchorEl, context }) => { |
| 12 | + const { user } = useCurrentUser() |
| 13 | + const preferenceUpdate = usePreferencesUpdateMutation() |
| 14 | + const { enqueueSnackbar } = useSnackbar() |
| 15 | + const { t } = useTranslation() |
| 16 | + |
| 17 | + const defaultValue = user?.preferences?.sendShortcutMode || 'shift+enter' |
| 18 | + const [value, setValue] = useState(defaultValue) |
| 19 | + |
| 20 | + const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => { |
| 21 | + event.preventDefault() |
| 22 | + onClose() |
| 23 | + const preferenceUpdates = { |
| 24 | + sendShortcutMode: value, |
| 25 | + } |
| 26 | + let msg = t('sendPreferenceConfigurator:success') |
| 27 | + if (context === 'chat') { |
| 28 | + msg += ` ${t('sendPreferenceConfigurator:canChangeInSettings')}` |
| 29 | + } |
| 30 | + enqueueSnackbar(msg, { variant: 'success' }) |
| 31 | + await preferenceUpdate.mutateAsync(preferenceUpdates) |
| 32 | + } |
| 33 | + |
| 34 | + const handleClose = async () => { |
| 35 | + onClose() |
| 36 | + if (context === 'chat') { |
| 37 | + enqueueSnackbar(t('sendPreferenceConfigurator:canChangeInSettings'), { variant: 'info' }) |
| 38 | + } |
| 39 | + await preferenceUpdate.mutateAsync({ sendShortcutMode: defaultValue }) |
| 40 | + } |
| 41 | + |
| 42 | + const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 43 | + // Zod parsing to please ts |
| 44 | + const preferenceUpdates = UserPreferencesSchema.parse({ |
| 45 | + sendShortcutMode: (event.target as HTMLInputElement).value, |
| 46 | + }) |
| 47 | + if (preferenceUpdates.sendShortcutMode) { |
| 48 | + setValue(preferenceUpdates.sendShortcutMode) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return ( |
| 53 | + <Menu |
| 54 | + open={open} |
| 55 | + onClose={handleClose} |
| 56 | + anchorEl={anchorEl} |
| 57 | + anchorOrigin={{ |
| 58 | + vertical: 'top', |
| 59 | + horizontal: 'right', |
| 60 | + }} |
| 61 | + transformOrigin={{ |
| 62 | + vertical: 'bottom', |
| 63 | + horizontal: 'right', |
| 64 | + }} |
| 65 | + > |
| 66 | + <form onSubmit={handleSubmit}> |
| 67 | + <FormControl sx={{ p: 2 }}> |
| 68 | + <Typography>{t('sendPreferenceConfigurator:title')}</Typography> |
| 69 | + <RadioGroup value={value} onChange={handleChange} name="sendPreferenceConfigurator"> |
| 70 | + <FormControlLabel |
| 71 | + sx={{ my: 2 }} |
| 72 | + value="shift+enter" |
| 73 | + control={<Radio />} |
| 74 | + label={ |
| 75 | + <div> |
| 76 | + <Box display="flex" alignItems="center" gap={1}> |
| 77 | + <strong>{t('sendPreferenceConfigurator:shift')}</strong> |
| 78 | + <ArrowUpward fontSize="small" />+ <strong>{t('sendPreferenceConfigurator:return')}</strong> |
| 79 | + <KeyboardReturn fontSize="small" /> |
| 80 | + {t('sendPreferenceConfigurator:toSend')} |
| 81 | + </Box> |
| 82 | + <Box display="flex" alignItems="center" gap={1}> |
| 83 | + <strong>{t('sendPreferenceConfigurator:return')}</strong> |
| 84 | + <KeyboardReturn fontSize="small" /> |
| 85 | + {t('sendPreferenceConfigurator:toAddNewline')} |
| 86 | + </Box> |
| 87 | + </div> |
| 88 | + } |
| 89 | + /> |
| 90 | + <FormControlLabel |
| 91 | + sx={{ mb: 2 }} |
| 92 | + value="enter" |
| 93 | + control={<Radio />} |
| 94 | + label={ |
| 95 | + <div> |
| 96 | + <Box display="flex" alignItems="center" gap={1}> |
| 97 | + <strong>{t('sendPreferenceConfigurator:return')}</strong> |
| 98 | + <KeyboardReturn fontSize="small" /> |
| 99 | + {t('sendPreferenceConfigurator:toSend')} |
| 100 | + </Box> |
| 101 | + <Box display="flex" alignItems="center" gap={1}> |
| 102 | + <strong>{t('sendPreferenceConfigurator:shift')}</strong> |
| 103 | + <ArrowUpward fontSize="small" />+ <strong>{t('sendPreferenceConfigurator:return')}</strong> |
| 104 | + <KeyboardReturn fontSize="small" /> |
| 105 | + {t('sendPreferenceConfigurator:toAddNewline')} |
| 106 | + </Box> |
| 107 | + </div> |
| 108 | + } |
| 109 | + /> |
| 110 | + </RadioGroup> |
| 111 | + <OutlineButtonBlack type="submit">{t('sendPreferenceConfigurator:ok')}</OutlineButtonBlack> |
| 112 | + </FormControl> |
| 113 | + </form> |
| 114 | + </Menu> |
| 115 | + ) |
| 116 | +} |
| 117 | + |
| 118 | +export const SendPreferenceConfiguratorButton = () => { |
| 119 | + const { t } = useTranslation() |
| 120 | + const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null) |
| 121 | + const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => setAnchorEl(event.currentTarget) |
| 122 | + const handleClose = () => setAnchorEl(null) |
| 123 | + const open = Boolean(anchorEl) |
| 124 | + |
| 125 | + return ( |
| 126 | + <> |
| 127 | + <OutlineButtonBlack onClick={handleClick} endIcon={<Settings />}> |
| 128 | + {t('sendPreferenceConfigurator:openConfigurator')} |
| 129 | + </OutlineButtonBlack> |
| 130 | + <SendPreferenceConfiguratorModal open={open} onClose={handleClose} anchorEl={anchorEl} context="settings" /> |
| 131 | + </> |
| 132 | + ) |
| 133 | +} |
0 commit comments