import React, { ReactNode, useState } from 'react';
import { useCatalog } from '@thoughtindustries/catalog';
import clsx from 'clsx';
import { ArrowDownIcon, ArrowRightIcon } from '@thoughtindustries/catalog/src/icons';
import { useLanguagesQueryQuery } from '@thoughtindustries/content';
type AggregationBucketProps = {
href: string;
value: string;
count?: number;
};
type AggregationProps = {
index: number;
label: string;
defaultIsExpanded: boolean;
aggregationBuckets: ReactNode;
};
const AggregationBucket = ({ href, value, count }: AggregationBucketProps): JSX.Element => (
{value}
{count && (
)}
);
const Aggregation = ({
index,
label,
defaultIsExpanded,
aggregationBuckets
}: AggregationProps): JSX.Element => {
const [isExpanded, setIsExpanded] = useState(defaultIsExpanded);
const handleToggle = () => {
setIsExpanded(prevIsExpanded => !prevIsExpanded);
};
const buttonLinkClassnames =
'w-full leading-normal text-left transition-colors ease-in-out duration-200 bg-none text-accent flex items-center gap-4';
const ariaId = `catalog-aggregation-dropdown-${index}`;
return (
);
};
const CatalogAggregations = (): JSX.Element => {
const { params, urlManager } = useCatalog();
const { aggregations, aggregationFilters, isCurated, token, tokenLabel } = params;
const { data } = useLanguagesQueryQuery();
const firstAggregationFilterLabel = aggregationFilters.length
? aggregationFilters[0].label
: undefined;
const languages = data ? data.Languages : [];
const contents = aggregations.map(({ label = '', buckets = [] }, index) => {
const isAggregationLabel = label === firstAggregationFilterLabel;
const isTokenLabel = tokenLabel && label === tokenLabel;
const isCurrentLabel = isAggregationLabel || isTokenLabel;
const shouldExpandFirst = !isCurated && !token;
const isExpanded = isCurrentLabel || (shouldExpandFirst && index === 0);
const aggregationBuckets = buckets.map(({ value = '', count, query }, bucketIndex) => {
const filter = { label, value };
const isLanguageLabel = query?.includes('language');
const languageLabel = languages.find(({ code }) => code === value)?.label || value;
const displayLabel = isLanguageLabel ? languageLabel : value;
const props = {
href: urlManager.composeURLForAddAggregationFilter(filter),
value: displayLabel,
count
};
return ;
});
const props = {
index,
defaultIsExpanded: isExpanded,
label,
aggregationBuckets
};
return ;
});
return <>{contents}>;
};
CatalogAggregations.displayName = 'CatalogAggregations';
export default CatalogAggregations;