import { useApolloClient } from '@apollo/client'; export const buildApiFilters = (filters = {}) => { const { tagFilters, ...otherFilters } = filters; const tagsApiFilter = tagFilters ? { tags: tagFilters.flatMap((tagFilter) => tagFilter.values.map( (tag) => ({ namespace: tagFilter.key, key: tag.tagKey, value: tag.value }) ) ) } : {}; return { ...otherFilters, ...tagsApiFilter }; }; export const useFetchSystems = ({ query, onComplete, onError }) => { const client = useApolloClient(); return (limit, offset, requestVariables = {}) => client .query({ query, fetchResults: true, fetchPolicy: 'no-cache', variables: { limit, offset, ...requestVariables } }) .then(({ data }) => { const entities = data?.hostsList || []; const result = { entities, meta: { ...(requestVariables.tags && { tags: requestVariables.tags }), totalCount: data?.hosts?.totalCount || 0 } }; onComplete?.(result); return result; }) .catch((error) => { if (onError) { onError(error); return { entities: [], meta: { totalCount: 0 } }; } else { throw error; } }); };