import React, { useEffect, useRef, useState } from 'react' import { ArticleGuide, useProductGuidesContext } from 'components/context/ProductGuidesContext' import { useTranslation } from 'components/hooks/useTranslation' import { ArticleCard } from './ArticleCard' import { DropdownMenu } from '@primer/react' import { ItemInput } from '@primer/react/lib/ActionList/List' const PAGE_SIZE = 9 export const ArticleCards = () => { const { t } = useTranslation('product_guides') const guideTypes: Record = t('guide_types') const { allTopics, includeGuides } = useProductGuidesContext() const [numVisible, setNumVisible] = useState(PAGE_SIZE) const [typeFilter, setTypeFilter] = useState() const [topicFilter, setTopicFilter] = useState() const [filteredResults, setFilteredResults] = useState>([]) const typesRef = useRef(null) const topicsRef = useRef(null) const articleCardRef = useRef(null) useEffect(() => { setNumVisible(PAGE_SIZE) setFilteredResults( (includeGuides || []).filter((card) => { const matchesType = card.type === typeFilter?.key const matchesTopic = card.topics.some((key) => key === topicFilter?.key) return (typeFilter?.key ? matchesType : true) && (topicFilter?.key ? matchesTopic : true) }) ) }, [typeFilter, topicFilter]) const clickDropdown = (e: React.RefObject) => { if (e === typesRef && typesRef.current) typesRef.current.focus() if (e === topicsRef && topicsRef.current) topicsRef.current.focus() } const loadMore = () => { if (articleCardRef.current) { const childListLength = articleCardRef.current.childElementCount // Leading semi-colon due to prettier to prevent possible ASI failures // Need to explicitly type assert as HTMLDivElement as focus property missing from dom type definitions for Element. ;(articleCardRef.current.childNodes.item(childListLength - 1) as HTMLDivElement).focus() } setNumVisible(numVisible + PAGE_SIZE) } const isUserFiltering = typeFilter !== undefined || topicFilter !== undefined const guides = isUserFiltering ? filteredResults : includeGuides || [] const types = Object.entries(guideTypes).map(([key, val]) => { return { text: val, key } }) as ItemInput[] types.unshift({ text: t('filters.all'), key: undefined }) const topics = allTopics?.map((topic) => { return { text: topic, key: topic } }) as ItemInput[] topics.unshift({ text: t('filters.all'), key: undefined }) return (
clickDropdown(typesRef)} onKeyDown={() => clickDropdown(typesRef)} role="button" tabIndex={-1} className="text-uppercase f6 color-fg-muted d-block" > {t('filters.type')}
clickDropdown(topicsRef)} onKeyDown={() => clickDropdown(topicsRef)} role="button" tabIndex={-1} className="text-uppercase f6 color-fg-muted d-block" > {t('filters.topic')}
{guides.length === 0 ? t('guides_found.none') : guides.length === 1 ? t('guides_found.one') : t('guides_found.multiple').replace('{n}', guides.length)}
    {guides.slice(0, numVisible).map((card) => { return ( ) })}
{guides.length > numVisible && ( )}
) }