From a4e57260202a3b6d2d49e4bfd18698be42fe89c2 Mon Sep 17 00:00:00 2001 From: Greg Richardson Date: Sat, 18 Feb 2023 00:14:19 -0700 Subject: [PATCH] feat(docs-search): search result ui with 2-tier hierarchy --- apps/docs/components/Clippy/ClippyModal.tsx | 99 ++++++++++++++++++++- 1 file changed, 95 insertions(+), 4 deletions(-) diff --git a/apps/docs/components/Clippy/ClippyModal.tsx b/apps/docs/components/Clippy/ClippyModal.tsx index e5795b06273..f83acf6b75b 100644 --- a/apps/docs/components/Clippy/ClippyModal.tsx +++ b/apps/docs/components/Clippy/ClippyModal.tsx @@ -6,8 +6,10 @@ import { SSE } from 'sse.js' import clippyImageDark from '../../public/img/clippy-dark.png' import clippyImage from '../../public/img/clippy.png' +import { useSupabaseClient } from '@supabase/auth-helpers-react' import { useTheme } from 'common/Providers' import Image from 'next/image' +import Link from 'next/link' import { Button, IconAlertCircle, @@ -51,10 +53,12 @@ const ClippyModal: FC = ({ onClose }) => { const { isDarkMode } = useTheme() const [query, setQuery] = useState('') const [answer, setAnswer] = useState('') + const [results, setResults] = useState([]) const [isLoading, setIsLoading] = useState(false) const [isResponding, setIsResponding] = useState(false) const [hasError, setHasError] = useState(false) const eventSourceRef = useRef() + const supabaseClient = useSupabaseClient() const cantHelp = answer?.trim() === "Sorry, I don't know how to help with that." const status = isLoading @@ -65,7 +69,33 @@ const ClippyModal: FC = ({ onClose }) => { ? 'Clippy has failed you' : undefined - const handleConfirm = useCallback(async (query: string) => { + const handleSearchConfirm = useCallback( + async (query: string) => { + setHasError(false) + setAnswer(undefined) + setIsLoading(true) + + const { error, data: pageSections } = await supabaseClient.functions.invoke('search', { + body: { query }, + }) + + setIsLoading(false) + + if (error) { + setIsLoading(false) + setIsResponding(false) + setHasError(true) + console.error(error) + return + } + + setResults(pageSections) + console.log(pageSections) + }, + [supabaseClient] + ) + + const handleClippyConfirm = useCallback(async (query: string) => { setHasError(false) setAnswer(undefined) setIsLoading(true) @@ -143,7 +173,7 @@ const ClippyModal: FC = ({ onClose }) => { onKeyDown={(e) => { switch (e.key) { case 'Enter': - handleConfirm(query) + handleSearchConfirm(query) return default: return @@ -164,7 +194,7 @@ const ClippyModal: FC = ({ onClose }) => { )} - {!isLoading && !answer && !hasError && ( + {!isLoading && !answer && !hasError && results.length === 0 && (

Not sure where to start?

@@ -176,7 +206,7 @@ const ClippyModal: FC = ({ onClose }) => { className="hover:bg-slate-400 hover:dark:bg-slate-400 px-4 py-2 bg-slate-300 dark:bg-slate-200 rounded-lg transition-colors" onClick={() => { setQuery(question) - handleConfirm(question) + handleClippyConfirm(question) }} > {question} @@ -187,6 +217,67 @@ const ClippyModal: FC = ({ onClose }) => {
)} + {results.length > 0 && ( +
+ {results.map((page) => { + const pageSections = page.sections.filter((section) => !!section.heading) + return ( +
+ + +
+ + + + +
+
{page.meta.title}
+
+ + {pageSections.length > 0 && ( +
+
+
+ {pageSections.map((section) => ( + + +
+ + + +
+
+
+ {page.meta.title} +
+
{section.heading}
+
+
+ + ))} +
+
+ )} +
+ ) + })} +
+ )} {answer && (
{cantHelp ? (