11import { Loading } from '@/components/ui/Loading' ;
22import { cn } from '@/lib/utils' ;
3+ import { Graph } from '@graphprotocol/grc-20' ;
34import { Key , type Messages , SpaceEvents , SpaceInfo , StoreConnect , Utils } from '@graphprotocol/hypergraph' ;
45import { useIdentityToken } from '@privy-io/react-auth' ;
56import { useQueryClient } from '@tanstack/react-query' ;
@@ -12,12 +13,36 @@ interface CreateSpaceCardProps extends Omit<React.HTMLAttributes<HTMLDivElement>
1213export function CreateSpaceCard ( { className, ...props } : CreateSpaceCardProps ) {
1314 const [ isLoading , setIsLoading ] = useState ( false ) ;
1415 const [ spaceName , setSpaceName ] = useState ( '' ) ;
16+ const [ spaceType , setSpaceType ] = useState < 'private' | 'public' > ( 'private' ) ;
1517 const { identityToken } = useIdentityToken ( ) ;
1618 const accountAddress = useSelector ( StoreConnect . store , ( state ) => state . context . accountAddress ) ;
1719 const keys = useSelector ( StoreConnect . store , ( state ) => state . context . keys ) ;
1820 const queryClient = useQueryClient ( ) ;
1921
20- const createSpace = async ( ) => {
22+ const createPublicSpace = async ( ) => {
23+ if ( ! accountAddress ) {
24+ alert ( 'Missing account address' ) ;
25+ return ;
26+ }
27+
28+ setIsLoading ( true ) ;
29+
30+ try {
31+ await Graph . createSpace ( {
32+ editorAddress : accountAddress ,
33+ name : spaceName ,
34+ network : 'TESTNET' ,
35+ } ) ;
36+ } catch ( error ) {
37+ alert ( 'Failed to create space' ) ;
38+ console . error ( error ) ;
39+ } finally {
40+ setIsLoading ( false ) ;
41+ queryClient . invalidateQueries ( { queryKey : [ 'public-spaces' ] } ) ;
42+ }
43+ } ;
44+
45+ const createPrivateSpace = async ( ) => {
2146 setIsLoading ( true ) ;
2247 if ( ! accountAddress || ! keys || ! identityToken ) {
2348 console . error ( 'Missing required fields' , {
@@ -84,7 +109,7 @@ export function CreateSpaceCard({ className, ...props }: CreateSpaceCardProps) {
84109 } ) ;
85110 const data = await response . json ( ) ;
86111 if ( data . space ) {
87- queryClient . invalidateQueries ( { queryKey : [ 'spaces' ] } ) ;
112+ queryClient . invalidateQueries ( { queryKey : [ 'private- spaces' ] } ) ;
88113 setSpaceName ( '' ) ;
89114 } else {
90115 throw new Error ( 'Failed to create space' ) ;
@@ -97,10 +122,24 @@ export function CreateSpaceCard({ className, ...props }: CreateSpaceCardProps) {
97122 }
98123 } ;
99124
125+ const createSpace = async ( ) => {
126+ if ( spaceType === 'private' ) {
127+ await createPrivateSpace ( ) ;
128+ } else {
129+ await createPublicSpace ( ) ;
130+ }
131+ } ;
132+
100133 return (
101134 < div className = { cn ( 'c-card' , className ) } { ...props } >
102135 < h2 className = "c-card-title" > Create a new space</ h2 >
103- < form className = "flex gap-2" >
136+ < form
137+ className = "flex gap-2"
138+ onSubmit = { ( event ) => {
139+ event . preventDefault ( ) ;
140+ createSpace ( ) ;
141+ } }
142+ >
104143 < input
105144 type = "text"
106145 placeholder = "My cool space"
@@ -109,7 +148,15 @@ export function CreateSpaceCard({ className, ...props }: CreateSpaceCardProps) {
109148 required
110149 className = "c-input grow"
111150 />
112- < button type = "submit" disabled = { isLoading } onClick = { createSpace } className = "c-button shrink-0" >
151+ < select
152+ className = "c-input min-w-22"
153+ value = { spaceType }
154+ onChange = { ( e ) => setSpaceType ( e . target . value as 'private' | 'public' ) }
155+ >
156+ < option value = "private" > Private</ option >
157+ < option value = "public" > Public</ option >
158+ </ select >
159+ < button type = "submit" disabled = { isLoading } className = "c-button shrink-0" >
113160 Create Space
114161 { isLoading ? < Loading hideLabel /> : null }
115162 </ button >
0 commit comments