-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathuse-spaces.ts
More file actions
73 lines (63 loc) · 2.04 KB
/
Copy pathuse-spaces.ts
File metadata and controls
73 lines (63 loc) · 2.04 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
62
63
64
65
66
67
68
69
70
71
72
73
import { Config, store } from '@graphprotocol/hypergraph';
import { useQuery } from '@tanstack/react-query';
import { useSelector } from '@xstate/store/react';
import { gql, request } from 'graphql-request';
import { useEffect } from 'react';
import { useHypergraphApp } from '../HypergraphAppContext.js';
const publicSpacesQueryDocument = gql`
query Spaces($memberId: UUID!) {
spaces(filter: {members: {some: {memberSpaceId: {is: $memberId}}}}) {
id
page {
name
}
}
}
`;
type PublicSpacesQueryResult = {
spaces: {
id: string;
page: {
name: string;
} | null;
}[];
};
export const useSpaces = (params: { mode: 'public' | 'private' }) => {
const identityAccountAddress = useSelector(store, (state) => state.context.identity?.accountAddress);
const privyIdentityAccountAddress = useSelector(store, (state) => state.context.privyIdentity?.accountAddress);
const accountAddress = identityAccountAddress ? identityAccountAddress : privyIdentityAccountAddress;
const publicResult = useQuery({
queryKey: ['hypergraph-public-spaces', params.mode],
queryFn: async () => {
const result = await request<PublicSpacesQueryResult>(
`${Config.getApiOrigin()}/v2/graphql`,
publicSpacesQueryDocument,
{
accountAddress,
},
);
return result?.spaces
? result.spaces.map((space) => ({
id: space.id,
name: space.page?.name,
}))
: [];
},
enabled: params.mode === 'public' && !!accountAddress,
});
const { isConnecting, listSpaces } = useHypergraphApp();
useEffect(() => {
if (params.mode === 'private' && !isConnecting) {
listSpaces();
}
}, [params.mode, listSpaces, isConnecting]);
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;
};