From 66a770faec2478cda0b60c03a84418ad8f631dac Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 27 May 2024 01:47:22 -0500 Subject: [PATCH] search --- app/src/components/SearchBar.module.scss | 36 ++++++++++++++- app/src/components/SearchBar.tsx | 58 ++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 7 deletions(-) diff --git a/app/src/components/SearchBar.module.scss b/app/src/components/SearchBar.module.scss index b802f46..5544b4d 100644 --- a/app/src/components/SearchBar.module.scss +++ b/app/src/components/SearchBar.module.scss @@ -6,10 +6,15 @@ border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; - min-width: 500px; + width: 500px; min-height: 200px; - padding: 12px; + padding: 6px; + + text-wrap: wrap; + white-space: pre-wrap; + word-wrap: break-word; + overflow-wrap: break-word; } .entry { @@ -18,4 +23,31 @@ border-radius: 4px; border: none; outline: none; +} + +.searchResults { + display: flex; + flex-direction: column; + align-items: stretch; +} + +.searchResult { + padding: 12px; + cursor: pointer; + border-radius: 4px; + text-align: left; + background-color: unset; + border: none; + + &:hover { + background-color: rgba(0, 0, 0, 0.05); + } + + .title { + font-size: 1rem; + } + + .subtitle { + font-size: .75rem; + } } \ No newline at end of file diff --git a/app/src/components/SearchBar.tsx b/app/src/components/SearchBar.tsx index 9d81ebb..58c9ed2 100644 --- a/app/src/components/SearchBar.tsx +++ b/app/src/components/SearchBar.tsx @@ -11,10 +11,18 @@ import { useFocus, useInteractions, } from "@floating-ui/react"; -import { useState } from "react"; +import { useDebounce } from "use-debounce"; +import { useEffect, useState } from "react"; +import { atom, useAtom, useSetAtom } from "jotai"; +import { nodesOpenedAtom } from "../App"; + +const searchQueryAtom = atom(""); +const showMenuAtom = atom(false); export default function SearchBar() { - const [showMenu, setShowMenu] = useState(() => false); + const [showMenu, setShowMenu] = useAtom(showMenuAtom); + const [searchQuery, setSearchQuery] = useAtom(searchQueryAtom); + const [searchResults, setSearchResults] = useState([]); const { refs, context, floatingStyles } = useFloating({ placement: "bottom-start", @@ -29,6 +37,22 @@ export default function SearchBar() { useDismiss(context), ]); + useEffect(() => { + setSearchResults([]); + const trimmed = searchQuery.trim(); + if (trimmed === "") return; + + (async () => { + const params = new URLSearchParams(); + params.set("query", trimmed); + const resp = await fetch( + `http://localhost:5195/node/search?${params.toString()}`, + ); + const data = await resp.json(); + setSearchResults(data.results); + })(); + }, [searchQuery]); + return ( <>
@@ -38,6 +62,8 @@ export default function SearchBar() { placeholder="Search..." onFocus={() => setShowMenu(true)} ref={refs.setReference} + value={searchQuery} + onChange={(evt) => setSearchQuery(evt.target.value)} {...getReferenceProps()} />
@@ -51,7 +77,7 @@ export default function SearchBar() { style={{ ...floatingStyles }} {...getFloatingProps()} > - + @@ -60,6 +86,28 @@ export default function SearchBar() { ); } -function SearchMenu() { - return <>Search suggestions...; +function SearchMenu({ results }) { + const setNodesOpened = useSetAtom(nodesOpenedAtom); + const setSearchQuery = useSetAtom(searchQueryAtom); + const setShowMenu = useSetAtom(showMenuAtom); + + return ( +
+ {results.map((result) => ( + + ))} +
+ ); }