generated from acdh-oeaw/template-app-next
-
Notifications
You must be signed in to change notification settings - Fork 0
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
9e9d968
commit 011ee7c
Showing
7 changed files
with
120 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useTranslations } from "next-intl"; | ||
import { type ReactNode, useEffect, useRef } from "react"; | ||
import { useInfiniteHits } from "react-instantsearch"; | ||
|
||
import type { Publication } from "@/lib/model"; | ||
|
||
import { PublicationGrid } from "./publication-grid"; | ||
import { Button } from "./ui/button"; | ||
|
||
export function InfiniteScroll(): ReactNode { | ||
const t = useTranslations("SearchPage"); | ||
const { items, isLastPage, showMore } = useInfiniteHits<Publication>(); | ||
const sentinelRef = useRef(null); | ||
useEffect(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (sentinelRef.current !== null) { | ||
const observer = new IntersectionObserver((entries) => { | ||
entries.forEach((entry) => { | ||
if (entry.isIntersecting && !isLastPage) { | ||
showMore(); | ||
// showMore && showMore(); | ||
} | ||
}); | ||
}); | ||
|
||
observer.observe(sentinelRef.current); | ||
|
||
return () => { | ||
observer.disconnect(); | ||
}; | ||
} | ||
return () => { | ||
return null; | ||
}; | ||
}, [isLastPage, showMore]); | ||
|
||
return ( | ||
<> | ||
<PublicationGrid publications={items} /> | ||
{isLastPage ? ( | ||
<hr className="m-auto mt-8 w-1/3" /> | ||
) : ( | ||
<div ref={sentinelRef} className="text-center"> | ||
<Button | ||
onPress={() => { | ||
showMore(); | ||
}} | ||
> | ||
{t("show_more")} | ||
</Button> | ||
</div> | ||
)} | ||
</> | ||
); | ||
} |
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 @@ | ||
import type { Hit } from "instantsearch.js"; | ||
import type { ReactNode } from "react"; | ||
import { Hits } from "react-instantsearch"; | ||
|
||
import type { Publication } from "@/lib/model"; | ||
|
||
import { PublicationLink } from "./publication-link"; | ||
|
||
function TableRow({ hit }: { hit: Hit<Publication> }) { | ||
return ( | ||
<> | ||
<PublicationLink publication={hit} /> ({hit.year_display}) | ||
</> | ||
); | ||
} | ||
|
||
export function PaginatedTable(): ReactNode { | ||
return <Hits hitComponent={TableRow} />; | ||
} |
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 |
---|---|---|
@@ -1,8 +1,14 @@ | ||
import { useTranslations } from "next-intl"; | ||
import type { ReactNode } from "react"; | ||
import { useStats } from "react-instantsearch"; | ||
|
||
export function InstantSearchStats(): ReactNode { | ||
const t = useTranslations("SearchPage"); | ||
const stats = useStats(); | ||
// https://www.algolia.com/doc/api-reference/widgets/stats/react/#hook | ||
return <>{stats.nbHits} results</>; | ||
return ( | ||
<> | ||
{stats.nbHits} {stats.nbHits === 1 ? t("result") : t("results")} | ||
</> | ||
); | ||
} |
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,13 @@ | ||
import type { Publication } from "@/lib/model"; | ||
|
||
import { AppLink } from "./app-link"; | ||
|
||
interface PublicationLinkProps { | ||
publication: Publication; | ||
} | ||
|
||
export function PublicationLink(props: PublicationLinkProps) { | ||
return ( | ||
<AppLink href={`/publications/${props.publication.id}`}>{props.publication.title}</AppLink> | ||
); | ||
} |
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