2025-07-23 20:11:32 +05:30
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
|
2025-04-16 16:55:38 -07:00
|
|
|
import { useGetEntitiesQuery } from '@graphql/entity.generated';
|
|
|
|
|
import { Entity } from '@types';
|
2025-01-29 20:42:01 -05:00
|
|
|
|
2025-07-18 02:21:02 +05:30
|
|
|
export function useGetEntities(urns: string[]): {
|
|
|
|
|
entities: Entity[];
|
|
|
|
|
loading: boolean;
|
|
|
|
|
} {
|
2025-07-23 20:11:32 +05:30
|
|
|
const verifiedUrns = useMemo(
|
|
|
|
|
() => urns.filter((urn) => typeof urn === 'string' && urn.startsWith('urn:li:')),
|
|
|
|
|
[urns],
|
|
|
|
|
);
|
2025-01-29 20:42:01 -05:00
|
|
|
|
2025-07-18 15:03:40 -04:00
|
|
|
const { data, loading } = useGetEntitiesQuery({
|
|
|
|
|
variables: { urns: verifiedUrns },
|
|
|
|
|
skip: !verifiedUrns.length,
|
|
|
|
|
fetchPolicy: 'cache-first',
|
|
|
|
|
});
|
2025-07-23 20:11:32 +05:30
|
|
|
|
|
|
|
|
const [entities, setEntities] = useState<Entity[]>([]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (data?.entities && data.entities.length > 0) {
|
|
|
|
|
setEntities(data.entities as Entity[]);
|
|
|
|
|
} else if (!loading && (!data?.entities || data.entities.length === 0)) {
|
|
|
|
|
setEntities([]);
|
|
|
|
|
}
|
|
|
|
|
}, [data, loading]);
|
|
|
|
|
|
2025-07-18 02:21:02 +05:30
|
|
|
return { entities, loading };
|
2025-01-29 20:42:01 -05:00
|
|
|
}
|