|
| 1 | +import React, { ChangeEvent, useCallback, useEffect, useState } from 'react'; |
| 2 | +import GuidesGrid from './GuidesGrid'; |
| 3 | +import GuidesFilter from './GuidesFilter'; |
| 4 | +import { ImageProps } from '../Image'; |
| 5 | +import patternGrid from './images/pattern-grid.svg'; |
| 6 | +import mobileGrid from './images/mobile-grid.svg'; |
| 7 | +import { guides } from '../../data/guides'; |
| 8 | +import { filterSearchGuides, SelectedFilters } from './filter-search-guides'; |
| 9 | +import GuidesNoResults from './GuidesNoResults'; |
| 10 | +import { useLocation } from '@reach/router'; |
| 11 | +import { GuideProduct, AIProvider } from '../../data/guides/types'; |
| 12 | +import { products } from '../../data/guides'; |
| 13 | + |
| 14 | +const GuidesContent = ({ guideImages }: { guideImages: ImageProps[] }) => { |
| 15 | + const location = useLocation(); |
| 16 | + |
| 17 | + // Parse product and provider query parameters |
| 18 | + const getInitialFilters = (): SelectedFilters => { |
| 19 | + const params = new URLSearchParams(location.search); |
| 20 | + const productParam = params.get('product'); |
| 21 | + const providerParam = params.get('provider'); |
| 22 | + const validProductNames = Object.keys(products); |
| 23 | + |
| 24 | + const initialProducts: GuideProduct[] = productParam |
| 25 | + ? productParam |
| 26 | + .split(',') |
| 27 | + .map((p) => p.trim()) |
| 28 | + .filter((product): product is GuideProduct => validProductNames.includes(product)) |
| 29 | + : []; |
| 30 | + |
| 31 | + const validProviderNames: AIProvider[] = ['anthropic', 'openai']; |
| 32 | + const initialProviders: AIProvider[] = providerParam |
| 33 | + ? providerParam |
| 34 | + .split(',') |
| 35 | + .map((p) => p.trim()) |
| 36 | + .filter((provider): provider is AIProvider => validProviderNames.includes(provider as AIProvider)) |
| 37 | + : []; |
| 38 | + |
| 39 | + return { |
| 40 | + products: initialProducts, |
| 41 | + aiProviders: initialProviders, |
| 42 | + }; |
| 43 | + }; |
| 44 | + |
| 45 | + const [selected, setSelected] = useState<SelectedFilters>(getInitialFilters); |
| 46 | + const [searchTerm, setSearchTerm] = useState(''); |
| 47 | + const [filteredGuides, setFilteredGuides] = useState(guides); |
| 48 | + |
| 49 | + const handleSearch = useCallback((e: ChangeEvent<HTMLInputElement>) => { |
| 50 | + setSearchTerm(e.target.value); |
| 51 | + }, []); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + const filtered = filterSearchGuides(guides, selected, searchTerm); |
| 55 | + setFilteredGuides(filtered); |
| 56 | + }, [selected, searchTerm]); |
| 57 | + |
| 58 | + return ( |
| 59 | + <> |
| 60 | + <section className="mx-auto px-6 md:px-0 relative"> |
| 61 | + <div className="w-full sm:w-1/2 max-w-[37.5rem] pt-20 sm:pt-24"> |
| 62 | + <h1 className="ui-text-title text-title">Guides</h1> |
| 63 | + <p className="ui-text-sub-header mt-4"> |
| 64 | + In-depth guides to help you build realtime features with Ably and understand how best to architect an app |
| 65 | + for your use case. |
| 66 | + </p> |
| 67 | + </div> |
| 68 | + <div className="w-full my-10 sm:my-16 flex flex-col sm:flex-row gap-x-10"> |
| 69 | + <div className="w-full sm:w-[20%] relative"> |
| 70 | + <GuidesFilter selected={selected} setSelected={setSelected} handleSearch={handleSearch} /> |
| 71 | + </div> |
| 72 | + <div className="w-full sm:w-[80%] mt-10 sm:mt-0"> |
| 73 | + {filteredGuides.length > 0 ? ( |
| 74 | + <GuidesGrid guideImages={guideImages} guides={filteredGuides} searchTerm={searchTerm} /> |
| 75 | + ) : ( |
| 76 | + <GuidesNoResults /> |
| 77 | + )} |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + </section> |
| 81 | + |
| 82 | + <img |
| 83 | + src={patternGrid} |
| 84 | + alt="" |
| 85 | + aria-hidden="true" |
| 86 | + className="absolute -z-10 right-0 top-16 hidden sm:block w-[60%] md:w-[40%] pointer-events-none" |
| 87 | + /> |
| 88 | + |
| 89 | + <img |
| 90 | + src={mobileGrid} |
| 91 | + alt="" |
| 92 | + aria-hidden="true" |
| 93 | + className="absolute -z-10 right-0 top-16 block sm:hidden pointer-events-none" |
| 94 | + /> |
| 95 | + </> |
| 96 | + ); |
| 97 | +}; |
| 98 | + |
| 99 | +export default GuidesContent; |
0 commit comments