2023-04-04 05:11:03 -07:00
|
|
|
import { Entities } from 'soapbox/entity-store/entities';
|
2023-06-25 13:57:13 -07:00
|
|
|
import { useBatchedEntities } from 'soapbox/entity-store/hooks/useBatchedEntities';
|
2023-06-23 09:30:10 -07:00
|
|
|
import { useLoggedIn } from 'soapbox/hooks';
|
2023-05-01 11:58:40 -07:00
|
|
|
import { useApi } from 'soapbox/hooks/useApi';
|
2023-04-04 05:11:03 -07:00
|
|
|
import { type Relationship, relationshipSchema } from 'soapbox/schemas';
|
|
|
|
|
2023-06-25 13:57:13 -07:00
|
|
|
function useRelationships(listKey: string[], ids: string[]) {
|
2023-04-04 05:11:03 -07:00
|
|
|
const api = useApi();
|
2023-06-23 09:30:10 -07:00
|
|
|
const { isLoggedIn } = useLoggedIn();
|
2023-06-25 14:11:00 -07:00
|
|
|
|
|
|
|
function fetchRelationships(ids: string[]) {
|
|
|
|
const q = ids.map((id) => `id[]=${id}`).join('&');
|
|
|
|
return api.get(`/api/v1/accounts/relationships?${q}`);
|
|
|
|
}
|
2023-04-04 05:11:03 -07:00
|
|
|
|
2023-06-25 13:57:13 -07:00
|
|
|
const { entityMap: relationships, ...result } = useBatchedEntities<Relationship>(
|
|
|
|
[Entities.RELATIONSHIPS, ...listKey],
|
|
|
|
ids,
|
2023-06-25 14:11:00 -07:00
|
|
|
fetchRelationships,
|
2023-06-25 13:57:13 -07:00
|
|
|
{ schema: relationshipSchema, enabled: isLoggedIn },
|
2023-04-04 05:11:03 -07:00
|
|
|
);
|
|
|
|
|
2023-06-25 13:57:13 -07:00
|
|
|
return { relationships, ...result };
|
2023-04-04 05:11:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export { useRelationships };
|