Skip to content

[Enhancement]: When selecting item from search bar, it will take you directly to item's game page #2614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/frontend/components/UI/SearchBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import React, {
useRef
} from 'react'
import { useTranslation } from 'react-i18next'
import { useNavigate } from 'react-router-dom'
import ContextProvider from 'frontend/state/ContextProvider'
import './index.css'
import { faXmark } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { GameInfo } from '../../../../common/types'

function fixFilter(text: string) {
const regex = new RegExp(/([?\\|*|+|(|)|[|]|])+/, 'g')
Expand All @@ -21,6 +23,8 @@ export default React.memo(function SearchBar() {
const { handleSearch, filterText, epic, gog, sideloadedLibrary } =
useContext(ContextProvider)
const { t } = useTranslation()
const navigate = useNavigate()

const input = useRef<HTMLInputElement>(null)

const list = useMemo(() => {
Expand Down Expand Up @@ -61,12 +65,35 @@ export default React.memo(function SearchBar() {
}, [input])

const handleClick = (title: string) => {
handleSearch('')
if (input.current) {
input.current.value = title
handleSearch(title)
input.current.value = ''

const game: GameInfo | undefined = getGameInfoByAppTitle(title)

if (game !== undefined) {
navigate(`/gamepage/${game.runner}/${game.app_name}`, {
state: { gameInfo: game }
})
}
}
}

const getGameInfoByAppTitle = (title: string) => {
return (
getGameInfoByAppTitleAndLibrary(epic.library, title) ||
getGameInfoByAppTitleAndLibrary(gog.library, title) ||
getGameInfoByAppTitleAndLibrary(sideloadedLibrary, title)
)
}

const getGameInfoByAppTitleAndLibrary = (
library: GameInfo[],
title: string
) => {
return library.filter((g: GameInfo) => g.title === title).at(0)
}

return (
<div className="SearchBar" data-testid="searchBar">
<span className="searchButton" tabIndex={-1}>
Expand Down