pleroma/app/soapbox/api/hooks/groups/useGroupRelationships.ts

25 lines
892 B
TypeScript
Raw Normal View History

2023-05-02 10:25:32 -07:00
import { Entities } from 'soapbox/entity-store/entities';
import { useBatchedEntities } from 'soapbox/entity-store/hooks/useBatchedEntities';
import { useApi, useLoggedIn } from 'soapbox/hooks';
2023-05-02 10:25:32 -07:00
import { type GroupRelationship, groupRelationshipSchema } from 'soapbox/schemas';
function useGroupRelationships(listKey: string[], ids: string[]) {
2023-05-02 10:25:32 -07:00
const api = useApi();
const { isLoggedIn } = useLoggedIn();
2023-05-02 10:25:32 -07:00
function fetchGroupRelationships(ids: string[]) {
const q = ids.map((id) => `id[]=${id}`).join('&');
return api.get(`/api/v1/groups/relationships?${q}`);
}
2023-05-02 10:25:32 -07:00
const { entityMap: relationships, ...result } = useBatchedEntities<GroupRelationship>(
[Entities.RELATIONSHIPS, ...listKey],
ids,
fetchGroupRelationships,
{ schema: groupRelationshipSchema, enabled: isLoggedIn },
);
2023-05-02 10:25:32 -07:00
return { relationships, ...result };
2023-05-02 10:25:32 -07:00
}
export { useGroupRelationships };