Skip to content

Commit 03f1d51

Browse files
authored
listing public spaces (#235)
1 parent 75f4eab commit 03f1d51

File tree

14 files changed

+92
-12
lines changed

14 files changed

+92
-12
lines changed

apps/connect/src/routes/authenticate.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,11 @@ function AuthenticateComponent() {
282282
nonce: appInfo.appNonce,
283283
ephemeralPublicKey: appInfo.ephemeralEncryptionPublicKey,
284284
appIdentityAddress: appIdentity.address,
285+
appIdentityAddressPrivateKey: appIdentity.addressPrivateKey,
286+
accountAddress: accountAddress,
285287
encryptionPrivateKey: appIdentity.encryptionPrivateKey,
286288
signaturePrivateKey: appIdentity.signaturePrivateKey,
287289
signaturePublicKey: appIdentity.signaturePublicKey,
288-
appIdentityAddressPrivateKey: appIdentity.addressPrivateKey,
289290
encryptionPublicKey: appIdentity.encryptionPublicKey,
290291
spaces: spacesData?.map((space) => ({ id: space.id })) ?? [],
291292
expiry: appInfo.expiry,
@@ -364,6 +365,7 @@ function AuthenticateComponent() {
364365
appIdentity: {
365366
address: newAppIdentity.address,
366367
addressPrivateKey: newAppIdentity.addressPrivateKey,
368+
accountAddress,
367369
encryptionPrivateKey: keys.encryptionPrivateKey,
368370
signaturePrivateKey: keys.signaturePrivateKey,
369371
encryptionPublicKey: newAppIdentity.encryptionPublicKey,
@@ -420,6 +422,7 @@ function AuthenticateComponent() {
420422
appIdentity: {
421423
address: decryptedIdentity.address,
422424
addressPrivateKey: decryptedIdentity.addressPrivateKey,
425+
accountAddress: decryptedIdentity.accountAddress,
423426
encryptionPrivateKey: decryptedIdentity.encryptionPrivateKey,
424427
signaturePrivateKey: decryptedIdentity.signaturePrivateKey,
425428
encryptionPublicKey: decryptedIdentity.encryptionPublicKey,

apps/events/src/routes/authenticate-success.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function RouteComponent() {
4444
setIdentity({
4545
address: parsedAuthParams.appIdentityAddress,
4646
addressPrivateKey: parsedAuthParams.appIdentityAddressPrivateKey,
47+
accountAddress: parsedAuthParams.accountAddress,
4748
signaturePublicKey: parsedAuthParams.signaturePublicKey,
4849
signaturePrivateKey: parsedAuthParams.signaturePrivateKey,
4950
encryptionPublicKey: parsedAuthParams.encryptionPublicKey,

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

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';

packages/hypergraph/src/connect/create-callback-params.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type CreateAuthUrlParams = {
88
appId: string;
99
appIdentityAddress: string;
1010
appIdentityAddressPrivateKey: string;
11+
accountAddress: string;
1112
signaturePublicKey: string;
1213
signaturePrivateKey: string;
1314
encryptionPublicKey: string;

packages/hypergraph/src/connect/identity-encryption.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,6 @@ export const decryptAppIdentity = async (
228228
signaturePrivateKey,
229229
address: appIdentityAddress,
230230
addressPrivateKey: appIdentityAddressPrivateKey,
231+
accountAddress,
231232
};
232233
};

packages/hypergraph/src/connect/parse-callback-params.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const parseCallbackParams = ({
5252
return Effect.succeed({
5353
appIdentityAddress: data.appIdentityAddress,
5454
appIdentityAddressPrivateKey: data.appIdentityAddressPrivateKey,
55+
accountAddress: data.accountAddress,
5556
signaturePublicKey: data.signaturePublicKey,
5657
signaturePrivateKey: data.signaturePrivateKey,
5758
encryptionPublicKey: data.encryptionPublicKey,

0 commit comments

Comments
 (0)