"use client"; import React, { ReactElement, useState } from "react"; import { DocsContentMetadata } from "@/app/common/documentation"; import { CommandDialog, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/app/components/ui/command"; import { Button, ButtonProps } from "@/app/components/ui/button"; import { useRouter } from "next/navigation"; import { cn } from "@/app/common/utils"; export function CommandMenu({ ...props }: ButtonProps): ReactElement { const router = useRouter(); /** * Whether to show the search */ const [open, setOpen] = useState(false); /** * The pages that were found */ const [pages, setPages] = useState(undefined); // Handle keyboard shortcuts React.useEffect(() => { const down = (e: KeyboardEvent) => { if ((e.key === "k" && (e.metaKey || e.ctrlKey)) || e.key === "/") { if ( (e.target instanceof HTMLElement && e.target.isContentEditable) || e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement || e.target instanceof HTMLSelectElement ) { return; } e.preventDefault(); setOpen(open => !open); } }; return () => document.removeEventListener("keydown", down); }, []); /** * Search the documentation * for the given query. * * @param query the query to search for */ async function searchDocs(query: string): Promise { // Don't bother searching if the query is less than 3 characters if (query.length < 3) { setPages(undefined); return; } // Attempt to search for the query const response = await fetch(`/api/docs/search?query=${query}`); const pages: DocsContentMetadata[] = await response.json(); setPages(pages); } return ( <> { await searchDocs(search); }} /> No results found. {pages && ( {pages.map(page => { return ( { router.push(`/docs/${page.slug}`); setOpen(false); }} > {page.title} ); })} )} ); }