import { useCallback, useState } from 'react'; import { useDispatch } from 'react-redux'; import { updateDeleteAccountModal } from '../../state/ducks/modalDialog'; import { SessionWrapperModal } from '../SessionWrapperModal'; import { SessionButton, SessionButtonColor, SessionButtonType } from '../basic/SessionButton'; import { SpacerLG } from '../basic/Text'; import { SessionSpinner } from '../loading'; import { deleteEverythingAndNetworkData, sendConfigMessageAndDeleteEverything, } from '../../util/accountManager'; import { SessionRadioGroup } from '../basic/SessionRadioGroup'; import { Localizer } from '../basic/Localizer'; const DEVICE_ONLY = 'device_only'; const DEVICE_AND_NETWORK = 'device_and_network'; type DeleteModes = typeof DEVICE_ONLY | typeof DEVICE_AND_NETWORK; const DescriptionBeforeAskingConfirmation = (props: { deleteMode: DeleteModes; setDeleteMode: (deleteMode: DeleteModes) => void; }) => { const { deleteMode, setDeleteMode } = props; return ( <> { if (value === DEVICE_ONLY || value === DEVICE_AND_NETWORK) { setDeleteMode(value); } }} items={[ { label: window.i18n('clearDeviceOnly'), value: DEVICE_ONLY }, { label: window.i18n('clearDeviceAndNetwork'), value: 'device_and_network' }, ]} /> ); }; const DescriptionWhenAskingConfirmation = (props: { deleteMode: DeleteModes }) => { return ( {props.deleteMode === 'device_and_network' ? window.i18n('clearDeviceAndNetworkConfirm') : window.i18n('clearDeviceDescription')} ); }; export const DeleteAccountModal = () => { const [isLoading, setIsLoading] = useState(false); const [askingConfirmation, setAskingConfirmation] = useState(false); const [deleteMode, setDeleteMode] = useState(DEVICE_ONLY); const dispatch = useDispatch(); const onDeleteEverythingLocallyOnly = async () => { if (!isLoading) { setIsLoading(true); try { window.log.warn('Deleting everything on device but keeping network data'); await sendConfigMessageAndDeleteEverything(); } catch (e) { window.log.warn(e); } finally { setIsLoading(false); } } }; const onDeleteEverythingAndNetworkData = async () => { if (!isLoading) { setIsLoading(true); try { window.log.warn('Deleting everything including network data'); await deleteEverythingAndNetworkData(); } catch (e) { window.log.warn(e); } finally { setIsLoading(false); } } }; /** * Performs specified on close action then removes the modal. */ const onClickCancelHandler = useCallback(() => { dispatch(updateDeleteAccountModal(null)); }, [dispatch]); return ( {askingConfirmation ? ( ) : ( )}
{ if (!askingConfirmation) { setAskingConfirmation(true); return; } if (deleteMode === 'device_only') { void onDeleteEverythingLocallyOnly(); } else if (deleteMode === 'device_and_network') { void onDeleteEverythingAndNetworkData(); } }} disabled={isLoading} /> { dispatch(updateDeleteAccountModal(null)); }} disabled={isLoading} />
); };