-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62a57c5
commit d0a5d2d
Showing
9 changed files
with
901 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
import { useEffect, useRef, useState } from 'react' | ||
import Image from 'next/image' | ||
import Link from 'next/link' | ||
|
||
import { searchClient } from 'utils/meilisearchClient' | ||
import { | ||
InstantSearch, | ||
Highlight, | ||
Configure, | ||
connectStateResults, | ||
connectSearchBox, | ||
connectHits | ||
} from 'react-instantsearch-dom' | ||
import { getImageUrl } from 'utils/getImageUrl' | ||
import formatPrice from 'utils/format-price' | ||
|
||
import { GameFragment_cover } from 'graphql/generated/GameFragment' | ||
|
||
import { | ||
Search as SearchIcon, | ||
Close as CloseIcon | ||
} from '@styled-icons/material-outlined' | ||
import { Apple, Windows, Linux } from '@styled-icons/fa-brands' | ||
import * as S from './styles' | ||
import Button from 'components/Button' | ||
|
||
const Search = () => { | ||
const [isOpen, setIsOpen] = useState(false) | ||
|
||
useEffect(() => { | ||
document.body.style.overflow = isOpen ? 'hidden' : 'unset' | ||
|
||
return () => { | ||
document.body.style.overflow = 'unset' | ||
} | ||
}, [isOpen]) | ||
|
||
return ( | ||
<S.Wrapper isOpen={isOpen}> | ||
<InstantSearch indexName="game" searchClient={searchClient}> | ||
<SearchBox | ||
handleVisibility={() => setIsOpen(!isOpen)} | ||
isOpen={isOpen} | ||
/> | ||
{/** Maximum number of results */} | ||
<Configure hitsPerPage={12} /> | ||
<Results /> | ||
</InstantSearch> | ||
<S.Overlay onClick={() => setIsOpen(!isOpen)} aria-hidden={!isOpen} /> | ||
</S.Wrapper> | ||
) | ||
} | ||
|
||
export default Search | ||
|
||
export type SearchBoxProps = { | ||
handleVisibility: () => void | ||
isOpen: boolean | ||
currentRefinement: string | ||
refine: (v: string) => void | ||
} | ||
|
||
const SearchBox = connectSearchBox( | ||
({ handleVisibility, isOpen, currentRefinement, refine }: SearchBoxProps) => { | ||
const inputRef = useRef<HTMLInputElement>(null) | ||
|
||
useEffect(() => { | ||
if (isOpen && inputRef.current) { | ||
inputRef.current.focus() | ||
} | ||
}, [isOpen]) | ||
|
||
return ( | ||
<S.SearchForm role="search"> | ||
<S.InputWrapper isOpen={isOpen}> | ||
<S.Input | ||
type="search" | ||
ref={inputRef} | ||
placeholder="Search here…" | ||
autoComplete="off" | ||
autoCorrect="off" | ||
autoCapitalize="off" | ||
spellCheck="false" | ||
value={currentRefinement} | ||
onChange={(event) => refine(event.currentTarget.value)} | ||
/> | ||
|
||
<S.Icon> | ||
{isOpen ? ( | ||
<CloseIcon onClick={handleVisibility} aria-label="Close Search" /> | ||
) : ( | ||
<SearchIcon onClick={handleVisibility} aria-label="Open Search" /> | ||
)} | ||
</S.Icon> | ||
</S.InputWrapper> | ||
</S.SearchForm> | ||
) | ||
} | ||
) | ||
|
||
const Results = connectStateResults(({ searchState }) => | ||
searchState?.query ? <Hits /> : null | ||
) | ||
|
||
type Platform = { | ||
name: 'windows' | 'linux' | 'mac' | ||
} | ||
|
||
export type GameHitProps = { | ||
id: string | ||
name: string | ||
short_description: string | ||
cover: GameFragment_cover | null | ||
slug: string | ||
price: number | ||
platforms: Platform[] | ||
release_date: string | null | ||
} | ||
|
||
export type HitsProps = { | ||
hits: GameHitProps[] | ||
} | ||
|
||
const Hits = connectHits(({ hits }: HitsProps) => ( | ||
<S.List> | ||
{hits.length ? ( | ||
hits.map((hit) => ( | ||
<S.ListItem key={hit.id}> | ||
<Hit hit={hit} /> | ||
</S.ListItem> | ||
)) | ||
) : ( | ||
<NoResults /> | ||
)} | ||
</S.List> | ||
)) | ||
|
||
export type HitProps = { | ||
hit: GameHitProps | ||
} | ||
|
||
const Hit = ({ hit }: HitProps) => { | ||
const platformIcons = { | ||
linux: <Linux title="Linux" />, | ||
mac: <Apple title="Mac" />, | ||
windows: <Windows title="Windows" /> | ||
} | ||
const releaseYear = | ||
hit.release_date && new Date(hit.release_date).getFullYear() | ||
|
||
return ( | ||
<Link href={`game/${hit.slug}`} passHref> | ||
<S.Result> | ||
<S.ImageWrapper> | ||
<Image | ||
src={`${getImageUrl(hit.cover?.url)}`} | ||
alt={hit.name} | ||
layout="fill" | ||
objectFit="cover" | ||
/> | ||
</S.ImageWrapper> | ||
<S.Info> | ||
<S.Title> | ||
<Highlight attribute="name" hit={hit} /> | ||
{releaseYear && ( | ||
<S.ReleaseYear | ||
itemProp="releaseYear" | ||
dateTime={hit.release_date!} | ||
> | ||
{releaseYear} | ||
</S.ReleaseYear> | ||
)} | ||
</S.Title> | ||
<S.Details> | ||
<S.Price>{formatPrice(hit.price)}</S.Price> | ||
<S.Platform> | ||
{hit.platforms.map((icon: Platform) => ( | ||
<S.PlatformIcon key={`${hit.id}${icon.name}`}> | ||
{platformIcons[icon.name]} | ||
</S.PlatformIcon> | ||
))} | ||
</S.Platform> | ||
</S.Details> | ||
<S.Description> | ||
<Highlight attribute="short_description" hit={hit} /> | ||
</S.Description> | ||
</S.Info> | ||
</S.Result> | ||
</Link> | ||
) | ||
} | ||
|
||
const NoResults = () => ( | ||
<S.NoResultsWrapper> | ||
<Image | ||
src="/img/empty.svg" | ||
alt="A gamer in a couch playing videogame" | ||
width={110} | ||
height={110} | ||
/> | ||
|
||
<S.NoResultsInfo> | ||
<S.NoResultsTitle> | ||
<SearchIcon size={24} /> No Results Found | ||
</S.NoResultsTitle> | ||
<S.NoResultsDescription> | ||
Try searching another term. | ||
</S.NoResultsDescription> | ||
<Link href="/" passHref> | ||
<Button as="a">Explore all games </Button> | ||
</Link> | ||
</S.NoResultsInfo> | ||
</S.NoResultsWrapper> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Story, Meta } from '@storybook/react/types-6-0' | ||
import Search from '.' | ||
|
||
export default { | ||
title: 'Search', | ||
component: Search, | ||
parameters: { | ||
backgrounds: { | ||
default: 'won-dark' | ||
} | ||
} | ||
} as Meta | ||
|
||
export const Default: Story = () => <Search /> |
Oops, something went wrong.