'use client'; import React, { useMemo } from 'react'; import { Breadcrumb, Button } from 'antd'; import Link from 'next/link'; import { BusterRoutes, createBusterRoute } from '@/routes'; import { useBusterCollectionListContextSelector, useCollectionIndividual, useCollectionLists } from '@/context/Collections'; import { AppContentHeader, AppMaterialIcons, AppSegmented, AppTooltip } from '@/components'; import { useHotkeys } from 'react-hotkeys-hook'; import { CollectionsListEmit } from '@/api/buster_socket/collections'; import isEmpty from 'lodash/isEmpty'; import omit from 'lodash/omit'; import { useMemoizedFn } from 'ahooks'; import { SegmentedValue } from 'antd/es/segmented'; export const CollectionListHeader: React.FC<{ collectionId?: string; setOpenNewCollectionModal: (open: boolean) => void; }> = React.memo(({ collectionId, setOpenNewCollectionModal }) => { const onSetCollectionListFilters = useBusterCollectionListContextSelector( (x) => x.setCollectionListFilters ); const collectionListFilters = useBusterCollectionListContextSelector( (x) => x.collectionListFilters ); const collectionsList = useBusterCollectionListContextSelector((x) => x.collectionsList); const isCollectionListFetched = useBusterCollectionListContextSelector( (x) => x.isCollectionListFetched ); const { collection } = useCollectionIndividual({ collectionId }); const collectionTitle = collection?.name || 'Collections'; const showFilters = useMemo( () => (isCollectionListFetched && collectionsList?.length !== 0) || !isEmpty(collectionsList) || !isEmpty(omit(collectionListFilters, 'page', 'page_size')), [isCollectionListFetched, collectionsList?.length, collectionListFilters] ); const breadcrumbItems = useMemo( () => [ { title: ( {collectionTitle} ) } ], [collectionId, collectionTitle] ); useHotkeys('n', () => { setOpenNewCollectionModal(true); }); return ( <>
{showFilters && ( )}
); }); CollectionListHeader.displayName = 'CollectionListHeader'; const filters = [ { label: 'All', value: JSON.stringify({}) }, { label: 'My collections', value: JSON.stringify({ owned_by_me: true }) }, { label: 'Shared with me', value: JSON.stringify({ shared_with_me: true }) } ]; const CollectionFilters: React.FC<{ setCollectionListFilters: ReturnType['setCollectionListFilters']; collectionListFilters?: Omit; }> = React.memo(({ setCollectionListFilters, collectionListFilters }) => { const value = useMemo(() => { const activeFiltersValue = JSON.stringify(collectionListFilters); return filters.find((f) => f.value === activeFiltersValue)?.value || filters[0].value; }, [filters, collectionListFilters]); const onChangeFilter = useMemoizedFn((v: SegmentedValue) => { let parsedValue; try { parsedValue = JSON.parse(v as string); } catch (error) { console.error('error', error); } setCollectionListFilters(parsedValue); }); return (
); }); CollectionFilters.displayName = 'CollectionFilters';