-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
space.ts
32 lines (25 loc) · 945 Bytes
/
space.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import * as models from '../../models';
import { database } from '../../common/database';
import VCS from '../../sync/vcs';
import { useCallback, useEffect, useState } from 'react';
import { isLoggedIn } from '../../account/session';
import { Space } from '../../models/space';
export const useRemoteSpaces = (vcs?: VCS) => {
const [loading, setLoading] = useState(false);
const refresh = useCallback(async () => {
if (vcs && isLoggedIn()) {
setLoading(true);
const teams = await vcs.teams();
const spaces = await Promise.all(teams.map(team => models.initModel<Space>(
models.space.type,
{ _id: `${models.space.prefix}_${team.id}`, remoteId: team.id, name: team.name },
)));
await database.batchModifyDocs({ upsert: spaces });
setLoading(false);
}
}, [vcs]);
useEffect(() => {
(async () => { await refresh(); })();
}, [refresh]);
return { loading, refresh };
};