insomnia/packages/insomnia-app/app/ui/hooks/project.ts

37 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-08-10 23:57:13 +00:00
import { useCallback } from 'react';
import { useSelector } from 'react-redux';
2021-08-10 23:57:13 +00:00
import { useAsync } from 'react-use';
2021-07-22 23:04:56 +00:00
import { database } from '../../common/database';
import { initializeProjectFromTeam } from '../../sync/vcs/initialize-model-from';
2021-07-22 23:04:56 +00:00
import { VCS } from '../../sync/vcs/vcs';
import { selectIsLoggedIn, selectSettings } from '../redux/selectors';
import { useSafeState } from './use-safe-state';
export const useRemoteProjects = (vcs?: VCS) => {
const [loading, setLoading] = useSafeState(false);
const isLoggedIn = useSelector(selectIsLoggedIn);
const { incognitoMode } = useSelector(selectSettings);
const refresh = useCallback(async () => {
if (vcs && isLoggedIn) {
setLoading(true);
const teams = await vcs.teams();
const projects = await Promise.all(teams.map(initializeProjectFromTeam));
await database.batchModifyDocs({ upsert: projects });
setLoading(false);
}
}, [vcs, setLoading, isLoggedIn]);
// If the refresh callback changes, refresh
useAsync(async () => {
if (!incognitoMode) {
await refresh();
}
}, [refresh, incognitoMode]);
return { loading, refresh };
};