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
5 changes: 5 additions & 0 deletions .changeset/add-editor-member-ids.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphprotocol/hypergraph": minor
---

Add `editorIds` and `memberIds` arrays to the `PublicSpace` type returned by `Space.findManyPublic()` and `usePublicSpaces()`.
24 changes: 24 additions & 0 deletions packages/hypergraph/src/space/find-many-public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ const spaceFields = `
}
}
}
editorsList {
memberSpaceId
}
membersList {
memberSpaceId
}
`;

const spacesQueryDocument = `
Expand Down Expand Up @@ -51,6 +57,8 @@ export const PublicSpaceSchema = EffectSchema.Struct({
id: EffectSchema.String,
name: EffectSchema.String,
avatar: EffectSchema.optional(EffectSchema.String),
editorIds: EffectSchema.Array(EffectSchema.String),
memberIds: EffectSchema.Array(EffectSchema.String),
});

export type PublicSpace = typeof PublicSpaceSchema.Type;
Expand All @@ -69,6 +77,12 @@ type SpacesQueryResult = {
} | null;
}[];
} | null;
editorsList?: {
memberSpaceId: string;
}[];
membersList?: {
memberSpaceId: string;
}[];
}[];
};

Expand All @@ -90,6 +104,14 @@ const getAvatarFromSpace = (space: SpaceQueryEntry) => {
return undefined;
};

const getEditorIdsFromSpace = (space: SpaceQueryEntry): string[] => {
return (space.editorsList ?? []).map((e) => e.memberSpaceId).filter((id): id is string => typeof id === 'string');
};

const getMemberIdsFromSpace = (space: SpaceQueryEntry): string[] => {
return (space.membersList ?? []).map((m) => m.memberSpaceId).filter((id): id is string => typeof id === 'string');
};

export const parseSpacesQueryResult = (queryResult: SpacesQueryResult) => {
const data: PublicSpace[] = [];
const invalidSpaces: Record<string, unknown>[] = [];
Expand All @@ -100,6 +122,8 @@ export const parseSpacesQueryResult = (queryResult: SpacesQueryResult) => {
id: space.id,
name: space.page?.name ?? undefined,
avatar: getAvatarFromSpace(space),
editorIds: getEditorIdsFromSpace(space),
memberIds: getMemberIdsFromSpace(space),
};

const decodedSpace = decodeSpace(rawSpace);
Expand Down
34 changes: 34 additions & 0 deletions packages/hypergraph/test/space/find-many-public.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ const buildQuerySpace = ({
id = 'space-id',
name = 'Space name',
avatar,
editorsList = [],
membersList = [],
}: {
id?: string;
name?: string | null;
avatar?: string | null;
editorsList?: { memberSpaceId: string }[];
membersList?: { memberSpaceId: string }[];
} = {}) => {
return {
id,
Expand All @@ -33,6 +37,8 @@ const buildQuerySpace = ({
},
],
},
editorsList,
membersList,
};
};

Expand All @@ -47,6 +53,8 @@ describe('parseSpacesQueryResult', () => {
id: 'space-1',
name: 'Space 1',
avatar: 'https://example.com/avatar.png',
editorIds: [],
memberIds: [],
},
]);
expect(invalidSpaces).toHaveLength(0);
Expand All @@ -61,6 +69,8 @@ describe('parseSpacesQueryResult', () => {
{
id: 'space-2',
name: 'Space 2',
editorIds: [],
memberIds: [],
},
]);
});
Expand All @@ -78,9 +88,33 @@ describe('parseSpacesQueryResult', () => {
id: 'space-valid',
name: 'Space valid',
avatar: 'https://example.com/a.png',
editorIds: [],
memberIds: [],
},
]);
expect(invalidSpaces).toHaveLength(1);
expect(invalidSpaces[0]).toMatchObject({ id: 'space-invalid' });
});

it('parses editorIds and memberIds', () => {
const { data } = parseSpacesQueryResult({
spaces: [
buildQuerySpace({
id: 'space-with-members',
name: 'Space with members',
editorsList: [{ memberSpaceId: 'editor-1' }, { memberSpaceId: 'editor-2' }],
membersList: [{ memberSpaceId: 'member-1' }],
}),
],
});

expect(data).toEqual([
{
id: 'space-with-members',
name: 'Space with members',
editorIds: ['editor-1', 'editor-2'],
memberIds: ['member-1'],
},
]);
});
});
Loading