From f6a8691e3acb4bc427c19e85c9bfc568d3dc8a23 Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 5 Nov 2025 21:41:56 +0800 Subject: [PATCH] fix(translation): update user role checks for translation prefetching Refactored translation prefetching logic across multiple components to utilize the new isFreeRole function. This change ensures that users with Free roles do not trigger translation prefetching, optimizing performance and enhancing user experience. Updated relevant imports and conditions in EntryItemImpl, useEntryContent, and various EntryList components. Signed-off-by: Innei --- .../layer/renderer/src/modules/entry-column/item.tsx | 4 ++-- .../layer/renderer/src/modules/entry-content/hooks.tsx | 4 ++-- .../src/modules/entry-list/EntryListContentArticle.tsx | 4 ++-- .../src/modules/entry-list/EntryListContentPicture.tsx | 4 ++-- .../src/modules/entry-list/EntryListContentSocial.tsx | 4 ++-- .../mobile/src/modules/entry-list/EntryListContentVideo.tsx | 4 ++-- .../screens/(stack)/entries/[entryId]/EntryDetailScreen.tsx | 4 ++-- packages/internal/constants/src/enums.ts | 6 ++++++ 8 files changed, 20 insertions(+), 14 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/entry-column/item.tsx b/apps/desktop/layer/renderer/src/modules/entry-column/item.tsx index 41b1005fc..1b19738eb 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-column/item.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-column/item.tsx @@ -1,4 +1,4 @@ -import { FeedViewType, UserRole } from "@follow/constants" +import { FeedViewType, isFreeRole } from "@follow/constants" import { useHasEntry } from "@follow/store/entry/hooks" import { useEntryTranslation, usePrefetchEntryTranslation } from "@follow/store/translation/hooks" import { useUserRole } from "@follow/store/user/hooks" @@ -26,7 +26,7 @@ const EntryItemImpl = memo(function EntryItemImpl({ const enableTranslation = useGeneralSettingKey("translation") const actionLanguage = useActionLanguage() const userRole = useUserRole() - const shouldPrefetchTranslation = enableTranslation && userRole !== UserRole.Free + const shouldPrefetchTranslation = enableTranslation && !isFreeRole(userRole) const translation = useEntryTranslation({ entryId, language: actionLanguage, diff --git a/apps/desktop/layer/renderer/src/modules/entry-content/hooks.tsx b/apps/desktop/layer/renderer/src/modules/entry-content/hooks.tsx index 689d6ef07..862e21696 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-content/hooks.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-content/hooks.tsx @@ -1,4 +1,4 @@ -import { UserRole } from "@follow/constants" +import { isFreeRole } from "@follow/constants" import { useEntry, usePrefetchEntryDetail } from "@follow/store/entry/hooks" import { useEntryTranslation, usePrefetchEntryTranslation } from "@follow/store/translation/hooks" import { useUserRole } from "@follow/store/user/hooks" @@ -50,7 +50,7 @@ export const useEntryContent = (entryId: string) => { const enableTranslation = useShowAITranslation() const userRole = useUserRole() - const shouldPrefetchTranslation = enableTranslation && userRole !== UserRole.Free + const shouldPrefetchTranslation = enableTranslation && !isFreeRole(userRole) const actionLanguage = useActionLanguage() const contentTranslated = useEntryTranslation({ entryId, diff --git a/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx b/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx index 99a6844f4..96a97d6ed 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx @@ -1,5 +1,5 @@ import type { FeedViewType } from "@follow/constants" -import { UserRole } from "@follow/constants" +import { isFreeRole } from "@follow/constants" import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks" import { useUserRole } from "@follow/store/user/hooks" import type { FlashListRef, ListRenderItemInfo } from "@shopify/flash-list" @@ -55,7 +55,7 @@ export const EntryListContentArticle = ({ const translation = useGeneralSettingKey("translation") const actionLanguage = useActionLanguage() const userRole = useUserRole() - const translationPrefetchEnabled = translation && userRole !== UserRole.Free + const translationPrefetchEnabled = translation && !isFreeRole(userRole) usePrefetchEntryTranslation({ entryIds: active ? viewableItems.map((item) => item.key) : [], language: actionLanguage, diff --git a/apps/mobile/src/modules/entry-list/EntryListContentPicture.tsx b/apps/mobile/src/modules/entry-list/EntryListContentPicture.tsx index 44d203dd4..9bc44bd06 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentPicture.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentPicture.tsx @@ -1,4 +1,4 @@ -import { UserRole } from "@follow/constants" +import { isFreeRole } from "@follow/constants" import { useTypeScriptHappyCallback } from "@follow/hooks" import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks" import { useUserRole } from "@follow/store/user/hooks" @@ -38,7 +38,7 @@ export const EntryListContentPicture = ({ const translation = useGeneralSettingKey("translation") const actionLanguage = useActionLanguage() const userRole = useUserRole() - const translationPrefetchEnabled = translation && userRole !== UserRole.Free + const translationPrefetchEnabled = translation && !isFreeRole(userRole) usePrefetchEntryTranslation({ entryIds: active ? viewableItems.map((item) => item.key) : [], language: actionLanguage, diff --git a/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx b/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx index 4892f0efc..c6d0893c8 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx @@ -1,4 +1,4 @@ -import { UserRole } from "@follow/constants" +import { isFreeRole } from "@follow/constants" import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks" import { useUserRole } from "@follow/store/user/hooks" import type { FlashListRef, ListRenderItemInfo } from "@shopify/flash-list" @@ -48,7 +48,7 @@ export const EntryListContentSocial = ({ const translation = useGeneralSettingKey("translation") const actionLanguage = useActionLanguage() const userRole = useUserRole() - const translationPrefetchEnabled = translation && userRole !== UserRole.Free + const translationPrefetchEnabled = translation && !isFreeRole(userRole) usePrefetchEntryTranslation({ entryIds: active ? viewableItems.map((item) => item.key) : [], language: actionLanguage, diff --git a/apps/mobile/src/modules/entry-list/EntryListContentVideo.tsx b/apps/mobile/src/modules/entry-list/EntryListContentVideo.tsx index b38f027d4..9d6608661 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentVideo.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentVideo.tsx @@ -1,4 +1,4 @@ -import { UserRole } from "@follow/constants" +import { isFreeRole } from "@follow/constants" import { useTypeScriptHappyCallback } from "@follow/hooks" import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks" import { useUserRole } from "@follow/store/user/hooks" @@ -36,7 +36,7 @@ export const EntryListContentVideo = ({ const translation = useGeneralSettingKey("translation") const actionLanguage = useActionLanguage() const userRole = useUserRole() - const translationPrefetchEnabled = translation && userRole !== UserRole.Free + const translationPrefetchEnabled = translation && !isFreeRole(userRole) usePrefetchEntryTranslation({ entryIds: active ? viewableItems.map((item) => item.key) : [], language: actionLanguage, diff --git a/apps/mobile/src/screens/(stack)/entries/[entryId]/EntryDetailScreen.tsx b/apps/mobile/src/screens/(stack)/entries/[entryId]/EntryDetailScreen.tsx index 73f1fcab8..7ed1b0740 100644 --- a/apps/mobile/src/screens/(stack)/entries/[entryId]/EntryDetailScreen.tsx +++ b/apps/mobile/src/screens/(stack)/entries/[entryId]/EntryDetailScreen.tsx @@ -1,4 +1,4 @@ -import { FeedViewType, UserRole } from "@follow/constants" +import { FeedViewType, isFreeRole } from "@follow/constants" import { useEntry, useEntryReadHistory, usePrefetchEntryDetail } from "@follow/store/entry/hooks" import { entrySyncServices } from "@follow/store/entry/store" import { useFeedById } from "@follow/store/feed/hooks" @@ -145,7 +145,7 @@ const EntryContentWebViewWithContext = ({ entryId }: { entryId: string }) => { const showTranslationOnce = useAtomValue(showAITranslationAtom) const actionLanguage = useActionLanguage() const userRole = useUserRole() - const translationPrefetchEnabled = translationSetting && userRole !== UserRole.Free + const translationPrefetchEnabled = translationSetting && !isFreeRole(userRole) const entry = useEntry(entryId, (state) => ({ content: state.content, readabilityContent: state.readabilityContent, diff --git a/packages/internal/constants/src/enums.ts b/packages/internal/constants/src/enums.ts index e1b51392e..ee9602609 100644 --- a/packages/internal/constants/src/enums.ts +++ b/packages/internal/constants/src/enums.ts @@ -30,3 +30,9 @@ export const UserRoleName: Record = { [UserRole.Pro]: "Pro", [UserRole.Plus]: "Plus", } as const + +export const isFreeRole = (role?: UserRole | null) => { + return role + ? role === UserRole.Free || role === UserRole.Trial || role === UserRole.PreProTrial + : true +}