Skip to content

Commit

Permalink
feat: waits for document drive server to be loaded before checking de…
Browse files Browse the repository at this point in the history
…fault drive is added
  • Loading branch information
acaldas committed May 7, 2024
1 parent 23cd72d commit fc10ee8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/hooks/useDocumentDriveServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function useDocumentDriveServer(

const getDocumentModel = useGetDocumentModel();

const [documentDrives, refreshDocumentDrives] = useDocumentDrives(server);
const [documentDrives, refreshDocumentDrives, , documentDrivesStatus] = useDocumentDrives(server);

async function openFile(drive: string, id: string) {
const document = await server.getDocument(drive, id);
Expand Down Expand Up @@ -429,7 +429,7 @@ export function useDocumentDriveServer(
driveId: string,
type: DriveType,
): Promise<SyncStatus | undefined> {
if (getIsLocalDrive({ type } as unknown as TreeItem)) return;
if (type === "LOCAL_DRIVE") return;
try {
return server.getSyncStatus(driveId);
} catch (error) {
Expand All @@ -456,6 +456,7 @@ export function useDocumentDriveServer(
return useMemo(
() => ({
documentDrives,
documentDrivesStatus,
addDocument,
openFile,
addFile,
Expand All @@ -479,6 +480,6 @@ export function useDocumentDriveServer(
onSyncStatus,
clearStorage,
}),
[documentDrives],
[documentDrives, documentDrivesStatus],
);
}
16 changes: 10 additions & 6 deletions src/hooks/useDocumentDrives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ const readWriteDocumentDrivesAtom = (server: IDocumentDriveServer) => () =>
},
);
// keeps track of document drives that have been initialized
export type IDrivesState = 'INITIAL' | 'LOADING' | 'LOADED' | 'ERROR';
export const documentDrivesInitializedMapAtomFamily = atomFamily(() =>
atom(false),
atom<IDrivesState>('INITIAL'),
);

// returns an array with the document drives of a
Expand Down Expand Up @@ -60,13 +61,15 @@ export function useDocumentDrives(server: IDocumentDriveServer) {

// if the server has not been initialized then
// fetches the drives for the first time
const [isInitialized, setIsInitialized] = useAtom(
const [status, setStatus] = useAtom(
documentDrivesInitializedMapAtomFamily(server),
);

if (!isInitialized) {
setIsInitialized(true);
refreshDocumentDrives();
if (status === 'INITIAL') {
setStatus('LOADING');
refreshDocumentDrives()
.then(() => setStatus('LOADED'))
.catch(() => setStatus('ERROR'));
}

const serverSubscribeUpdates = useCallback(() => {
Expand All @@ -92,7 +95,8 @@ export function useDocumentDrives(server: IDocumentDriveServer) {
documentDrives,
refreshDocumentDrives,
serverSubscribeUpdates,
status,
] as const,
[documentDrives],
[documentDrives, status],
);
}
6 changes: 3 additions & 3 deletions src/hooks/useLoadDefaultDrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { useFeatureFlag } from './useFeatureFlags';

export const useLoadDefaultDrive = () => {
const loading = useRef(false);
const { addRemoteDrive, documentDrives } = useDocumentDriveServer();
const { addRemoteDrive, documentDrives, documentDrivesStatus } = useDocumentDriveServer();
const {
setConfig,
config: { defaultDrive },
} = useFeatureFlag();

useEffect(() => {
if (defaultDrive && !defaultDrive.loaded && !loading.current) {
if (defaultDrive && documentDrivesStatus === "LOADED" && !defaultDrive.loaded && !loading.current) {
const isDriveAlreadyAdded = documentDrives.some(drive => {
return drive.state.local.triggers.some(
trigger => trigger.data?.url === defaultDrive.url,
Expand Down Expand Up @@ -56,7 +56,7 @@ export const useLoadDefaultDrive = () => {
.catch(console.error)
.finally(() => (loading.current = false));
}
}, [documentDrives, defaultDrive]);
}, [documentDrives, defaultDrive, documentDrivesStatus]);

return loading.current;
};

0 comments on commit fc10ee8

Please sign in to comment.