File tree Expand file tree Collapse file tree 4 files changed +53
-11
lines changed
packages/hypergraph-react/src Expand file tree Collapse file tree 4 files changed +53
-11
lines changed Original file line number Diff line number Diff line change 3
3
_useDeleteEntityPublic ,
4
4
useHypergraphApp ,
5
5
useQuery ,
6
+ useSpace ,
6
7
} from '@graphprotocol/hypergraph-react' ;
7
8
import { useState } from 'react' ;
8
9
import { Event } from '../schema' ;
@@ -21,18 +22,16 @@ export const Playground = () => {
21
22
const [ isCreating , setIsCreating ] = useState ( false ) ;
22
23
const { getSmartSessionClient } = useHypergraphApp ( ) ;
23
24
24
- const deleteEntity = _useDeleteEntityPublic ( Event , {
25
- space : 'a57cd482-6dd3-4ba3-ac44-e2e8ea7a2862' ,
26
- } ) ;
25
+ const { name, id : spaceId } = useSpace ( { mode : 'public' } ) ;
27
26
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 } ) ;
31
29
32
30
console . log ( { isLoading, isError, data } ) ;
33
31
34
32
return (
35
33
< div >
34
+ < h2 className = "text-lg font-bold" > Space: { name } </ h2 >
36
35
{ isLoading && < div > Loading...</ div > }
37
36
{ isError && < div > Error</ div > }
38
37
< Button
Original file line number Diff line number Diff line change @@ -9,13 +9,14 @@ export const Route = createLazyFileRoute('/playground')({
9
9
} ) ;
10
10
11
11
function RouteComponent ( ) {
12
+ const space = 'a57cd482-6dd3-4ba3-ac44-e2e8ea7a2862' ;
12
13
return (
13
- < HypergraphSpaceProvider space = "a57cd482-6dd3-4ba3-ac44-e2e8ea7a2862" >
14
+ < HypergraphSpaceProvider space = { space } >
14
15
< div className = "flex flex-col gap-4 max-w-(--breakpoint-sm) mx-auto py-8" >
15
16
< h1 className = "text-2xl font-bold" > Playground</ h1 >
16
17
< 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 } />
19
20
</ div >
20
21
</ HypergraphSpaceProvider >
21
22
) ;
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import {
14
14
useSyncExternalStore ,
15
15
} from 'react' ;
16
16
import { useHypergraphApp } from './HypergraphAppContext.js' ;
17
+ import { usePublicSpace } from './internal/use-public-space.js' ;
17
18
18
19
// TODO space can be undefined
19
20
export type HypergraphContext = { space : string } ;
@@ -64,8 +65,9 @@ export function useSpace(options: { space?: string; mode: 'private' | 'public' }
64
65
const spaceId = spaceIdFromParams ?? spaceIdFromContext ;
65
66
const handle = useSubscribeToSpaceAndGetHandle ( { spaceId, enabled : options . mode === 'private' } ) ;
66
67
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 } ;
69
71
}
70
72
71
73
export function useCreateEntity < const S extends Entity . AnyNoContext > ( type : S , options ?: { space ?: string } ) {
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments