2023-05-02 10:25:32 -07:00
|
|
|
import { Entities } from 'soapbox/entity-store/entities';
|
2023-06-26 09:05:03 -07:00
|
|
|
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';
|
|
|
|
|
2023-06-26 09:05:03 -07:00
|
|
|
function useGroupRelationships(listKey: string[], ids: string[]) {
|
2023-05-02 10:25:32 -07:00
|
|
|
const api = useApi();
|
2023-06-26 09:05:03 -07:00
|
|
|
const { isLoggedIn } = useLoggedIn();
|
2023-05-02 10:25:32 -07:00
|
|
|
|
2023-06-26 09:05:03 -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
|
|
|
|
2023-06-26 09:05:03 -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
|
|
|
|
2023-06-26 09:05:03 -07:00
|
|
|
return { relationships, ...result };
|
2023-05-02 10:25:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export { useGroupRelationships };
|