Skip to content

Commit 0af6aa6

Browse files
committed
allow to create public spaces
1 parent ebbdc4e commit 0af6aa6

4 files changed

Lines changed: 171 additions & 124 deletions

File tree

apps/connect/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"dependencies": {
1717
"@base-ui-components/react": "1.0.0-beta.0",
18+
"@graphprotocol/grc-20": "^0.21.2",
1819
"@graphprotocol/hypergraph": "workspace:*",
1920
"@graphprotocol/hypergraph-react": "workspace:*",
2021
"@privy-io/react-auth": "^2.13.0",

apps/connect/src/components/CreateSpaceCard.tsx

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Loading } from '@/components/ui/Loading';
22
import { cn } from '@/lib/utils';
3+
import { Graph } from '@graphprotocol/grc-20';
34
import { Key, type Messages, SpaceEvents, SpaceInfo, StoreConnect, Utils } from '@graphprotocol/hypergraph';
45
import { useIdentityToken } from '@privy-io/react-auth';
56
import { useQueryClient } from '@tanstack/react-query';
@@ -12,12 +13,36 @@ interface CreateSpaceCardProps extends Omit<React.HTMLAttributes<HTMLDivElement>
1213
export 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', {
@@ -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>

apps/connect/src/routes/authenticate.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ function AuthenticateComponent() {
575575
<CreateSpaceCard className="lg:col-2" />
576576
<div className="relative min-h-80 lg:col-2">
577577
<SpacesCard
578-
spaces={privateSpacesData ?? []}
578+
spaces={[...(privateSpacesData ?? []), ...(publicSpacesData ?? [])]}
579579
status={
580580
privateSpacesPending
581581
? 'loading'

0 commit comments

Comments
 (0)