From ddb5c9ed2ff7ac8869cfeb1be6eac0266bdb9b7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Mee=C3=9Fen?= <14222414+cmeessen@users.noreply.github.com> Date: Fri, 22 Mar 2024 14:18:52 +0100 Subject: [PATCH] feat: global search accessible via Ctrl K / Meta K --- .../GlobalSearchAutocomplete/index.tsx | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/frontend/components/GlobalSearchAutocomplete/index.tsx b/frontend/components/GlobalSearchAutocomplete/index.tsx index 3c16fc73c..262ff34cc 100644 --- a/frontend/components/GlobalSearchAutocomplete/index.tsx +++ b/frontend/components/GlobalSearchAutocomplete/index.tsx @@ -2,11 +2,13 @@ // SPDX-FileCopyrightText: 2022 - 2023 dv4all // SPDX-FileCopyrightText: 2023 - 2024 Netherlands eScience Center // SPDX-FileCopyrightText: 2023 Dusan Mijatovic (Netherlands eScience Center) +// SPDX-FileCopyrightText: 2024 Christian Meeßen (GFZ) // SPDX-FileCopyrightText: 2024 Ewan Cahen (Netherlands eScience Center) +// SPDX-FileCopyrightText: 2024 Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences // // SPDX-License-Identifier: Apache-2.0 -import React, {useEffect, useState} from 'react' +import React, {useCallback, useEffect, useRef, useState} from 'react' import {ClickAwayListener} from '@mui/base' import {useRouter} from 'next/router' @@ -31,8 +33,10 @@ export default function GlobalSearchAutocomplete(props: Props) { const [selected, setSelected] = useState(0) const [hasResults, setHasResults] = useState(true) const [searchResults, setSearchResults] = useState([]) + const [searchCombo, setSearchCombo] = useState('Ctrl K') const lastValue = useDebounce(inputValue, 150) + const inputRef = useRef(null) // console.group('GlobalSearchAutocomplete') // console.log('inputValue...', inputValue) @@ -129,6 +133,30 @@ export default function GlobalSearchAutocomplete(props: Props) { } } + useEffect(() => { + if (navigator === undefined || navigator.userAgent === undefined) { + return + } else if (/(Mac|iPhone|iPod|iPad)/i.test(navigator.userAgent)) { + setSearchCombo('⌘ K') + } + }, []) + + const handleCtrlK = useCallback((event: KeyboardEvent) => { + if (event.ctrlKey || event.metaKey) { + if (event.key?.toLowerCase() === 'k') { + event.preventDefault() + inputRef.current?.focus() + } + } + }, [inputRef]) + + useEffect(() => { + window.addEventListener('keydown', handleCtrlK) + return () => { + window.removeEventListener('keydown', handleCtrlK) + } + },[handleCtrlK]) + return ( { setOpen(false) @@ -144,6 +172,7 @@ export default function GlobalSearchAutocomplete(props: Props) { + {isOpen &&