import { useModalSearchParams } from '@/hooks/useModalSearchParams' import { Button } from '@/shared/Button/Button' import { Modal } from '@/shared/Modal/Modal' import { MODAL_PARAMS_KEYS } from '@/types/modal' import { Box, CircularProgress, IconButton, Stack, Typography, } from '@mui/material' import { StyledButton, StyledSettingContainer, StyledSynchedText, } from './styled' import { SectionTitle } from '@/shared/SectionTitle/SectionTitle' import { CheckmarkIcon } from '@/assets' import { Input } from '@/shared/Input/Input' import VisibilityOffOutlinedIcon from '@mui/icons-material/VisibilityOffOutlined' import VisibilityOutlinedIcon from '@mui/icons-material/VisibilityOutlined' import { ChangeEvent, FC, useState } from 'react' import { Checkbox } from '@/shared/Checkbox/Checkbox' import { useEnqueueSnackbar } from '@/hooks/useEnqueueSnackbar' import { swicCall } from '@/modules/swic' import { useParams } from 'react-router-dom' import { dbi } from '@/modules/db' type ModalSettingsProps = { isSynced: boolean } export const ModalSettings: FC = ({ isSynced }) => { const { getModalOpened, handleClose } = useModalSearchParams() const { npub = '' } = useParams<{ npub: string }>() const notify = useEnqueueSnackbar() const isModalOpened = getModalOpened(MODAL_PARAMS_KEYS.SETTINGS) const handleCloseModal = handleClose(MODAL_PARAMS_KEYS.SETTINGS) const [enteredPassword, setEnteredPassword] = useState('') const [isPasswordShown, setIsPasswordShown] = useState(false) const [isPasswordInvalid, setIsPasswordInvalid] = useState(false) const [isChecked, setIsChecked] = useState(false) const [isLoading, setIsLoading] = useState(false) const handlePasswordChange = (e: ChangeEvent) => { setIsPasswordInvalid(false) setEnteredPassword(e.target.value) } const handlePasswordTypeChange = () => setIsPasswordShown((prevState) => !prevState) const onClose = () => { handleCloseModal() setEnteredPassword('') setIsPasswordInvalid(false) } const handleChangeCheckbox = (e: unknown, checked: boolean) => { setIsChecked(checked) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsPasswordInvalid(false) if (enteredPassword.trim().length < 6) { return setIsPasswordInvalid(true) } try { setIsLoading(true) await swicCall('saveKey', npub, enteredPassword) notify('Key saved', 'success') dbi.addSynced(npub) // Sync npub setEnteredPassword('') setIsPasswordInvalid(false) setIsLoading(false) } catch (error) { setIsPasswordInvalid(false) setIsLoading(false) } } return ( Cloud sync {isSynced && ( Synched )} Use this login on multiple devices {isSynced ? ( This uploads your private key, encrypted by your password, to Nsec App's server. ) : ( <> {isPasswordShown ? ( ) : ( )} } type={isPasswordShown ? 'text' : 'password'} onChange={handlePasswordChange} value={enteredPassword} helperText={ isPasswordInvalid ? 'Invalid password' : '' } placeholder='Enter a password' helperTextProps={{ sx: { '&.helper_text': { color: 'red', }, }, }} disabled={!isChecked} /> Sync{' '} {isLoading && ( )} )} ) }