From ccf4bdcde017a73c2386e01e406198f0f93181bc Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 11 Apr 2023 23:49:26 +0100 Subject: [PATCH] feat: add global search input and search page --- src/components/common/SearchInput.tsx | 62 +++++++++++++++++++++++++++ src/components/main/MainFeed.tsx | 4 +- src/components/main/MainSidebar.tsx | 10 +++-- src/models/home.model.ts | 27 +++++++++++- src/pages/search.tsx | 54 +++++++++++++++++++++++ src/queries/home.ts | 1 + 6 files changed, 152 insertions(+), 6 deletions(-) create mode 100644 src/components/common/SearchInput.tsx create mode 100644 src/pages/search.tsx diff --git a/src/components/common/SearchInput.tsx b/src/components/common/SearchInput.tsx new file mode 100644 index 00000000..8995dcab --- /dev/null +++ b/src/components/common/SearchInput.tsx @@ -0,0 +1,62 @@ +import { Profile } from "~/lib/types" +import { useAccountSites } from "~/queries/site" +import { useForm } from "react-hook-form" +import { useAccountState } from "@crossbell/connect-kit" +import { useState, useEffect } from "react" +import { useCommentPage, useUpdateComment } from "~/queries/page" +import { useRouter } from "next/router" +import { useTranslation } from "next-i18next" + +export const SearchInput: React.FC<{ + characterId?: string + value?: string +}> = ({ characterId, value }) => { + const account = useAccountState((s) => s.computed.account) + const userSites = useAccountSites() + const commentPage = useCommentPage() + const updateComment = useUpdateComment() + const router = useRouter() + const [viewer, setViewer] = useState(null) + const { t } = useTranslation(["common", "site"]) + + useEffect(() => { + if (userSites.isSuccess && userSites.data?.length) { + setViewer(userSites.data[0]) + } + }, [userSites, router]) + + const form = useForm({ + defaultValues: { + content: value || "", + }, + }) + + const handleSubmit = form.handleSubmit(async (values) => { + router.push(`/search?q=${values.content}`) + }) + + useEffect(() => { + if (commentPage.isSuccess || updateComment.isSuccess) { + form.reset() + } + }, [commentPage.isSuccess, updateComment.isSuccess, form]) + + return ( +
+
+
+ +
+ +
+
+ ) +} diff --git a/src/components/main/MainFeed.tsx b/src/components/main/MainFeed.tsx index 6abe11b3..0b7efef0 100644 --- a/src/components/main/MainFeed.tsx +++ b/src/components/main/MainFeed.tsx @@ -140,7 +140,8 @@ const Post = ({ export const MainFeed: React.FC<{ type?: FeedType noteIds?: string[] -}> = ({ type, noteIds }) => { + keyword?: string +}> = ({ type, noteIds, keyword }) => { const { t } = useTranslation(["common", "site"]) const currentCharacterId = useAccountState( @@ -154,6 +155,7 @@ export const MainFeed: React.FC<{ characterId: currentCharacterId, noteIds: noteIds, daysInterval: hotInterval, + keyword: keyword, }) const hasFiltering = type === "latest" diff --git a/src/components/main/MainSidebar.tsx b/src/components/main/MainSidebar.tsx index 071a1745..0ab9b026 100644 --- a/src/components/main/MainSidebar.tsx +++ b/src/components/main/MainSidebar.tsx @@ -11,8 +11,9 @@ import { CharacterFloatCard } from "~/components/common/CharacterFloatCard" import { useAccountSites, useSubscribeToSites } from "~/queries/site" import { useTranslation } from "next-i18next" import topics from "../../../data/topics.json" +import { SearchInput } from "~/components/common/SearchInput" -export function MainSidebar() { +export function MainSidebar({ hideSearch }: { hideSearch?: boolean }) { const showcaseSites = useGetSites(showcase) const { t } = useTranslation("index") @@ -36,9 +37,10 @@ export function MainSidebar() { const [showcaseMore, setShowcaseMore] = useState(false) return ( -
+
+ {!hideSearch && }
-
+

{t("Hot Topics")}

    {topics.map((topic: any) => ( @@ -58,7 +60,7 @@ export function MainSidebar() {
-
+

{t("Suggested creators for you")}