Skip to content

Commit

Permalink
fix: only show enabled modules in global search bar when no results a…
Browse files Browse the repository at this point in the history
…vailable
  • Loading branch information
ewan-escience committed Jul 16, 2024
1 parent ed4caf7 commit cdc410f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
5 changes: 3 additions & 2 deletions frontend/components/AppHeader/isActiveMenuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-FileCopyrightText: 2023 - 2024 Netherlands eScience Center
// SPDX-FileCopyrightText: 2023 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2023 Netherlands eScience Center
// SPDX-FileCopyrightText: 2024 Ewan Cahen (Netherlands eScience Center) <e.cahen@esciencecenter.nl>
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -15,7 +16,7 @@ export default function isActiveMenuItem({item, activePath}:IsActiveMenuItemProp
if (activePath && item.match) {
// console.log('activePath...', activePath)
// console.log('match...', item.match)
// using startsWith to be activate root of the tree
// using startsWith to activate root of the tree
return activePath.startsWith(item.match)
}

Expand Down
22 changes: 16 additions & 6 deletions frontend/components/GlobalSearchAutocomplete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import BusinessIcon from '@mui/icons-material/Business'
import Diversity3Icon from '@mui/icons-material/Diversity3'
import logger from '~/utils/logger'
import useSnackbar from '~/components/snackbar/useSnackbar'
import {useModules} from '~/config/useModules'

type Props = {
className?: string
Expand All @@ -37,6 +38,7 @@ export default function GlobalSearchAutocomplete(props: Props) {
const [hasResults, setHasResults] = useState(true)
const [searchResults, setSearchResults] = useState<GlobalSearchResults[]>([])
const [searchCombo, setSearchCombo] = useState('Ctrl K')
const {isModuleEnabled} = useModules()

const lastValue = useDebounce(inputValue, 150)
const inputRef = useRef<HTMLInputElement>(null)
Expand Down Expand Up @@ -64,12 +66,20 @@ export default function GlobalSearchAutocomplete(props: Props) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [lastValue])

const defaultValues: GlobalSearchResults[] = [
{name: 'Go to Software page', slug: '', source: 'software'},
{name: 'Go to Projects page', slug: '', source: 'projects'},
{name: 'Go to Organisations page', slug: '', source: 'organisations'},
{name: 'Go to Communities page', slug: '', source: 'communities'},
]
const defaultValues: GlobalSearchResults[] = []

if (isModuleEnabled('software')) {
defaultValues.push({name: 'Go to Software page', slug: '', source: 'software'})
}
if (isModuleEnabled('projects')) {
defaultValues.push({name: 'Go to Projects page', slug: '', source: 'projects'})
}
if (isModuleEnabled('organisations')) {
defaultValues.push({name: 'Go to Organisations page', slug: '', source: 'organisations'})
}
if (isModuleEnabled('communities')) {
defaultValues.push({name: 'Go to Communities page', slug: '', source: 'communities'})
}

async function fetchData(search: string) {
// Fetch api
Expand Down
2 changes: 1 addition & 1 deletion frontend/config/defaultSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@
"fontWeightBold": 600
}
}
}
}
1 change: 1 addition & 0 deletions frontend/config/defaultSettings.json.license
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ SPDX-FileCopyrightText: 2022 - 2023 Dusan Mijatovic (dv4all)
SPDX-FileCopyrightText: 2022 - 2023 dv4all
SPDX-FileCopyrightText: 2023 - 2024 Dusan Mijatovic (Netherlands eScience Center)
SPDX-FileCopyrightText: 2023 - 2024 Netherlands eScience Center
SPDX-FileCopyrightText: 2024 Ewan Cahen (Netherlands eScience Center) <e.cahen@esciencecenter.nl>

SPDX-License-Identifier: Apache-2.0
24 changes: 24 additions & 0 deletions frontend/config/useModules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-FileCopyrightText: 2024 Ewan Cahen (Netherlands eScience Center) <e.cahen@esciencecenter.nl>
// SPDX-FileCopyrightText: 2024 Netherlands eScience Center
//
// SPDX-License-Identifier: Apache-2.0

import useRsdSettings from '~/config/useRsdSettings'

export function useModules() {
const {host} = useRsdSettings()

// TODO: change the type of the module parameter to RsdModule when the modules news and user are also in the default settings
function isModuleEnabled(module: 'software'| 'projects' | 'organisations' | 'communities'): boolean {
if (host?.modules && host?.modules?.length > 0){
// include only options defined for this RSD host
return host.modules.includes(module)
}
// else all menuItems are allowed by default
return true
}

return ({
isModuleEnabled
})
}

0 comments on commit cdc410f

Please sign in to comment.