2021-06-04 07:20:16 +00:00
|
|
|
|
2021-08-10 23:57:13 +00:00
|
|
|
import { useCallback } from 'react';
|
2021-08-05 23:30:31 +00:00
|
|
|
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';
|
2021-08-20 15:12:36 +00:00
|
|
|
import { initializeProjectFromTeam } from '../../sync/vcs/initialize-model-from';
|
2021-07-22 23:04:56 +00:00
|
|
|
import { VCS } from '../../sync/vcs/vcs';
|
2021-10-14 14:59:45 +00:00
|
|
|
import { selectIsLoggedIn, selectSettings } from '../redux/selectors';
|
2021-06-23 16:40:48 +00:00
|
|
|
import { useSafeState } from './use-safe-state';
|
2021-06-04 07:20:16 +00:00
|
|
|
|
2021-08-20 15:12:36 +00:00
|
|
|
export const useRemoteProjects = (vcs?: VCS) => {
|
2021-06-23 16:40:48 +00:00
|
|
|
const [loading, setLoading] = useSafeState(false);
|
2021-08-05 23:30:31 +00:00
|
|
|
const isLoggedIn = useSelector(selectIsLoggedIn);
|
2021-10-14 14:59:45 +00:00
|
|
|
const { incognitoMode } = useSelector(selectSettings);
|
2021-06-04 07:20:16 +00:00
|
|
|
|
|
|
|
const refresh = useCallback(async () => {
|
2021-08-05 23:30:31 +00:00
|
|
|
if (vcs && isLoggedIn) {
|
2021-06-04 07:20:16 +00:00
|
|
|
setLoading(true);
|
|
|
|
|
|
|
|
const teams = await vcs.teams();
|
2021-08-20 15:12:36 +00:00
|
|
|
const projects = await Promise.all(teams.map(initializeProjectFromTeam));
|
|
|
|
await database.batchModifyDocs({ upsert: projects });
|
2021-06-04 07:20:16 +00:00
|
|
|
|
|
|
|
setLoading(false);
|
|
|
|
}
|
2021-08-05 23:30:31 +00:00
|
|
|
}, [vcs, setLoading, isLoggedIn]);
|
2021-06-04 07:20:16 +00:00
|
|
|
|
2021-06-15 04:21:06 +00:00
|
|
|
// If the refresh callback changes, refresh
|
2021-10-14 14:59:45 +00:00
|
|
|
useAsync(async () => {
|
|
|
|
if (!incognitoMode) {
|
|
|
|
await refresh();
|
|
|
|
}
|
|
|
|
}, [refresh, incognitoMode]);
|
2021-06-04 07:20:16 +00:00
|
|
|
return { loading, refresh };
|
|
|
|
};
|