sync files
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { createMMKV } from 'react-native-mmkv';
|
||||
import { localFileRegistry } from '../services/localFileRegistry';
|
||||
import { safDirectory } from '../services/safDirectory';
|
||||
import { useDeviceFiles } from './useDeviceFiles';
|
||||
|
||||
const syncStateStorage = createMMKV({ id: 'vaultdrop-sync-state' });
|
||||
|
||||
export function getIsSyncing(): boolean {
|
||||
return syncStateStorage.getString('is_syncing') === 'true';
|
||||
}
|
||||
|
||||
export function setIsSyncing(value: boolean) {
|
||||
syncStateStorage.set('is_syncing', value ? 'true' : 'false');
|
||||
}
|
||||
|
||||
export function useSyncQueue() {
|
||||
const [pendingCount, setPendingCount] = useState(0);
|
||||
const [isSyncing, setIsSyncingState] = useState(() => getIsSyncing());
|
||||
const { folders } = useDeviceFiles();
|
||||
|
||||
const refresh = useCallback(() => {
|
||||
const allFolders = safDirectory.getAll();
|
||||
const syncFolderIds = new Set(
|
||||
allFolders.filter((f) => f.syncMode !== 'none').map((f) => f.id)
|
||||
);
|
||||
|
||||
if (syncFolderIds.size === 0) {
|
||||
setPendingCount(0);
|
||||
setIsSyncingState(getIsSyncing());
|
||||
return;
|
||||
}
|
||||
|
||||
const registry = localFileRegistry.getAll();
|
||||
let count = 0;
|
||||
for (const entry of registry) {
|
||||
if (entry.backendFileId) continue;
|
||||
if (entry.syncStatus !== 'local') continue;
|
||||
if (entry.folderId && !syncFolderIds.has(entry.folderId)) continue;
|
||||
count++;
|
||||
}
|
||||
|
||||
setPendingCount(count);
|
||||
setIsSyncingState(getIsSyncing());
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
refresh();
|
||||
const interval = setInterval(refresh, 5000);
|
||||
return () => clearInterval(interval);
|
||||
}, [refresh]);
|
||||
|
||||
return { pendingCount, isSyncing, refresh };
|
||||
}
|
||||
Reference in New Issue
Block a user