feat: refactor discover module state management
- Introduced new atom hooks for managing discover search data, improving state handling in UnifiedDiscoverForm. - Replaced direct atom usage with custom hooks for better abstraction and maintainability. - Updated related components to utilize the new state management approach, enhancing overall code clarity. Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -17,7 +17,6 @@ import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { repository } from "@pkg"
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
import { produce } from "immer"
|
||||
import { atom, useAtomValue, useStore } from "jotai"
|
||||
import type { ChangeEvent, CompositionEvent } from "react"
|
||||
import { startTransition, useCallback, useEffect, useMemo } from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
@@ -30,6 +29,11 @@ import { useModalStack } from "~/components/ui/modal/stacked/hooks"
|
||||
import { useRequireLogin } from "~/hooks/common/useRequireLogin"
|
||||
import { followClient } from "~/lib/api-client"
|
||||
|
||||
import {
|
||||
getDiscoverSearchData,
|
||||
setDiscoverSearchData,
|
||||
useDiscoverSearchData,
|
||||
} from "./atoms/discover"
|
||||
import { DiscoverFeedCard } from "./DiscoverFeedCard"
|
||||
import { DiscoverImport } from "./DiscoverImport"
|
||||
import { DiscoverInboxList } from "./DiscoverInboxList"
|
||||
@@ -37,8 +41,6 @@ import { DiscoverTransform } from "./DiscoverTransform"
|
||||
import { DiscoverUser } from "./DiscoverUser"
|
||||
import { FeedForm } from "./FeedForm"
|
||||
|
||||
const discoverSearchDataAtom = atom<Record<string, DiscoveryItem[]>>()
|
||||
|
||||
// Auto-detect input type
|
||||
function detectInputType(value: string): "rss" | "rsshub" | "search" {
|
||||
const trimmed = value.trim()
|
||||
@@ -98,7 +100,6 @@ export function UnifiedDiscoverForm() {
|
||||
const { ensureLogin } = useRequireLogin()
|
||||
const { present, dismissAll } = useModalStack()
|
||||
const isMobile = useMobile()
|
||||
const jotaiStore = useStore()
|
||||
|
||||
// Auto-detect input type based on current value
|
||||
const detectedType = useMemo(() => {
|
||||
@@ -130,7 +131,7 @@ export function UnifiedDiscoverForm() {
|
||||
trigger("keyword")
|
||||
}, [trigger, keywordFromSearch])
|
||||
|
||||
const discoverSearchData = useAtomValue(discoverSearchDataAtom)?.[atomKey] || []
|
||||
const discoverSearchData = useDiscoverSearchData()?.[atomKey] || []
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: async ({ keyword, target }: { keyword: string; target: "feeds" | "lists" }) => {
|
||||
@@ -170,7 +171,7 @@ export function UnifiedDiscoverForm() {
|
||||
data = data.filter((item) => !item.list?.fee)
|
||||
}
|
||||
|
||||
jotaiStore.set(discoverSearchDataAtom, (prev) => ({
|
||||
setDiscoverSearchData((prev) => ({
|
||||
...prev,
|
||||
[atomKey]: data,
|
||||
}))
|
||||
@@ -233,10 +234,9 @@ export function UnifiedDiscoverForm() {
|
||||
|
||||
const handleSuccess = useCallback(
|
||||
(item: DiscoveryItem) => {
|
||||
const currentData = jotaiStore.get(discoverSearchDataAtom)
|
||||
const currentData = getDiscoverSearchData()
|
||||
if (!currentData) return
|
||||
jotaiStore.set(
|
||||
discoverSearchDataAtom,
|
||||
setDiscoverSearchData(
|
||||
produce(currentData, (draft) => {
|
||||
const sub = (draft[atomKey] || []).find((i) => {
|
||||
if (item.feed) {
|
||||
@@ -252,15 +252,14 @@ export function UnifiedDiscoverForm() {
|
||||
}),
|
||||
)
|
||||
},
|
||||
[atomKey, jotaiStore],
|
||||
[atomKey],
|
||||
)
|
||||
|
||||
const handleUnSubscribed = useCallback(
|
||||
(item: DiscoveryItem) => {
|
||||
const currentData = jotaiStore.get(discoverSearchDataAtom)
|
||||
const currentData = getDiscoverSearchData()
|
||||
if (!currentData) return
|
||||
jotaiStore.set(
|
||||
discoverSearchDataAtom,
|
||||
setDiscoverSearchData(
|
||||
produce(currentData, (draft) => {
|
||||
const sub = (draft[atomKey] || []).find(
|
||||
(i) => i.feed?.id === item.feed?.id || i.list?.id === item.list?.id,
|
||||
@@ -272,7 +271,7 @@ export function UnifiedDiscoverForm() {
|
||||
}),
|
||||
)
|
||||
},
|
||||
[atomKey, jotaiStore],
|
||||
[atomKey],
|
||||
)
|
||||
|
||||
const handleTargetChange = useCallback(
|
||||
@@ -471,10 +470,7 @@ export function UnifiedDiscoverForm() {
|
||||
className="flex cursor-button items-center justify-between gap-2 hover:text-accent"
|
||||
type="button"
|
||||
onClick={() => {
|
||||
jotaiStore.set(discoverSearchDataAtom, {
|
||||
...jotaiStore.get(discoverSearchDataAtom),
|
||||
[atomKey]: [],
|
||||
})
|
||||
setDiscoverSearchData({})
|
||||
mutation.reset()
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { createAtomHooks } from "@follow/utils"
|
||||
import type { DiscoveryItem } from "@follow-app/client-sdk"
|
||||
import { atom, useAtomValue } from "jotai"
|
||||
import { selectAtom } from "jotai/utils"
|
||||
import { useMemo } from "react"
|
||||
|
||||
const internalAtom = atom<Record<string, DiscoveryItem[]>>({})
|
||||
export const [, , useDiscoverSearchData, , getDiscoverSearchData, setDiscoverSearchData] =
|
||||
createAtomHooks(internalAtom)
|
||||
|
||||
export const useHasDiscoverSearchData = () => {
|
||||
return useAtomValue(
|
||||
useMemo(() => selectAtom(internalAtom, (data) => Object.keys(data).length > 0), []),
|
||||
)
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
import { cn } from "@follow/utils/utils"
|
||||
import type { ReactNode } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { useSearchParams } from "react-router"
|
||||
|
||||
import { AppErrorBoundary } from "~/components/common/AppErrorBoundary"
|
||||
import { ErrorComponentType } from "~/components/errors/enum"
|
||||
import { useSubViewTitle } from "~/modules/app-layout/subview/hooks"
|
||||
import { useHasDiscoverSearchData } from "~/modules/discover/atoms/discover"
|
||||
import { DiscoveryContent } from "~/modules/discover/DiscoveryContent"
|
||||
import { UnifiedDiscoverForm } from "~/modules/discover/UnifiedDiscoverForm"
|
||||
|
||||
@@ -28,10 +28,9 @@ function Section({ children, className }: SectionProps) {
|
||||
|
||||
export function Component() {
|
||||
const { t } = useTranslation()
|
||||
const [searchParams] = useSearchParams()
|
||||
const keyword = searchParams.get("keyword")
|
||||
const isSearching = !!keyword?.trim()
|
||||
|
||||
useSubViewTitle("words.discover")
|
||||
const hasSearchData = useHasDiscoverSearchData()
|
||||
|
||||
return (
|
||||
<div className="flex size-full flex-col px-6 py-8">
|
||||
@@ -51,7 +50,7 @@ export function Component() {
|
||||
</Section>
|
||||
|
||||
{/* Discovery Section - Hide when searching */}
|
||||
{!isSearching && (
|
||||
{!hasSearchData && (
|
||||
<Section>
|
||||
<AppErrorBoundary errorType={ErrorComponentType.RSSHubDiscoverError}>
|
||||
<DiscoveryContent />
|
||||
|
||||
Reference in New Issue
Block a user