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

33 lines
1015 B
TypeScript

import { useCallback } from 'react';
import { useSelector } from 'react-redux';
import { useAsync } from 'react-use';
import { database } from '../../common/database';
import { initializeProjectFromTeam } from '../../sync/vcs/initialize-model-from';
import { VCS } from '../../sync/vcs/vcs';
import { selectIsLoggedIn } from '../redux/selectors';
import { useSafeState } from './use-safe-state';
export const useRemoteProjects = (vcs?: VCS) => {
const [loading, setLoading] = useSafeState(false);
const isLoggedIn = useSelector(selectIsLoggedIn);
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(refresh, [refresh]);
return { loading, refresh };
};