Skip to content

Commit 3e80547

Browse files
authored
query name in public use space (#288)
1 parent c0a5857 commit 3e80547

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

apps/events/src/components/playground.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
_useDeleteEntityPublic,
44
useHypergraphApp,
55
useQuery,
6+
useSpace,
67
} from '@graphprotocol/hypergraph-react';
78
import { useState } from 'react';
89
import { Event } from '../schema';
@@ -21,18 +22,16 @@ export const Playground = () => {
2122
const [isCreating, setIsCreating] = useState(false);
2223
const { getSmartSessionClient } = useHypergraphApp();
2324

24-
const deleteEntity = _useDeleteEntityPublic(Event, {
25-
space: 'a57cd482-6dd3-4ba3-ac44-e2e8ea7a2862',
26-
});
25+
const { name, id: spaceId } = useSpace({ mode: 'public' });
2726

28-
const createEntity = _useCreateEntityPublic(Event, {
29-
space: 'a57cd482-6dd3-4ba3-ac44-e2e8ea7a2862',
30-
});
27+
const deleteEntity = _useDeleteEntityPublic(Event, { space: spaceId });
28+
const createEntity = _useCreateEntityPublic(Event, { space: spaceId });
3129

3230
console.log({ isLoading, isError, data });
3331

3432
return (
3533
<div>
34+
<h2 className="text-lg font-bold">Space: {name}</h2>
3635
{isLoading && <div>Loading...</div>}
3736
{isError && <div>Error</div>}
3837
<Button

apps/events/src/routes/playground.lazy.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ export const Route = createLazyFileRoute('/playground')({
99
});
1010

1111
function RouteComponent() {
12+
const space = 'a57cd482-6dd3-4ba3-ac44-e2e8ea7a2862';
1213
return (
13-
<HypergraphSpaceProvider space="a57cd482-6dd3-4ba3-ac44-e2e8ea7a2862">
14+
<HypergraphSpaceProvider space={space}>
1415
<div className="flex flex-col gap-4 max-w-(--breakpoint-sm) mx-auto py-8">
1516
<h1 className="text-2xl font-bold">Playground</h1>
1617
<Playground />
17-
<CreatePropertiesAndTypesEvent space="a57cd482-6dd3-4ba3-ac44-e2e8ea7a2862" />
18-
<CreateEvents space="a57cd482-6dd3-4ba3-ac44-e2e8ea7a2862" />
18+
<CreatePropertiesAndTypesEvent space={space} />
19+
<CreateEvents space={space} />
1920
</div>
2021
</HypergraphSpaceProvider>
2122
);

packages/hypergraph-react/src/HypergraphSpaceContext.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
useSyncExternalStore,
1515
} from 'react';
1616
import { useHypergraphApp } from './HypergraphAppContext.js';
17+
import { usePublicSpace } from './internal/use-public-space.js';
1718

1819
// TODO space can be undefined
1920
export type HypergraphContext = { space: string };
@@ -64,8 +65,9 @@ export function useSpace(options: { space?: string; mode: 'private' | 'public' }
6465
const spaceId = spaceIdFromParams ?? spaceIdFromContext;
6566
const handle = useSubscribeToSpaceAndGetHandle({ spaceId, enabled: options.mode === 'private' });
6667
const ready = options.mode === 'public' ? true : handle ? handle.isReady() : false;
67-
const space = useSelector(store, (state) => state.context.spaces.find((space) => space.id === spaceId));
68-
return { ready, name: space?.name, id: spaceId };
68+
const privateSpace = useSelector(store, (state) => state.context.spaces.find((space) => space.id === spaceId));
69+
const publicSpace = usePublicSpace({ spaceId, enabled: options.mode === 'public' });
70+
return { ready, name: options.mode === 'private' ? privateSpace?.name : publicSpace?.name, id: spaceId };
6971
}
7072

7173
export function useCreateEntity<const S extends Entity.AnyNoContext>(type: S, options?: { space?: string }) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 spaceQueryDocument = gql`
6+
query Space($spaceId: String!) {
7+
space(id: $spaceId) {
8+
entity {
9+
name
10+
}
11+
}
12+
}
13+
`;
14+
15+
type SpaceQueryResult = {
16+
space: {
17+
entity: {
18+
name: string;
19+
};
20+
} | null;
21+
};
22+
23+
export const usePublicSpace = ({ spaceId, enabled }: { spaceId: string; enabled: boolean }) => {
24+
const result = useQuery({
25+
queryKey: ['hypergraph-public-space', spaceId],
26+
queryFn: async () => {
27+
const result = await request<SpaceQueryResult>(GEO_API_TESTNET_ENDPOINT, spaceQueryDocument, {
28+
spaceId,
29+
});
30+
return result?.space?.entity
31+
? {
32+
name: result.space.entity.name,
33+
}
34+
: null;
35+
},
36+
enabled,
37+
});
38+
39+
return result.data;
40+
};

0 commit comments

Comments
 (0)