-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Dodano wyszukiwarkę Algolia do strony (#124)
* Add algolia search * Code review * Code review * Simple version of hits * Create custom algolia components * Code review * Code review * Change fading behaviour of hits * Add optional chaining operator * Fix * Quickfixes * Code review * Code review * Delete unused code * Add translation * Code review * CSS changes * Fix * Remove unused css classes * Change article reference Co-authored-by: Michał Miszczyszyn <mmiszy@users.noreply.github.com> Co-authored-by: Adam Siekierski <a@siekierski.ml>
- Loading branch information
1 parent
211fb77
commit 352a60e
Showing
14 changed files
with
410 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.heading { | ||
margin-top: 0; | ||
} | ||
|
||
.favicon { | ||
margin-right: 0.5rem; | ||
} | ||
|
||
.articleRef { | ||
display: block; | ||
} |
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,48 @@ | ||
import Link from 'next/link'; | ||
|
||
import styles from './AlgoliaHit.module.css'; | ||
|
||
export type Hit = { | ||
readonly objectID: string; | ||
readonly href: string; | ||
readonly publishedAt: string; | ||
readonly slug?: string; | ||
readonly description?: string; | ||
readonly title: string; | ||
readonly blog: { | ||
readonly name: string; | ||
readonly href: string; | ||
readonly favicon?: string; | ||
}; | ||
}; | ||
|
||
type HitProps = { | ||
readonly hit: Hit; | ||
}; | ||
|
||
export const AlgoliaHit = ({ hit }: HitProps) => ( | ||
<> | ||
<h3 className={styles.heading}>{hit.title}</h3> | ||
<span> | ||
<img | ||
loading="lazy" | ||
src={hit?.blog?.favicon || undefined} | ||
alt="" | ||
className={styles.favicon} | ||
height={16} | ||
width={16} | ||
/> | ||
{hit?.blog?.name} | ||
</span> | ||
{hit.slug ? ( | ||
<Link href={`/artykuly/${hit.slug}`}> | ||
<a className={styles.articleRef}>Przejdź do artykułu</a> | ||
</Link> | ||
) : ( | ||
<a className={styles.articleRef} href={hit.href} target="_blank" rel="noreferrer noopener"> | ||
Przejdź do artykułu | ||
</a> | ||
)} | ||
</> | ||
); | ||
AlgoliaHit.displayName = 'AlogliaHit'; |
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,19 @@ | ||
.list { | ||
list-style: none; | ||
width: 100%; | ||
padding: 0; | ||
} | ||
|
||
.listItem { | ||
width: 100%; | ||
padding: 1rem; | ||
margin-bottom: 0.35rem; | ||
border: 1px solid var(--brand-color-main); | ||
box-shadow: 0 2px 5px 0 var(--gray-shadow); | ||
background: var(--white); | ||
border-radius: 0.4rem; | ||
} | ||
|
||
.listItem:hover { | ||
background: var(--gray-border); | ||
} |
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,21 @@ | ||
import { connectHits } from 'react-instantsearch-dom'; | ||
|
||
import type { Hit } from './AlgoliaHit'; | ||
import { AlgoliaHit } from './AlgoliaHit'; | ||
import styles from './AlgoliaHits.module.css'; | ||
|
||
type HitsProps = { | ||
readonly hits: readonly Hit[]; | ||
}; | ||
|
||
const Hits = ({ hits }: HitsProps) => ( | ||
<ol className={styles.list}> | ||
{hits.map((hit) => ( | ||
<li className={styles.listItem} key={hit.objectID}> | ||
<AlgoliaHit hit={hit} /> | ||
</li> | ||
))} | ||
</ol> | ||
); | ||
|
||
export const AlgoliaHits = connectHits(Hits); |
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,47 @@ | ||
import algoliasearch from 'algoliasearch/lite'; | ||
import { memo, useEffect } from 'react'; | ||
import { InstantSearch, SearchBox } from 'react-instantsearch-dom'; | ||
|
||
import { AlgoliaHits } from './AlgoliaHits'; | ||
|
||
const searchClient = algoliasearch( | ||
process.env.NEXT_PUBLIC_ALGOLIA_APP_ID as string, | ||
process.env.NEXT_PUBLIC_ALGOLIA_API_KEY as string, | ||
); | ||
|
||
export type SearchState = { readonly query: string }; | ||
|
||
export type AlgoliaSearchProps = { | ||
readonly searchState: SearchState; | ||
readonly setSearchState: (searchState: SearchState) => void; | ||
}; | ||
|
||
export const AlogliaSearch = memo<AlgoliaSearchProps>(({ searchState, setSearchState }) => { | ||
const showHits = searchState.query !== ''; | ||
|
||
useEffect(() => { | ||
const handleKeyDown = (event: KeyboardEvent) => | ||
event.key === 'Escape' && setSearchState({ query: '' }); | ||
window.addEventListener('keydown', handleKeyDown); | ||
return () => window.removeEventListener('keydown', handleKeyDown); | ||
}, [setSearchState]); | ||
|
||
return ( | ||
<InstantSearch | ||
searchState={searchState} | ||
onSearchStateChange={setSearchState} | ||
searchClient={searchClient} | ||
indexName={process.env.NEXT_PUBLIC_ALGOLIA_INDEX_NAME as string} | ||
> | ||
<SearchBox | ||
translations={{ | ||
submitTitle: 'Zatwierdź frazę wyszukiwania.', | ||
resetTitle: 'Wyczyść frazę wyszukiwania.', | ||
placeholder: 'Szukaj...', | ||
}} | ||
/> | ||
{showHits && <AlgoliaHits />} | ||
</InstantSearch> | ||
); | ||
}); | ||
AlogliaSearch.displayName = 'AlogliaSearch'; |
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,15 @@ | ||
.section { | ||
max-width: 73rem; | ||
margin: 0 auto; | ||
} | ||
|
||
.searchWrapper { | ||
min-height: 5rem; | ||
padding: 1rem 1rem; | ||
margin-top: 3rem; | ||
padding: 1rem; | ||
|
||
@media screen and (min-width: 45em) { | ||
padding: 1rem 2rem; | ||
} | ||
} |
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,33 @@ | ||
import dynamic from 'next/dynamic'; | ||
import { memo, useState } from 'react'; | ||
|
||
import type { HomePageProps } from '../../pages/[displayStyle]/[cursor]'; | ||
import type { AlgoliaSearchProps, SearchState } from '../AlgoliaSearch/AlgoliaSearch'; | ||
import { MainTiles } from '../MainTiles/MainTiles'; | ||
|
||
import styles from './Main.module.scss'; | ||
|
||
type MainProps = HomePageProps; | ||
|
||
const AlogliaSearch = dynamic<AlgoliaSearchProps>( | ||
() => | ||
import( | ||
/* webpackChunkName: "AlgoliaSearch" */ | ||
'../AlgoliaSearch/AlgoliaSearch' | ||
).then((mod) => mod.AlogliaSearch), | ||
{ ssr: false }, | ||
); | ||
|
||
export const Main = memo<MainProps>((props) => { | ||
const [searchState, setSearchState] = useState<SearchState>({ query: '' }); | ||
|
||
return ( | ||
<section className={styles.section}> | ||
<div className={styles.searchWrapper}> | ||
<AlogliaSearch searchState={searchState} setSearchState={setSearchState} /> | ||
</div> | ||
{!searchState.query && <MainTiles {...props} />} | ||
</section> | ||
); | ||
}); | ||
Main.displayName = 'Main'; |
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
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
Oops, something went wrong.