-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathuse-spaces.ts
More file actions
61 lines (55 loc) · 1.61 KB
/
Copy pathuse-spaces.ts
File metadata and controls
61 lines (55 loc) · 1.61 KB
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
59
60
61
import { store } from '@graphprotocol/hypergraph';
import { useQuery } from '@tanstack/react-query';
import { useSelector } from '@xstate/store/react';
import { gql, request } from 'graphql-request';
import { GEO_API_TESTNET_ENDPOINT } from '../internal/constants.js';
const publicSpacesQueryDocument = gql`
query Spaces($accountAddress: String!) {
spaces(filter: {
member: { is: $accountAddress }
}) {
id
spaceAddress
entity {
name
}
}
}
`;
type PublicSpacesQueryResult = {
spaces: {
id: string;
spaceAddress: string;
entity: {
name: string;
};
}[];
};
export const useSpaces = (params: { mode: 'public' | 'private' }) => {
const accountAddress = useSelector(store, (state) => state.context.identity?.accountAddress);
const publicResult = useQuery({
queryKey: ['hypergraph-spaces', params.mode],
queryFn: async () => {
const result = await request<PublicSpacesQueryResult>(GEO_API_TESTNET_ENDPOINT, publicSpacesQueryDocument, {
accountAddress,
});
return result?.spaces
? result.spaces.map((space) => ({
id: space.id,
name: space.entity.name,
spaceAddress: space.spaceAddress,
}))
: [];
},
enabled: params.mode === 'public' && !!accountAddress,
});
const spaces = useSelector(store, (state) => state.context.spaces);
const spacesLoadingIsPending = useSelector(store, (state) => state.context.spacesLoadingIsPending);
if (params.mode === 'private') {
return {
data: spaces,
isPending: spacesLoadingIsPending,
};
}
return publicResult;
};