diff --git a/native/backup/backup-handler.js b/native/backup/backup-handler.js --- a/native/backup/backup-handler.js +++ b/native/backup/backup-handler.js @@ -1,25 +1,15 @@ // @flow -import AsyncStorage from '@react-native-async-storage/async-storage'; import * as React from 'react'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; import { accountHasPassword } from 'lib/shared/account-utils.js'; -import { getMessageForException } from 'lib/utils/errors.js'; -import { BACKUP_HASH_STORAGE_KEY } from './constants.js'; -import { convertObjToBytes } from './conversion-utils.js'; -import { useClientBackup } from './use-client-backup.js'; -import { commUtilsModule } from '../native-modules.js'; +import { commCoreModule } from '../native-modules.js'; import { useSelector } from '../redux/redux-utils.js'; -import Alert from '../utils/alert.js'; import { useStaffCanSee } from '../utils/staff-utils.js'; function BackupHandler(): null { - const userStore = useSelector(state => state.userStore); - const currentUserID = useSelector( - state => state.currentUserInfo && state.currentUserInfo.id, - ); const isBackupEnabled = useSelector( state => state.localSettings.isBackupEnabled, ); @@ -29,62 +19,27 @@ accountHasPassword(state.currentUserInfo), ); - const { uploadBackupProtocol } = useClientBackup(); - React.useEffect(() => { - if (!isBackupEnabled) { - AsyncStorage.removeItem(BACKUP_HASH_STORAGE_KEY); + if (!staffCanSee || !isAccountWithPassword) { + return; } - }, [isBackupEnabled]); - React.useEffect(() => { - void (async () => { - if ( - !isBackupEnabled || - !loggedIn || - !staffCanSee || - !isAccountWithPassword - ) { - return; + if (isBackupEnabled && loggedIn) { + try { + commCoreModule.startBackupHandler(); + } catch (err) { + console.log('Error starting backup handler:', err); } - - const userData = { userStore }; - const userDataBytes = convertObjToBytes(userData); - const currentBackupHash = commUtilsModule.sha256(userDataBytes.buffer); - - const mostRecentlyUploadedBackupHash = await AsyncStorage.getItem( - BACKUP_HASH_STORAGE_KEY, - ); - - if ( - !mostRecentlyUploadedBackupHash || - currentBackupHash !== mostRecentlyUploadedBackupHash - ) { + } else { + void (async () => { try { - await uploadBackupProtocol(userData); - await AsyncStorage.setItem( - BACKUP_HASH_STORAGE_KEY, - currentBackupHash, - ); - } catch (e) { - const message = String(getMessageForException(e)); - console.error(`Backup uploading error: ${message}`); - Alert.alert( - 'Backup protocol info', - `Backup uploading error: ${message}`, - ); + await commCoreModule.stopBackupHandler(); + } catch (err) { + console.log('Error stopping backup handler:', err); } - } - })(); - }, [ - currentUserID, - isBackupEnabled, - staffCanSee, - loggedIn, - uploadBackupProtocol, - userStore, - isAccountWithPassword, - ]); + })(); + } + }, [isBackupEnabled, staffCanSee, loggedIn, isAccountWithPassword]); return null; } diff --git a/native/backup/constants.js b/native/backup/constants.js deleted file mode 100644 --- a/native/backup/constants.js +++ /dev/null @@ -1,3 +0,0 @@ -// @flow - -export const BACKUP_HASH_STORAGE_KEY = 'RECENT_USER_DATA_HASH';