Skip to content

Commit d6be4c1

Browse files
authored
connect cleanup (#215)
1 parent e998f12 commit d6be4c1

File tree

10 files changed

+354
-299
lines changed

10 files changed

+354
-299
lines changed

apps/connect/src/components/create-space.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,14 @@ export function CreateSpace() {
9292
};
9393

9494
return (
95-
<>
96-
<Input value={spaceName} onChange={(e) => setSpaceName(e.target.value)} />
97-
<Button className="home-button" onClick={createSpace} disabled={isLoading}>
98-
Create Space {isLoading ? <Spinner /> : null}
99-
</Button>
100-
</>
95+
<div className="flex flex-col gap-2">
96+
<span className="text-xs text-gray-500">Create a new space</span>
97+
<div className="flex flex-row gap-2 items-center">
98+
<Input value={spaceName} onChange={(e) => setSpaceName(e.target.value)} />
99+
<Button className="home-button" onClick={createSpace} disabled={isLoading}>
100+
Create Space {isLoading ? <Spinner /> : null}
101+
</Button>
102+
</div>
103+
</div>
101104
);
102105
}
Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,30 @@
1-
import { useIdentityToken } from '@privy-io/react-auth';
2-
import { useQuery } from '@tanstack/react-query';
1+
import { useSpaces } from '@/hooks/use-spaces';
32

43
export function Spaces() {
5-
const { identityToken } = useIdentityToken();
6-
7-
const { isPending, error, data } = useQuery<{
8-
spaces: {
9-
id: string;
10-
name: string;
11-
appIdentities: { address: string; appId: string }[];
12-
keyBoxes: {
13-
id: string;
14-
ciphertext: string;
15-
nonce: string;
16-
authorPublicKey: string;
17-
}[];
18-
}[];
19-
}>({
20-
queryKey: ['spaces'],
21-
queryFn: async () => {
22-
if (!identityToken) return;
23-
const response = await fetch(`${import.meta.env.VITE_HYPERGRAPH_SYNC_SERVER_ORIGIN}/connect/spaces`, {
24-
headers: { 'privy-id-token': identityToken },
25-
});
26-
return await response.json();
27-
},
28-
});
4+
const { isPending, error, data } = useSpaces();
295

306
if (isPending) return 'Loading spaces …';
317

328
if (error) return `An error has occurred: ${error.message}`;
339

3410
return (
35-
<>
36-
{data.spaces.map((space) => (
37-
<div key={space.id}>
38-
<h2>
39-
{space.name} ({space.id})
40-
<br />
41-
---------
42-
<br />
43-
{space.appIdentities.map((appIdentity) => (
44-
<div key={appIdentity.address}>
45-
{appIdentity.appId} ({appIdentity.address})
46-
</div>
47-
))}
48-
</h2>
49-
</div>
50-
))}
51-
</>
11+
<div>
12+
<h2 className="font-bold mb-2 mt-2">Spaces</h2>
13+
<ul className="space-y-4">
14+
{data.map((space) => (
15+
<li key={space.id}>
16+
<p>{space.name}</p>
17+
<p className="text-xs text-gray-500 mt-2 mb-1">Apps with access to this space</p>
18+
<ul>
19+
{space.apps.map((app) => (
20+
<li key={app.id} className="text-sm">
21+
{app.name}
22+
</li>
23+
))}
24+
</ul>
25+
</li>
26+
))}
27+
</ul>
28+
</div>
5229
);
5330
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { getAppInfoByIds } from '@/lib/get-app-info-by-ids';
2+
import { useIdentityToken } from '@privy-io/react-auth';
3+
import { useQuery } from '@tanstack/react-query';
4+
5+
type SpaceData = {
6+
id: string;
7+
name: string;
8+
appIdentities: { address: string; appId: string }[];
9+
apps: { name: string; id: string }[];
10+
keyBoxes: {
11+
id: string;
12+
ciphertext: string;
13+
nonce: string;
14+
authorPublicKey: string;
15+
}[];
16+
};
17+
18+
export const useSpaces = () => {
19+
const { identityToken } = useIdentityToken();
20+
21+
return useQuery<SpaceData[]>({
22+
queryKey: ['spaces'],
23+
queryFn: async () => {
24+
if (!identityToken) return [];
25+
const response = await fetch(`${import.meta.env.VITE_HYPERGRAPH_SYNC_SERVER_ORIGIN}/connect/spaces`, {
26+
headers: { 'privy-id-token': identityToken },
27+
});
28+
const data = await response.json();
29+
const appIds = new Set<string>();
30+
for (const space of data.spaces) {
31+
for (const appIdentity of space.appIdentities) {
32+
appIds.add(appIdentity.appId);
33+
}
34+
}
35+
const appInfo = await getAppInfoByIds(Array.from(appIds));
36+
const spaces = data.spaces.map((space: SpaceData) => {
37+
const spaceAppIds = new Set<string>();
38+
for (const appIdentity of space.appIdentities) {
39+
spaceAppIds.add(appIdentity.appId);
40+
}
41+
return {
42+
...space,
43+
apps: Array.from(spaceAppIds).map((appId) => {
44+
return {
45+
// @ts-expect-error - need to improve appInfo typing
46+
name: appInfo[appId]?.name ?? 'Unknown',
47+
id: appId,
48+
};
49+
}),
50+
};
51+
});
52+
return spaces;
53+
},
54+
});
55+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const apps = {
2+
'93bb8907-085a-4a0e-83dd-62b0dc98e793': {
3+
name: 'Todos',
4+
},
5+
};
6+
7+
export const getAppInfoByIds = async (appIds: string[]) => {
8+
// sleep for 1 second
9+
await new Promise((resolve) => setTimeout(resolve, 1000));
10+
return apps;
11+
};

0 commit comments

Comments
 (0)