Skip to content

Commit 2403b28

Browse files
committed
listing public spaces
1 parent 75f4eab commit 2403b28

File tree

5 files changed

+72
-10
lines changed

5 files changed

+72
-10
lines changed

apps/events/src/routes/index.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Button } from '@/components/ui/button';
22
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
33
import { Input } from '@/components/ui/input';
44
import { store } from '@graphprotocol/hypergraph';
5-
import { useHypergraphApp } from '@graphprotocol/hypergraph-react';
5+
import { useHypergraphApp, useSpaces } from '@graphprotocol/hypergraph-react';
66
import { Link, createFileRoute } from '@tanstack/react-router';
77
import { useSelector } from '@xstate/store/react';
88
import { useEffect, useState } from 'react';
@@ -12,6 +12,7 @@ export const Route = createFileRoute('/')({
1212
});
1313

1414
function Index() {
15+
const { data: publicSpaces } = useSpaces({ mode: 'public' });
1516
const spaces = useSelector(store, (state) => state.context.spaces);
1617
const [spaceName, setSpaceName] = useState('');
1718

@@ -89,6 +90,8 @@ function Index() {
8990
Create space
9091
</Button>
9192
</div>
93+
94+
<h2 className="text-lg font-bold">Private Spaces</h2>
9295
<ul className="flex flex-col gap-2">
9396
{spaces.length === 0 && <div>No spaces</div>}
9497
{spaces.map((space) => {
@@ -106,6 +109,21 @@ function Index() {
106109
})}
107110
</ul>
108111

112+
<h2 className="text-lg font-bold">Public Spaces</h2>
113+
<ul className="flex flex-col gap-2">
114+
{publicSpaces?.map((space) => {
115+
return (
116+
<li key={space.id}>
117+
<Card>
118+
<CardHeader>
119+
<CardTitle>{space.name}</CardTitle>
120+
</CardHeader>
121+
</Card>
122+
</li>
123+
);
124+
})}
125+
</ul>
126+
109127
<div className="flex flex-col gap-2">
110128
<h2 className="text-lg font-bold">Account Inboxes</h2>
111129
<Button

packages/hypergraph-react/src/HypergraphAppContext.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
Utils,
2222
store,
2323
} from '@graphprotocol/hypergraph';
24+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
2425
import { useSelector as useSelectorStore } from '@xstate/store/react';
2526
import { Effect, Exit } from 'effect';
2627
import * as Schema from 'effect/Schema';
@@ -38,6 +39,8 @@ import type { Address } from 'viem';
3839

3940
const decodeResponseMessage = Schema.decodeUnknownEither(Messages.ResponseMessage);
4041

42+
const queryClient = new QueryClient();
43+
4144
export type HypergraphAppCtx = {
4245
// auth related
4346
logout(): void;
@@ -1352,7 +1355,9 @@ export function HypergraphAppProvider({
13521355
ensureSpaceInbox: ensureSpaceInboxForContext,
13531356
}}
13541357
>
1355-
<RepoContext.Provider value={repo}>{children}</RepoContext.Provider>
1358+
<QueryClientProvider client={queryClient}>
1359+
<RepoContext.Provider value={repo}>{children}</RepoContext.Provider>
1360+
</QueryClientProvider>
13561361
</HypergraphAppContext.Provider>
13571362
);
13581363
}

packages/hypergraph-react/src/HypergraphSpaceContext.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import type { AnyDocumentId, DocHandle, Repo } from '@automerge/automerge-repo';
44
import { useRepo } from '@automerge/automerge-repo-react-hooks';
55
import { Entity, Utils } from '@graphprotocol/hypergraph';
6-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
76
import * as Schema from 'effect/Schema';
87
import { type ReactNode, createContext, useContext, useMemo, useRef, useState, useSyncExternalStore } from 'react';
98

@@ -25,8 +24,6 @@ function useHypergraphSpaceInternal() {
2524
return context as HypergraphContext;
2625
}
2726

28-
const queryClient = new QueryClient();
29-
3027
export function HypergraphSpaceProvider({ space, children }: { space: string; children: ReactNode }) {
3128
const repo = useRepo();
3229
const ref = useRef<HypergraphContext | undefined>(undefined);
@@ -44,11 +41,7 @@ export function HypergraphSpaceProvider({ space, children }: { space: string; ch
4441
};
4542
}
4643

47-
return (
48-
<QueryClientProvider client={queryClient}>
49-
<HypergraphReactContext.Provider value={current}>{children}</HypergraphReactContext.Provider>
50-
</QueryClientProvider>
51-
);
44+
return <HypergraphReactContext.Provider value={current}>{children}</HypergraphReactContext.Provider>;
5245
}
5346

5447
export function useCreateEntity<const S extends Entity.AnyNoContext>(type: S) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { useQuery } from '@tanstack/react-query';
2+
import { gql, request } from 'graphql-request';
3+
import { GEO_API_TESTNET_ENDPOINT } from '../internal/constants.js';
4+
5+
const publicSpacesQueryDocument = gql`
6+
query Spaces($accountAddress: String!) {
7+
spaces(filter: {
8+
member: { is: $accountAddress }
9+
}) {
10+
id
11+
spaceAddress
12+
entity {
13+
name
14+
}
15+
}
16+
}
17+
`;
18+
19+
type PublicSpacesQueryResult = {
20+
spaces: {
21+
id: string;
22+
spaceAddress: string;
23+
entity: {
24+
name: string;
25+
};
26+
}[];
27+
};
28+
29+
export const useSpaces = (params: { mode: 'public' }) => {
30+
return useQuery({
31+
queryKey: ['hypergraph-spaces', params.mode],
32+
queryFn: async () => {
33+
const result = await request<PublicSpacesQueryResult>(GEO_API_TESTNET_ENDPOINT, publicSpacesQueryDocument, {
34+
accountAddress: '0xBE0298aF8D440bEFA78E7e8A538D8ecBFF06bfC7',
35+
});
36+
return result?.spaces
37+
? result.spaces.map((space) => ({
38+
id: space.id,
39+
name: space.entity.name,
40+
spaceAddress: space.spaceAddress,
41+
}))
42+
: [];
43+
},
44+
});
45+
};

packages/hypergraph-react/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { PublishDiff } from './components/publish-diff/publish-diff.js';
22
export { createWalletClient } from './create-wallet-client.js';
3+
export { useSpaces } from './hooks/use-spaces.js';
34
export { useExternalAccountInbox } from './hooks/useExternalAccountInbox.js';
45
export { useExternalSpaceInbox } from './hooks/useExternalSpaceInbox.js';
56
export { useOwnAccountInbox } from './hooks/useOwnAccountInbox.js';

0 commit comments

Comments
 (0)