From 2c232e74993a6ad50601076f7e9fcc23f22ab4ab Mon Sep 17 00:00:00 2001 From: Bram Adams <3282661+bramses@users.noreply.github.com> Date: Mon, 16 Sep 2024 13:34:14 -0400 Subject: [PATCH] refactor: Update EntryPage component to include related text for dynamic search --- src/components/EntryPage.tsx | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/components/EntryPage.tsx b/src/components/EntryPage.tsx index d55f032..77d35ee 100644 --- a/src/components/EntryPage.tsx +++ b/src/components/EntryPage.tsx @@ -88,6 +88,7 @@ const EntryPage = () => { ); const [isLinkModalOpen, setIsLinkModalOpen] = useState(false); const [links, setLinks] = useState([]); + const [relatedText, setRelatedText] = useState('Related Entries'); const openModal = (key: string) => setModalStates((prev) => ({ ...prev, [key]: true })); @@ -372,6 +373,28 @@ const EntryPage = () => { } }, [isLoaded, user]); + useEffect(() => { + const handleKeyDown = (event: KeyboardEvent) => { + if (event.key === 'k') { + console.log('handleKeyDown:', event.key); + const selectedText = window.getSelection()?.toString(); + console.log('selectedText:', selectedText); + if (selectedText) { + handleSearch(selectedText, ''); + if (relatedText === 'Related Entries') { + setRelatedText(`Using "K" to search: ${selectedText}`); + } + } + } + }; + + window.addEventListener('keydown', handleKeyDown); + + return () => { + window.removeEventListener('keydown', handleKeyDown); + }; + }, []); + useEffect(() => { if (data) { document.title = data.data; @@ -749,7 +772,7 @@ const EntryPage = () => { )} -

Related Entries

+

{relatedText}

{/* {JSON.stringify(searchResults)} */} {searchResults.map((result) => (