forked from snapshot-labs/snapshot-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseExtendedSpaces.ts
58 lines (50 loc) · 1.55 KB
/
useExtendedSpaces.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { ref, computed } from 'vue';
import { SPACES_QUERY } from '@/helpers/queries';
import { useApolloQuery } from '@/composables/useApolloQuery';
import { ExtendedSpace } from '@/helpers/interfaces';
import { mapOldPluginNames } from '@/helpers/utils';
const extentedSpaces = ref<ExtendedSpace[]>([]);
export function useExtendedSpaces() {
const loading = ref(false);
const { apolloQuery } = useApolloQuery();
async function loadExtentedSpaces(id_in: string[] = []) {
const filteredLoadedSpaces = id_in.filter(
id => !extentedSpaces.value?.find(space => space.id === id)
);
if (!filteredLoadedSpaces.length) return;
loading.value = true;
try {
const spaces = await apolloQuery(
{
query: SPACES_QUERY,
variables: {
id_in: filteredLoadedSpaces
}
},
'spaces'
);
const mappedSpaces = spaces.map(mapOldPluginNames);
extentedSpaces.value = [...extentedSpaces.value, ...mappedSpaces];
loading.value = false;
} catch (e) {
loading.value = false;
console.error(e);
return e;
}
}
const reloadSpace = (id: string) => {
const spaceToReload = extentedSpaces.value?.find(space => space.id === id);
if (spaceToReload) {
extentedSpaces.value = extentedSpaces.value.filter(
space => space.id !== id
);
loadExtentedSpaces([id]);
}
};
return {
loadExtentedSpaces,
reloadSpace,
extentedSpaces: computed(() => extentedSpaces.value),
spaceLoading: computed(() => loading.value)
};
}