From cd8dfd0c524bae1148b848f155d1e36f6afa122e Mon Sep 17 00:00:00 2001 From: Hector Alfaro Date: Thu, 14 Nov 2024 12:38:58 -0500 Subject: [PATCH] Add search to Copilot Cookbook (#53108) --- .../context/CategoryLandingContext.tsx | 16 +------- src/landings/components/CategoryLanding.tsx | 26 +++++++++++-- src/landings/components/CookBookFilter.tsx | 37 +++++++++++++++---- src/landings/types.ts | 18 +++++++++ 4 files changed, 71 insertions(+), 26 deletions(-) create mode 100644 src/landings/types.ts diff --git a/src/frame/components/context/CategoryLandingContext.tsx b/src/frame/components/context/CategoryLandingContext.tsx index 38360a030c06..3434ae772ba3 100644 --- a/src/frame/components/context/CategoryLandingContext.tsx +++ b/src/frame/components/context/CategoryLandingContext.tsx @@ -5,21 +5,7 @@ import { FeaturedLink, getFeaturedLinksFromReq, } from 'src/landings/components/ProductLandingContext' - -export type TocItem = { - fullPath: string - title: string - intro?: string - childTocItems?: Array<{ - fullPath: string - title: string - intro: string - octicon?: string - category?: string[] - complexity?: string[] - industry?: string[] - }> -} +import { TocItem } from '#src/landings/types.ts' export type CategoryLandingContextT = { title: string diff --git a/src/landings/components/CategoryLanding.tsx b/src/landings/components/CategoryLanding.tsx index f47a0c6d14ba..b3b4913f00c9 100644 --- a/src/landings/components/CategoryLanding.tsx +++ b/src/landings/components/CategoryLanding.tsx @@ -1,3 +1,4 @@ +import { useState } from 'react' import { useRouter } from 'next/router' import cx from 'classnames' import { CookBookArticleCard } from './CookBookArticleCard' @@ -11,6 +12,7 @@ import { useCategoryLandingContext } from 'src/frame/components/context/Category import { ClientSideRedirects } from 'src/rest/components/ClientSideRedirects' import { RestRedirect } from 'src/rest/components/RestRedirect' import { Breadcrumbs } from 'src/frame/components/page-header/Breadcrumbs' +import { ArticleCardItems } from '../types' export const CategoryLanding = () => { const router = useRouter() @@ -18,7 +20,23 @@ export const CategoryLanding = () => { const { title, intro, tocItems } = useCategoryLandingContext() // tocItems contains directories and its children, we only want the child articles - const onlyFlatItems = tocItems.flatMap((item) => item.childTocItems || []) + const onlyFlatItems: ArticleCardItems = tocItems.flatMap((item) => item.childTocItems || []) + + const [searchResults, setSearchResults] = useState(onlyFlatItems) + + const handleSearch = (query: string) => { + const results = onlyFlatItems.filter((token) => { + return Object.values(token).some((value) => { + if (typeof value === 'string') { + return value.toLowerCase().includes(query.toLowerCase()) + } else if (Array.isArray(value)) { + return value.some((item) => item.toLowerCase().includes(query.toLowerCase())) + } + return false + }) + }) + setSearchResults(results) + } return ( @@ -44,14 +62,14 @@ export const CategoryLanding = () => {
-

Explore {onlyFlatItems.length} prompt articles

+

Explore {searchResults.length} prompt articles

- +
    - {onlyFlatItems.map((item, index) => ( + {searchResults.map((item, index) => (
  • void + isSearchOpen?: boolean +} + +export const CookBookFilter = ({ onSearch, isSearchOpen }: Props) => { + const inputRef = useRef(null) + useEffect(() => { + if (isSearchOpen) { + inputRef.current?.focus() + } + }, [isSearchOpen]) -export const CookBookFilter = () => { return ( <>
    - +
    e.preventDefault()}> + { + const query = e.target.value || '' + onSearch(query) + }} + /> +
    diff --git a/src/landings/types.ts b/src/landings/types.ts new file mode 100644 index 000000000000..35a2a8fd7bd8 --- /dev/null +++ b/src/landings/types.ts @@ -0,0 +1,18 @@ +export type BaseTocItem = { + fullPath: string + title: string + intro?: string +} + +export type ChildTocItem = BaseTocItem & { + octicon?: string + category?: string[] + complexity?: string[] + industry?: string[] +} + +export type TocItem = BaseTocItem & { + childTocItems?: ChildTocItem[] +} + +export type ArticleCardItems = ChildTocItem[]