// @flow import React from 'react'; import Button from 'component/button'; import { getSavedPassword } from 'util/saved-passwords'; import Card from 'component/common/card'; import { withRouter } from 'react-router'; import Spinner from 'component/spinner'; import { Lbry } from 'lbry-redux'; import ErrorText from 'component/common/error-text'; import I18nMessage from 'component/i18nMessage'; type Props = { setSyncEnabled: boolean => void, syncEnabled: boolean, getSyncError: ?string, getSyncPending: boolean, getSync: (pw: string, cb: () => void) => void, checkSync: () => void, closeModal: () => void, updatePreferences: () => void, mode: string, }; const ENABLE_MODE = 'enable'; // steps const FETCH_FOR_ENABLE = 'fetch-for-enable'; const FETCH_FOR_DISABLE = 'fetch-for-disable'; const CONFIRM = 'confirm'; const INITIAL = 'initial'; const ERROR = 'error'; const SHARED_KEY = 'shared'; const LOCAL_KEY = 'local'; function SyncEnableFlow(props: Props) { const { setSyncEnabled, getSyncError, getSyncPending, getSync, checkSync, mode, closeModal, updatePreferences, } = props; const [step, setStep] = React.useState(INITIAL); const [prefDict, setPrefDict]: [any, (any) => void] = React.useState(); const [error, setError] = React.useState(); const [password, setPassword] = React.useState(''); const handleSyncToggle = async () => { const shared = prefDict.shared; const local = prefDict.local; let finalPrefs; if (shared && local) { if (mode === ENABLE_MODE) { finalPrefs = makeMergedPrefs(local, shared); } else { finalPrefs = makeMergedPrefs(shared, local); } } else { finalPrefs = local || shared || null; } // set busy (disable button) if (finalPrefs) { await Lbry.preference_set({ key: mode === ENABLE_MODE ? SHARED_KEY : LOCAL_KEY, value: finalPrefs }); } await setSyncEnabled(mode === ENABLE_MODE); await updatePreferences(); closeModal(); }; const makeMergedPrefs = (alt, base) => { let finalPrefs = base; let baseData = base.value; let altData = alt.value; if (!altData) { return base; } let mergedBlockListSet = new Set(baseData.blocked || []); let mergedSubscriptionsSet = new Set(baseData.subscriptions || []); let mergedTagsSet = new Set(baseData.tags || []); const altBlocklist = altData.blocked || []; const altSubscriptions = altData.subscriptions || []; const altTags = altData.tags || []; if (altBlocklist.length) { altBlocklist.forEach(el => mergedBlockListSet.add(el)); } if (altSubscriptions.length) { altSubscriptions.forEach(el => mergedSubscriptionsSet.add(el)); } if (altTags.length) { altTags.forEach(el => mergedTagsSet.add(el)); } baseData.blocked = Array.from(mergedBlockListSet); baseData.subscriptions = Array.from(mergedSubscriptionsSet); baseData.tags = Array.from(mergedTagsSet); finalPrefs.value = baseData; return finalPrefs; }; React.useEffect(() => { if (mode) { checkSync(); if (mode === ENABLE_MODE) { getSavedPassword().then(pw => { setPassword(pw); setStep(FETCH_FOR_ENABLE); }); } else { setStep(FETCH_FOR_DISABLE); } } }, [mode, setPassword]); React.useEffect(() => { if (step === FETCH_FOR_ENABLE) { getSync(password, (e, hasChanged) => { if (e) { setStep(ERROR); setError(e && e.message ? e.message : e); } else { Lbry.preference_get().then(result => { const prefs = {}; if (result[SHARED_KEY]) prefs[SHARED_KEY] = result[SHARED_KEY]; if (result[LOCAL_KEY]) prefs[LOCAL_KEY] = result[LOCAL_KEY]; setPrefDict(prefs); setStep(CONFIRM); }); } }); } if (step === FETCH_FOR_DISABLE) { Lbry.preference_get().then(result => { const prefs = {}; if (result[SHARED_KEY]) prefs[SHARED_KEY] = result[SHARED_KEY]; if (result[LOCAL_KEY]) prefs[LOCAL_KEY] = result[LOCAL_KEY]; setPrefDict(prefs); setStep(CONFIRM); }); } }, [step, setPrefDict, setStep, password]); if (getSyncPending) { return (