Skip to content

Commit f151196

Browse files
committed
allow to list private spaces
1 parent aab8eed commit f151196

5 files changed

Lines changed: 78 additions & 106 deletions

File tree

apps/events/src/routes/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const Route = createFileRoute('/')({
1313

1414
function Index() {
1515
const { data: publicSpaces } = useSpaces({ mode: 'public' });
16-
const spaces = useSelector(store, (state) => state.context.spaces);
16+
const { data: privateSpaces } = useSpaces({ mode: 'private' });
1717
const [spaceName, setSpaceName] = useState('');
1818

1919
const accountInboxes = useSelector(store, (state) => state.context.accountInboxes);
@@ -93,8 +93,8 @@ function Index() {
9393

9494
<h2 className="text-lg font-bold">Private Spaces</h2>
9595
<ul className="flex flex-col gap-2">
96-
{spaces.length === 0 && <div>No spaces</div>}
97-
{spaces.map((space) => {
96+
{privateSpaces && privateSpaces.length === 0 && <div>No spaces</div>}
97+
{privateSpaces?.map((space) => {
9898
return (
9999
<li key={space.id}>
100100
<Link to="/space/$spaceId" params={{ spaceId: space.id }}>

packages/hypergraph-react/src/HypergraphAppContext.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,12 @@ export function HypergraphAppProvider({
379379
const response = message.right;
380380
switch (response.type) {
381381
case 'list-spaces': {
382-
response.spaces.map((space) => {
383-
store.send({
384-
type: 'setSpaceFromList',
385-
spaceId: space.id,
382+
store.send({
383+
type: 'setSpacesList',
384+
spaces: response.spaces.map((space) => ({
385+
id: space.id,
386386
name: space.name,
387-
});
387+
})),
388388
});
389389
break;
390390
}

packages/hypergraph-react/src/hooks/use-spaces.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ type PublicSpacesQueryResult = {
2828
}[];
2929
};
3030

31-
export const useSpaces = (params: { mode: 'public' }) => {
31+
export const useSpaces = (params: { mode: 'public' | 'private' }) => {
3232
const accountAddress = useSelector(store, (state) => state.context.identity?.accountAddress);
33-
return useQuery({
33+
const publicResult = useQuery({
3434
queryKey: ['hypergraph-spaces', params.mode],
3535
queryFn: async () => {
3636
const result = await request<PublicSpacesQueryResult>(GEO_API_TESTNET_ENDPOINT, publicSpacesQueryDocument, {
@@ -44,5 +44,18 @@ export const useSpaces = (params: { mode: 'public' }) => {
4444
}))
4545
: [];
4646
},
47+
enabled: params.mode === 'public' && !!accountAddress,
4748
});
49+
50+
const spaces = useSelector(store, (state) => state.context.spaces);
51+
const spacesLoadingIsPending = useSelector(store, (state) => state.context.spacesLoadingIsPending);
52+
53+
if (params.mode === 'private') {
54+
return {
55+
data: spaces,
56+
isPending: spacesLoadingIsPending,
57+
};
58+
}
59+
60+
return publicResult;
4861
};

packages/hypergraph/src/store-connect.ts

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ type StoreEvent =
9090
| { type: 'reset' }
9191
| { type: 'addUpdateInFlight'; updateId: string }
9292
| { type: 'removeUpdateInFlight'; updateId: string }
93-
| { type: 'setSpaceFromList'; spaceId: string }
9493
| { type: 'applyEvent'; spaceId: string; event: SpaceEvent; state: SpaceState }
9594
| { type: 'updateConfirmed'; spaceId: string; clock: number }
9695
| { type: 'applyUpdate'; spaceId: string; firstUpdateClock: number; lastUpdateClock: number }
@@ -178,57 +177,6 @@ export const store: Store<StoreContext, StoreEvent, GenericEventObject> = create
178177
updatesInFlight: context.updatesInFlight.filter((id) => id !== event.updateId),
179178
};
180179
},
181-
setSpaceFromList: (context, event: { spaceId: string }) => {
182-
if (!context.repo) {
183-
return context;
184-
}
185-
const existingSpace = context.spaces.find((s) => s.id === event.spaceId);
186-
const lastUpdateClock = context.lastUpdateClock[event.spaceId] ?? -1;
187-
const result = context.repo.findWithProgress(idToAutomergeId(event.spaceId) as AnyDocumentId);
188-
189-
// set it to ready to interact with the document
190-
result.handle.doneLoading();
191-
192-
if (existingSpace) {
193-
return {
194-
...context,
195-
spaces: context.spaces.map((existingSpace) => {
196-
if (existingSpace.id === event.spaceId) {
197-
const newSpace: SpaceStorageEntry = {
198-
id: existingSpace.id,
199-
events: existingSpace.events ?? [],
200-
state: existingSpace.state,
201-
keys: existingSpace.keys ?? [],
202-
automergeDocHandle: result.handle,
203-
inboxes: existingSpace.inboxes ?? [],
204-
};
205-
return newSpace;
206-
}
207-
return existingSpace;
208-
}),
209-
lastUpdateClock: {
210-
...context.lastUpdateClock,
211-
[event.spaceId]: lastUpdateClock,
212-
},
213-
};
214-
}
215-
return {
216-
...context,
217-
spaces: [
218-
...context.spaces,
219-
{
220-
id: event.spaceId,
221-
events: [],
222-
state: undefined,
223-
keys: [],
224-
inboxes: [],
225-
updates: [],
226-
lastUpdateClock: -1,
227-
automergeDocHandle: result.handle,
228-
},
229-
],
230-
};
231-
},
232180
applyEvent: (context, event: { spaceId: string; event: SpaceEvent; state: SpaceState }) => {
233181
return {
234182
...context,

packages/hypergraph/src/store.ts

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export type SpaceStorageEntry = {
5353

5454
interface StoreContext {
5555
spaces: SpaceStorageEntry[];
56+
spacesLoadingIsPending: boolean;
5657
updatesInFlight: string[];
5758
invitations: Invitation[];
5859
repo: Repo | null;
@@ -73,6 +74,7 @@ interface StoreContext {
7374

7475
const initialStoreContext: StoreContext = {
7576
spaces: [],
77+
spacesLoadingIsPending: true,
7678
updatesInFlight: [],
7779
invitations: [],
7880
repo: null,
@@ -90,7 +92,7 @@ type StoreEvent =
9092
| { type: 'reset' }
9193
| { type: 'addUpdateInFlight'; updateId: string }
9294
| { type: 'removeUpdateInFlight'; updateId: string }
93-
| { type: 'setSpaceFromList'; spaceId: string; name: string }
95+
| { type: 'setSpacesList'; spaces: { id: string; name: string }[] }
9496
| { type: 'applyEvent'; spaceId: string; event: SpaceEvent; state: SpaceState }
9597
| { type: 'updateConfirmed'; spaceId: string; clock: number }
9698
| { type: 'applyUpdate'; spaceId: string; firstUpdateClock: number; lastUpdateClock: number }
@@ -182,58 +184,67 @@ export const store: Store<StoreContext, StoreEvent, GenericEventObject> = create
182184
updatesInFlight: context.updatesInFlight.filter((id) => id !== event.updateId),
183185
};
184186
},
185-
setSpaceFromList: (context, event: { spaceId: string; name: string }) => {
187+
setSpacesList: (context, event: { spaces: { id: string; name: string }[] }) => {
186188
if (!context.repo) {
187189
return context;
188190
}
189-
const existingSpace = context.spaces.find((s) => s.id === event.spaceId);
190-
const lastUpdateClock = context.lastUpdateClock[event.spaceId] ?? -1;
191-
const result = context.repo.findWithProgress(idToAutomergeId(event.spaceId) as AnyDocumentId);
192191

193-
// set it to ready to interact with the document
194-
result.handle.doneLoading();
192+
let storeContext: StoreContext = { ...context, spacesLoadingIsPending: false };
195193

196-
if (existingSpace) {
197-
return {
198-
...context,
199-
spaces: context.spaces.map((existingSpace) => {
200-
if (existingSpace.id === event.spaceId) {
201-
const newSpace: SpaceStorageEntry = {
202-
id: existingSpace.id,
203-
name: existingSpace.name,
204-
events: existingSpace.events ?? [],
205-
state: existingSpace.state,
206-
keys: existingSpace.keys ?? [],
207-
automergeDocHandle: result.handle,
208-
inboxes: existingSpace.inboxes ?? [],
209-
};
210-
return newSpace;
211-
}
212-
return existingSpace;
213-
}),
194+
for (const space of event.spaces) {
195+
const existingSpace = context.spaces.find((s) => s.id === space.id);
196+
const lastUpdateClock = context.lastUpdateClock[space.id] ?? -1;
197+
const result = context.repo.findWithProgress(idToAutomergeId(space.id) as AnyDocumentId);
198+
199+
// set it to ready to interact with the document
200+
result.handle.doneLoading();
201+
202+
if (existingSpace) {
203+
storeContext = {
204+
...storeContext,
205+
spaces: storeContext.spaces.map((existingSpace) => {
206+
if (existingSpace.id === space.id) {
207+
const newSpace: SpaceStorageEntry = {
208+
id: existingSpace.id,
209+
name: existingSpace.name,
210+
events: existingSpace.events ?? [],
211+
state: existingSpace.state,
212+
keys: existingSpace.keys ?? [],
213+
automergeDocHandle: result.handle,
214+
inboxes: existingSpace.inboxes ?? [],
215+
};
216+
return newSpace;
217+
}
218+
return existingSpace;
219+
}),
220+
lastUpdateClock: {
221+
...storeContext.lastUpdateClock,
222+
[space.id]: lastUpdateClock,
223+
},
224+
};
225+
}
226+
storeContext = {
227+
...storeContext,
228+
spaces: [
229+
...storeContext.spaces,
230+
{
231+
id: space.id,
232+
name: space.name,
233+
events: [],
234+
state: undefined,
235+
keys: [],
236+
inboxes: [],
237+
automergeDocHandle: result.handle,
238+
},
239+
],
214240
lastUpdateClock: {
215-
...context.lastUpdateClock,
216-
[event.spaceId]: lastUpdateClock,
241+
...storeContext.lastUpdateClock,
242+
[space.id]: -1,
217243
},
218244
};
219245
}
220-
return {
221-
...context,
222-
spaces: [
223-
...context.spaces,
224-
{
225-
id: event.spaceId,
226-
name: event.name,
227-
events: [],
228-
state: undefined,
229-
keys: [],
230-
inboxes: [],
231-
updates: [],
232-
lastUpdateClock: -1,
233-
automergeDocHandle: result.handle,
234-
},
235-
],
236-
};
246+
247+
return storeContext;
237248
},
238249
applyEvent: (context, event: { spaceId: string; event: SpaceEvent; state: SpaceState }) => {
239250
return {

0 commit comments

Comments
 (0)