fix(desktop): check feed follow state correctly for slash redirect

This commit is contained in:
Stephen Zhou 2025-05-14 20:16:54 +08:00
parent bf26a3eabc
commit 701ee30cf0
No known key found for this signature in database
4 changed files with 20 additions and 10 deletions

View File

@ -90,6 +90,7 @@
"sonner": "2.0.3",
"tinykeys": "3.0.0",
"tldts": "7.0.4",
"ufo": "1.6.1",
"use-context-selector": "2.0.0",
"use-sync-external-store": "1.5.0",
"usehooks-ts": "3.1.1",

View File

@ -1,6 +1,7 @@
import { UserRole } from "@follow/constants"
import { t } from "i18next"
import { useCallback } from "react"
import { withoutTrailingSlash, withTrailingSlash } from "ufo"
import { useEventCallback } from "usehooks-ts"
import { useServerConfigs } from "~/atoms/server-configs"
@ -12,8 +13,9 @@ import type { FeedFormDataValuesType } from "~/modules/discover/FeedForm"
import { FeedForm } from "~/modules/discover/FeedForm"
import type { ListFormDataValuesType } from "~/modules/discover/ListForm"
import { ListForm } from "~/modules/discover/ListForm"
import { getFeedByIdOrUrl } from "~/store/feed"
import {
getSubscriptionByFeedIdOrUrl,
getSubscriptionByFeedId,
useFeedSubscriptionCount,
useListSubscriptionCount,
} from "~/store/subscription"
@ -74,10 +76,14 @@ export const useFollow = () => {
canFollowMoreInboxAndNotify("feed")
}
const subscription = getSubscriptionByFeedIdOrUrl({
id: options?.id,
url: options?.url,
})
// Some feeds redirect xxx.com/feed to xxx.com/feed/
// Try to get a valid feed, then we can check isFollowed correctly
const feed =
getFeedByIdOrUrl({ id: options?.id, url: withTrailingSlash(options?.url) }) ??
getFeedByIdOrUrl({ id: options?.id, url: withoutTrailingSlash(options?.url) })
const id = options?.id || feed?.id
const url = feed?.type === "feed" ? feed.url : options?.url
const subscription = getSubscriptionByFeedId(id)
const isFollowed = !!subscription
present({
@ -96,8 +102,8 @@ export const useFollow = () => {
/>
) : (
<FeedForm
id={options?.id}
url={options?.url}
id={id}
url={url}
defaultValues={options?.defaultValues as FeedFormDataValuesType}
onSuccess={onSuccess}
/>

View File

@ -10,9 +10,9 @@ import {
import { useSubscriptionStore } from "./store"
const get = useSubscriptionStore.getState
export const getSubscriptionByFeedId = (feedId: FeedId) => {
const state = get()
return state.data[feedId]
export const getSubscriptionByFeedId = (feedId?: FeedId) => {
if (!feedId) return
return get().data[feedId]
}
export const getSubscriptionByFeedIdOrUrl = (params: FeedQueryParams) => {
const feed = getFeedByIdOrUrl(params)

View File

@ -683,6 +683,9 @@ importers:
tldts:
specifier: 7.0.4
version: 7.0.4
ufo:
specifier: 1.6.1
version: 1.6.1
use-context-selector:
specifier: 2.0.0
version: 2.0.0(react@19.0.0)(scheduler@0.25.0)