import { useState } from 'react' import { ArrowRightIcon, SearchIcon } from '@primer/octicons-react' import { useProductLandingContext } from 'components/context/ProductLandingContext' import { useTranslation } from 'components/hooks/useTranslation' import { CodeExampleCard } from 'components/landing/CodeExampleCard' import { Link } from 'components/Link' const PAGE_SIZE = 6 export const CodeExamples = () => { const { productCodeExamples } = useProductLandingContext() const { t } = useTranslation('product_landing') const [numVisible, setNumVisible] = useState(PAGE_SIZE) const [search, setSearch] = useState('') const onSearchChange: React.ChangeEventHandler = (e) => { setSearch(e.target.value) setNumVisible(PAGE_SIZE) // reset the visible count (only matters after searching) } const isSearching = !!search let searchResults: typeof productCodeExamples = [] if (isSearching) { const matchReg = new RegExp(search, 'i') searchResults = productCodeExamples.filter((example) => { const searchableStr = `${example.tags.join(' ')} ${example.title} ${example.description}` return matchReg.test(searchableStr) }) } return (
{(isSearching ? searchResults : productCodeExamples.slice(0, numVisible)).map((example) => { return (
) })}
{numVisible < productCodeExamples.length && !isSearching && ( )} {isSearching && searchResults.length === 0 && (
{' '}

{t('sorry')} {search}

{t('no_example')}
{t('try_another')}

{t('learn')}
)}
) }