import { getGetServerSideProps } from '@/api/ssrApollo' import Layout from '@/components/layout' import { SettingsHeader } from '../index' import { useVaultConfigurator } from '@/components/vault/use-vault-configurator' import { useMe } from '@/components/me' import { Button, InputGroup } from 'react-bootstrap' import bip39Words from '@/lib/bip39-words' import { Form, PasswordInput, SubmitButton } from '@/components/form' import { deviceSyncSchema } from '@/lib/validate' import RefreshIcon from '@/svgs/refresh-line.svg' import { useCallback, useEffect, useState } from 'react' import { useToast } from '@/components/toast' import { useWallets } from '@/wallets/index' export const getServerSideProps = getGetServerSideProps({ authRequired: true }) export default function DeviceSync ({ ssrData }) { const { me } = useMe() const { onVaultKeySet, beforeDisconnectVault } = useWallets() const { key, setVaultKey, clearVault, disconnectVault } = useVaultConfigurator({ onVaultKeySet, beforeDisconnectVault }) const [passphrase, setPassphrase] = useState() const setSeedPassphrase = useCallback(async (passphrase) => { await setVaultKey(passphrase) setPassphrase(passphrase) }, [setVaultKey]) const enabled = !!me?.privates?.vaultKeyHash const connected = !!key return (
{ (connected && passphrase && ) || (connected && ) || (enabled && ) || }
) } function Connect ({ passphrase }) { return (

Connect other devices

On your other devices, navigate to device sync settings and enter this exact passphrase.

Once you leave this page, this passphrase cannot be shown again. Connect all the devices you plan to use or write this passphrase down somewhere safe.

) } function Connected ({ disconnectVault }) { return (

Device sync is enabled!

Sensitive data on this device is now securely synced between all connected devices.

Disconnect to prevent this device from syncing data or to reset your passphrase.

) } function Enabled ({ setVaultKey, clearVault }) { const toaster = useToast() return (

Device sync is enabled

This device is not connected. Enter or scan your passphrase to connect. If you've lost your passphrase you may reset it.

{ try { await setVaultKey(passphrase) } catch (e) { console.error(e) toaster.danger('error setting vault key') } }} >
enable
) } const generatePassphrase = (n = 12) => { const rand = new Uint32Array(n) window.crypto.getRandomValues(rand) return Array.from(rand).map(i => bip39Words[i % bip39Words.length]).join(' ') } function Setup ({ setSeedPassphrase }) { const [passphrase, setPassphrase] = useState() const toaster = useToast() const newPassphrase = useCallback(() => { setPassphrase(() => generatePassphrase(12)) }, []) useEffect(() => { setPassphrase(() => generatePassphrase(12)) }, []) return (

Enable device sync

Enable secure sync of sensitive data (like wallet credentials) between your devices.

After enabled, your passphrase can be used to connect other devices.

{ try { await setSeedPassphrase(passphrase) } catch (e) { console.error(e) toaster.danger('error setting passphrase') } }} > } />
enable
) }