Skip to content

Commit d7771a7

Browse files
committed
add space type filtering
1 parent c128c89 commit d7771a7

2 files changed

Lines changed: 50 additions & 46 deletions

File tree

.changeset/add-space-type.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,13 @@
44
---
55

66
Add `type` field to `PublicSpace` type returned by `Space.findManyPublic()` and `usePublicSpaces()`. The type is either `"PERSONAL"` or `"DAO"`.
7+
8+
Add `spaceType` filter option to `Space.findManyPublic()` and `usePublicSpaces()` to filter spaces by type. Example usage:
9+
10+
```typescript
11+
// Filter for DAO spaces only
12+
const { data } = usePublicSpaces({ filter: { spaceType: 'DAO' } });
13+
14+
// Combine with existing filters
15+
const { data } = usePublicSpaces({ filter: { editorId: 'xxx', spaceType: 'PERSONAL' } });
16+
```

packages/hypergraph/src/space/find-many-public.ts

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,6 @@ const spaceFields = `
3030
}
3131
`;
3232

33-
const spacesQueryDocument = `
34-
query spaces {
35-
spaces {
36-
${spaceFields}
37-
}
38-
}
39-
`;
40-
41-
const memberSpacesQueryDocument = `
42-
query memberSpaces($accountId: UUID!) {
43-
spaces(filter: {members: {some: {memberSpaceId: {is: $accountId}}}}) {
44-
${spaceFields}
45-
}
46-
}
47-
`;
48-
49-
const editorSpacesQueryDocument = `
50-
query editorSpaces($accountId: UUID!) {
51-
spaces(filter: {editors: {some: {memberSpaceId: {is: $accountId}}}}) {
52-
${spaceFields}
53-
}
54-
}
55-
`;
5633

5734
export const SpaceTypeSchema = EffectSchema.Union(
5835
EffectSchema.Literal('PERSONAL'),
@@ -96,10 +73,6 @@ type SpacesQueryResult = {
9673
}[];
9774
};
9875

99-
type SpacesQueryVariables = {
100-
accountId: string;
101-
};
102-
10376
type SpaceQueryEntry = NonNullable<SpacesQueryResult['spaces']>[number];
10477

10578
const decodeSpace = EffectSchema.decodeUnknownEither(PublicSpaceSchema);
@@ -150,14 +123,49 @@ export const parseSpacesQueryResult = (queryResult: SpacesQueryResult) => {
150123
};
151124

152125
export type FindManyPublicFilter =
153-
| Readonly<{ memberId: string; editorId?: never }>
154-
| Readonly<{ editorId: string; memberId?: never }>
155-
| Readonly<{ memberId?: undefined; editorId?: undefined }>;
126+
| Readonly<{ memberId: string; editorId?: never; spaceType?: SpaceType }>
127+
| Readonly<{ editorId: string; memberId?: never; spaceType?: SpaceType }>
128+
| Readonly<{ memberId?: undefined; editorId?: undefined; spaceType?: SpaceType }>;
156129

157130
export type FindManyPublicParams = Readonly<{
158131
filter?: FindManyPublicFilter;
159132
}>;
160133

134+
const buildFilterString = (filter?: FindManyPublicFilter): string | undefined => {
135+
const conditions: string[] = [];
136+
137+
if (filter?.memberId) {
138+
conditions.push(`members: {some: {memberSpaceId: {is: "${filter.memberId}"}}}`);
139+
}
140+
141+
if (filter?.editorId) {
142+
conditions.push(`editors: {some: {memberSpaceId: {is: "${filter.editorId}"}}}`);
143+
}
144+
145+
if (filter?.spaceType) {
146+
conditions.push(`type: {is: ${filter.spaceType}}`);
147+
}
148+
149+
if (conditions.length === 0) {
150+
return undefined;
151+
}
152+
153+
return `filter: {${conditions.join(', ')}}`;
154+
};
155+
156+
const buildSpacesQuery = (filter?: FindManyPublicFilter): string => {
157+
const filterString = buildFilterString(filter);
158+
const filterClause = filterString ? `(${filterString})` : '';
159+
160+
return `
161+
query spaces {
162+
spaces${filterClause} {
163+
${spaceFields}
164+
}
165+
}
166+
`;
167+
};
168+
161169
export const findManyPublic = async (params?: FindManyPublicParams) => {
162170
const filter = params?.filter;
163171
const memberId = filter?.memberId;
@@ -168,21 +176,7 @@ export const findManyPublic = async (params?: FindManyPublicParams) => {
168176
}
169177

170178
const endpoint = `${Config.getApiOrigin()}/v2/graphql`;
171-
172-
if (memberId) {
173-
const queryResult = await request<SpacesQueryResult, SpacesQueryVariables>(endpoint, memberSpacesQueryDocument, {
174-
accountId: memberId,
175-
});
176-
return parseSpacesQueryResult(queryResult);
177-
}
178-
179-
if (editorId) {
180-
const queryResult = await request<SpacesQueryResult, SpacesQueryVariables>(endpoint, editorSpacesQueryDocument, {
181-
accountId: editorId,
182-
});
183-
return parseSpacesQueryResult(queryResult);
184-
}
185-
186-
const queryResult = await request<SpacesQueryResult>(endpoint, spacesQueryDocument);
179+
const queryDocument = buildSpacesQuery(filter);
180+
const queryResult = await request<SpacesQueryResult>(endpoint, queryDocument);
187181
return parseSpacesQueryResult(queryResult);
188182
};

0 commit comments

Comments
 (0)