-
Notifications
You must be signed in to change notification settings - Fork 10
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
24189da
commit 75740b2
Showing
1 changed file
with
85 additions
and
0 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,85 @@ | ||
import Layout from "@/components/layout"; | ||
import PostCard from "@/components/postCard"; | ||
import { useMeiliSearch } from "@/meilisearch/context"; | ||
import { usePocketBase } from "@/pocketbase"; | ||
import { Post } from "@/pocketbase/models"; | ||
import { ShareMeEnv, withEnv } from "@/utils/env"; | ||
import { Skeleton, Stack, Text, TextInput, Title } from "@mantine/core"; | ||
import { MeiliSearch } from "meilisearch"; | ||
import { GetServerSideProps } from "next"; | ||
import { useRouter } from "next/router"; | ||
import { useQuery } from "react-query"; | ||
|
||
interface SearchProps extends ShareMeEnv {} | ||
|
||
function Search({ signUpEnabled }: SearchProps) { | ||
const router = useRouter(); | ||
const pb = usePocketBase(); | ||
const meiliSearch = useMeiliSearch(); | ||
|
||
const { data: hits } = useQuery( | ||
["search", router.query.query, meiliSearch] as [ | ||
string, | ||
string[] | string | undefined, | ||
MeiliSearch | ||
], | ||
({ queryKey }) => { | ||
const [_, search, client] = queryKey; | ||
|
||
return client | ||
?.index("posts") | ||
.search(Array.isArray(search) ? search[0] : search!); | ||
} | ||
); | ||
|
||
const { data, isLoading } = useQuery([hits], ({ queryKey }) => { | ||
const [hits] = queryKey; | ||
|
||
return hits?.hits.length | ||
? pb.collection("posts").getList<Post>(1, 20, { | ||
expand: "files,author", | ||
filter: hits.hits.map((hit) => `id = '${hit.id}'`).join(" || "), | ||
$autoCancel: false, | ||
}) | ||
: null; | ||
}); | ||
|
||
return ( | ||
<Layout signUpEnabled={signUpEnabled}> | ||
<Stack align="stretch" maw={450} m="0 auto"> | ||
<Title order={2}> | ||
Search results for '{router.query.query}' | ||
</Title> | ||
<TextInput | ||
placeholder="Search" | ||
size="lg" | ||
radius="xl" | ||
variant="filled" | ||
value={router.query.query} | ||
onChange={(e) => router.push("/search?query=" + e.target.value)} | ||
/> | ||
<Text>{hits?.hits.length} Results</Text> | ||
{data?.items.map((p) => ( | ||
<PostCard key={p.id} post={p} maw="100%" /> | ||
))} | ||
{isLoading && | ||
new Array(10) | ||
.fill(null) | ||
.map((_, i) => ( | ||
<Skeleton key={i} height={200} width="100%" maw={400} /> | ||
))} | ||
<Text align="center"> | ||
Showing {data?.items.length} / {data?.totalItems} Posts | ||
</Text> | ||
</Stack> | ||
</Layout> | ||
); | ||
} | ||
|
||
export default Search; | ||
|
||
export const getServerSideProps: GetServerSideProps< | ||
SearchProps | ||
> = async () => ({ | ||
props: withEnv({}), | ||
}); |