useNavItems.ts 976 字节
Newer Older
P
Peter Pan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
import {fetcher} from '~/utils/fetch';
import intersection from 'lodash/intersection';
import {useMemo} from 'react';
import useRequest from '~/hooks/useRequest';

const allNavItems = ['scalars', 'samples', 'high-dimensional'];
export const navMap = {
    scalar: 'scalars',
    image: 'samples',
    embeddings: 'high-dimensional'
} as const;

const useNavItems = () => {
    const {data: components} = useRequest<(keyof typeof navMap)[]>('/components', fetcher, {
        refreshInterval: 61 * 1000,
        dedupingInterval: 29 * 1000,
        errorRetryInterval: 29 * 1000,
        errorRetryCount: Number.POSITIVE_INFINITY,
        revalidateOnFocus: true,
        revalidateOnReconnect: true,
        refreshWhenHidden: false,
        refreshWhenOffline: false
    });

    const navItems = useMemo(() => intersection(components?.map(component => navMap[component]) ?? [], allNavItems), [
        components
    ]);

    return navItems;
};

export default useNavItems;