import { ChangeEvent, useContext, useEffect, useState } from 'react' import FileReaderInput, { Result } from 'react-file-reader-input' import Box from '@mui/material/Box' import Button from '@mui/material/Button' import Typography from '@mui/material/Typography' import Divider from '@mui/material/Divider' import Switch from '@mui/material/Switch' import FormGroup from '@mui/material/FormGroup' import FormControlLabel from '@mui/material/FormControlLabel' import Paper from '@mui/material/Paper' import useTheme from '@mui/material/styles/useTheme' import { settings } from 'services/Settings' import { notification } from 'services/Notification' import { ShellContext } from 'contexts/ShellContext' import { StorageContext } from 'contexts/StorageContext' import { SettingsContext } from 'contexts/SettingsContext' import { PeerNameDisplay } from 'components/PeerNameDisplay' import { ConfirmDialog } from 'components/ConfirmDialog' import { isErrorWithMessage } from '../../lib/type-guards' interface SettingsProps { userId: string } export const Settings = ({ userId }: SettingsProps) => { const theme = useTheme() const { setTitle, showAlert } = useContext(ShellContext) const { updateUserSettings, getUserSettings } = useContext(SettingsContext) const { getPersistedStorage } = useContext(StorageContext) const [ isDeleteSettingsConfirmDiaglogOpen, setIsDeleteSettingsConfirmDiaglogOpen, ] = useState(false) const [, setIsNotificationPermissionDetermined] = useState(false) const { playSoundOnNewMessage, showNotificationOnNewMessage, showActiveTypingStatus, } = getUserSettings() const persistedStorage = getPersistedStorage() useEffect(() => { ;(async () => { await notification.requestPermission() // This state needs to be set to cause a rerender so that // areNotificationsAvailable is up-to-date. setIsNotificationPermissionDetermined(true) })() }, []) useEffect(() => { setTitle('Settings') }, [setTitle]) const handlePlaySoundOnNewMessageChange = ( _event: ChangeEvent, playSoundOnNewMessage: boolean ) => { updateUserSettings({ playSoundOnNewMessage }) } const handleShowNotificationOnNewMessageChange = ( _event: ChangeEvent, showNotificationOnNewMessage: boolean ) => { updateUserSettings({ showNotificationOnNewMessage }) } const handleShowActiveTypingStatusChange = ( _event: ChangeEvent, showActiveTypingStatus: boolean ) => { updateUserSettings({ showActiveTypingStatus }) } const handleDeleteSettingsClick = () => { setIsDeleteSettingsConfirmDiaglogOpen(true) } const handleDeleteSettingsCancel = () => { setIsDeleteSettingsConfirmDiaglogOpen(false) } const handleDeleteSettingsConfirm = async () => { await persistedStorage.clear() window.location.reload() } const handleExportSettingsClick = async () => { try { await settings.exportSettings(getUserSettings()) } catch (e) { if (isErrorWithMessage(e)) { showAlert(e.message, { severity: 'error' }) } } } const handleImportSettingsClick = async ([[, file]]: Result[]) => { try { const userSettings = await settings.importSettings(file) updateUserSettings(userSettings) showAlert('Profile successfully imported', { severity: 'success' }) } catch (e) { if (isErrorWithMessage(e)) { showAlert(e.message, { severity: 'error' }) } } } const areNotificationsAvailable = notification.permission === 'granted' return ( Chat When a message is received in the background: } label="Play a sound" /> } label="Show a notification" /> } label="Show active typing indicators" /> Disabling this will also hide your active typing status from others. Data Export profile data Export your Chitchatter profile data so that it can be moved to another browser or device.{' '} Be careful not to share the exported data with anyone. It contains your unique verification keys. Import profile data Import your Chitchatter profile that was previously exported from another browser or device. { handleImportSettingsClick(results) }, }} > Delete all profile data Be careful with this. This will cause your user name to change from{' '} {userId} {' '} to a new, randomly-assigned name. It will also reset all of your saved Chitchatter application preferences. Chitchatter only stores user preferences and never message content of any kind. This preference data is only stored locally on your device and not a server. ) }