pleroma/app/soapbox/api/hooks/accounts/useRelationships.ts

26 lines
893 B
TypeScript
Raw Normal View History

import { Entities } from 'soapbox/entity-store/entities';
import { useBatchedEntities } from 'soapbox/entity-store/hooks/useBatchedEntities';
import { useLoggedIn } from 'soapbox/hooks';
2023-05-01 11:58:40 -07:00
import { useApi } from 'soapbox/hooks/useApi';
import { type Relationship, relationshipSchema } from 'soapbox/schemas';
function useRelationships(listKey: string[], ids: string[]) {
const api = useApi();
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}`);
}
const { entityMap: relationships, ...result } = useBatchedEntities<Relationship>(
[Entities.RELATIONSHIPS, ...listKey],
ids,
2023-06-25 14:11:00 -07:00
fetchRelationships,
{ schema: relationshipSchema, enabled: isLoggedIn },
);
return { relationships, ...result };
}
export { useRelationships };