Skip to content

Commit

Permalink
refactor: Update EntryPage component to include related text for dyna…
Browse files Browse the repository at this point in the history
…mic search
  • Loading branch information
bramses committed Sep 16, 2024
1 parent fcf51fa commit 2c232e7
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/components/EntryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const EntryPage = () => {
);
const [isLinkModalOpen, setIsLinkModalOpen] = useState(false);
const [links, setLinks] = useState<any[]>([]);
const [relatedText, setRelatedText] = useState('Related Entries');

const openModal = (key: string) =>
setModalStates((prev) => ({ ...prev, [key]: true }));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -749,7 +772,7 @@ const EntryPage = () => {
</div>
)}

<h2 className="my-4 text-4xl font-extrabold">Related Entries</h2>
<h2 className="my-4 text-4xl font-extrabold">{relatedText}</h2>
{/* <span>{JSON.stringify(searchResults)}</span> */}
{searchResults.map((result) => (
<div key={result.id}>
Expand Down

0 comments on commit 2c232e7

Please sign in to comment.