Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/events/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const Route = createFileRoute('/')({

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

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

<h2 className="text-lg font-bold">Private Spaces</h2>
<ul className="flex flex-col gap-2">
{spaces.length === 0 && <div>No spaces</div>}
{spaces.map((space) => {
{privateSpaces && privateSpaces.length === 0 && <div>No spaces</div>}
{privateSpaces?.map((space) => {
return (
<li key={space.id}>
<Link to="/space/$spaceId" params={{ spaceId: space.id }}>
Expand Down
10 changes: 5 additions & 5 deletions packages/hypergraph-react/src/HypergraphAppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,12 @@ export function HypergraphAppProvider({
const response = message.right;
switch (response.type) {
case 'list-spaces': {
response.spaces.map((space) => {
store.send({
type: 'setSpaceFromList',
spaceId: space.id,
store.send({
type: 'setSpacesList',
spaces: response.spaces.map((space) => ({
id: space.id,
name: space.name,
});
})),
});
break;
}
Expand Down
17 changes: 15 additions & 2 deletions packages/hypergraph-react/src/hooks/use-spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ type PublicSpacesQueryResult = {
}[];
};

export const useSpaces = (params: { mode: 'public' }) => {
export const useSpaces = (params: { mode: 'public' | 'private' }) => {
const accountAddress = useSelector(store, (state) => state.context.identity?.accountAddress);
return useQuery({
const publicResult = useQuery({
queryKey: ['hypergraph-spaces', params.mode],
queryFn: async () => {
const result = await request<PublicSpacesQueryResult>(GEO_API_TESTNET_ENDPOINT, publicSpacesQueryDocument, {
Expand All @@ -44,5 +44,18 @@ export const useSpaces = (params: { mode: 'public' }) => {
}))
: [];
},
enabled: params.mode === 'public' && !!accountAddress,
});

const spaces = useSelector(store, (state) => state.context.spaces);
const spacesLoadingIsPending = useSelector(store, (state) => state.context.spacesLoadingIsPending);

if (params.mode === 'private') {
return {
data: spaces,
isPending: spacesLoadingIsPending,
};
}

return publicResult;
};
52 changes: 0 additions & 52 deletions packages/hypergraph/src/store-connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ type StoreEvent =
| { type: 'reset' }
| { type: 'addUpdateInFlight'; updateId: string }
| { type: 'removeUpdateInFlight'; updateId: string }
| { type: 'setSpaceFromList'; spaceId: string }
| { type: 'applyEvent'; spaceId: string; event: SpaceEvent; state: SpaceState }
| { type: 'updateConfirmed'; spaceId: string; clock: number }
| { type: 'applyUpdate'; spaceId: string; firstUpdateClock: number; lastUpdateClock: number }
Expand Down Expand Up @@ -178,57 +177,6 @@ export const store: Store<StoreContext, StoreEvent, GenericEventObject> = create
updatesInFlight: context.updatesInFlight.filter((id) => id !== event.updateId),
};
},
setSpaceFromList: (context, event: { spaceId: string }) => {
if (!context.repo) {
return context;
}
const existingSpace = context.spaces.find((s) => s.id === event.spaceId);
const lastUpdateClock = context.lastUpdateClock[event.spaceId] ?? -1;
const result = context.repo.findWithProgress(idToAutomergeId(event.spaceId) as AnyDocumentId);

// set it to ready to interact with the document
result.handle.doneLoading();

if (existingSpace) {
return {
...context,
spaces: context.spaces.map((existingSpace) => {
if (existingSpace.id === event.spaceId) {
const newSpace: SpaceStorageEntry = {
id: existingSpace.id,
events: existingSpace.events ?? [],
state: existingSpace.state,
keys: existingSpace.keys ?? [],
automergeDocHandle: result.handle,
inboxes: existingSpace.inboxes ?? [],
};
return newSpace;
}
return existingSpace;
}),
lastUpdateClock: {
...context.lastUpdateClock,
[event.spaceId]: lastUpdateClock,
},
};
}
return {
...context,
spaces: [
...context.spaces,
{
id: event.spaceId,
events: [],
state: undefined,
keys: [],
inboxes: [],
updates: [],
lastUpdateClock: -1,
automergeDocHandle: result.handle,
},
],
};
},
applyEvent: (context, event: { spaceId: string; event: SpaceEvent; state: SpaceState }) => {
return {
...context,
Expand Down
99 changes: 55 additions & 44 deletions packages/hypergraph/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type SpaceStorageEntry = {

interface StoreContext {
spaces: SpaceStorageEntry[];
spacesLoadingIsPending: boolean;
updatesInFlight: string[];
invitations: Invitation[];
repo: Repo | null;
Expand All @@ -73,6 +74,7 @@ interface StoreContext {

const initialStoreContext: StoreContext = {
spaces: [],
spacesLoadingIsPending: true,
updatesInFlight: [],
invitations: [],
repo: null,
Expand All @@ -90,7 +92,7 @@ type StoreEvent =
| { type: 'reset' }
| { type: 'addUpdateInFlight'; updateId: string }
| { type: 'removeUpdateInFlight'; updateId: string }
| { type: 'setSpaceFromList'; spaceId: string; name: string }
| { type: 'setSpacesList'; spaces: { id: string; name: string }[] }
| { type: 'applyEvent'; spaceId: string; event: SpaceEvent; state: SpaceState }
| { type: 'updateConfirmed'; spaceId: string; clock: number }
| { type: 'applyUpdate'; spaceId: string; firstUpdateClock: number; lastUpdateClock: number }
Expand Down Expand Up @@ -182,58 +184,67 @@ export const store: Store<StoreContext, StoreEvent, GenericEventObject> = create
updatesInFlight: context.updatesInFlight.filter((id) => id !== event.updateId),
};
},
setSpaceFromList: (context, event: { spaceId: string; name: string }) => {
setSpacesList: (context, event: { spaces: { id: string; name: string }[] }) => {
if (!context.repo) {
return context;
}
const existingSpace = context.spaces.find((s) => s.id === event.spaceId);
const lastUpdateClock = context.lastUpdateClock[event.spaceId] ?? -1;
const result = context.repo.findWithProgress(idToAutomergeId(event.spaceId) as AnyDocumentId);

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

if (existingSpace) {
return {
...context,
spaces: context.spaces.map((existingSpace) => {
if (existingSpace.id === event.spaceId) {
const newSpace: SpaceStorageEntry = {
id: existingSpace.id,
name: existingSpace.name,
events: existingSpace.events ?? [],
state: existingSpace.state,
keys: existingSpace.keys ?? [],
automergeDocHandle: result.handle,
inboxes: existingSpace.inboxes ?? [],
};
return newSpace;
}
return existingSpace;
}),
for (const space of event.spaces) {
const existingSpace = context.spaces.find((s) => s.id === space.id);
const lastUpdateClock = context.lastUpdateClock[space.id] ?? -1;
const result = context.repo.findWithProgress(idToAutomergeId(space.id) as AnyDocumentId);

// set it to ready to interact with the document
result.handle.doneLoading();

if (existingSpace) {
storeContext = {
...storeContext,
spaces: storeContext.spaces.map((existingSpace) => {
if (existingSpace.id === space.id) {
const newSpace: SpaceStorageEntry = {
id: existingSpace.id,
name: existingSpace.name,
events: existingSpace.events ?? [],
state: existingSpace.state,
keys: existingSpace.keys ?? [],
automergeDocHandle: result.handle,
inboxes: existingSpace.inboxes ?? [],
};
return newSpace;
}
return existingSpace;
}),
lastUpdateClock: {
...storeContext.lastUpdateClock,
[space.id]: lastUpdateClock,
},
};
}
storeContext = {
...storeContext,
spaces: [
...storeContext.spaces,
{
id: space.id,
name: space.name,
events: [],
state: undefined,
keys: [],
inboxes: [],
automergeDocHandle: result.handle,
},
],
lastUpdateClock: {
...context.lastUpdateClock,
[event.spaceId]: lastUpdateClock,
...storeContext.lastUpdateClock,
[space.id]: -1,
},
};
}
return {
...context,
spaces: [
...context.spaces,
{
id: event.spaceId,
name: event.name,
events: [],
state: undefined,
keys: [],
inboxes: [],
updates: [],
lastUpdateClock: -1,
automergeDocHandle: result.handle,
},
],
};

return storeContext;
},
applyEvent: (context, event: { spaceId: string; event: SpaceEvent; state: SpaceState }) => {
return {
Expand Down
Loading