From 053f680dc0d88219d2a4ddf5654aa17bb55ff7c3 Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 5 Nov 2025 01:24:34 +0800 Subject: [PATCH] fix(translation): implement user role-based translation prefetching Enhanced translation prefetching logic across multiple components by incorporating user role checks. Users with the Free role will no longer trigger translation prefetching, optimizing performance and resource usage. Updated relevant hooks and components to utilize the new logic for improved user experience. Signed-off-by: Innei --- .../layer/renderer/src/modules/entry-column/item.tsx | 7 +++++-- .../layer/renderer/src/modules/entry-content/hooks.tsx | 6 +++++- .../src/modules/entry-list/EntryListContentArticle.tsx | 6 +++++- .../src/modules/entry-list/EntryListContentPicture.tsx | 6 +++++- .../src/modules/entry-list/EntryListContentSocial.tsx | 6 +++++- .../src/modules/entry-list/EntryListContentVideo.tsx | 6 +++++- .../(stack)/entries/[entryId]/EntryDetailScreen.tsx | 8 +++++--- packages/internal/store/src/modules/translation/store.ts | 6 ++++++ 8 files changed, 41 insertions(+), 10 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 c9640dd0b..41b1005fc 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-column/item.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-column/item.tsx @@ -1,6 +1,7 @@ -import { FeedViewType } from "@follow/constants" +import { FeedViewType, UserRole } 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" import type { FC } from "react" import { memo } from "react" @@ -24,6 +25,8 @@ const EntryItemImpl = memo(function EntryItemImpl({ }: EntryItemProps) { const enableTranslation = useGeneralSettingKey("translation") const actionLanguage = useActionLanguage() + const userRole = useUserRole() + const shouldPrefetchTranslation = enableTranslation && userRole !== UserRole.Free const translation = useEntryTranslation({ entryId, language: actionLanguage, @@ -31,7 +34,7 @@ const EntryItemImpl = memo(function EntryItemImpl({ }) usePrefetchEntryTranslation({ entryIds: [entryId], - enabled: enableTranslation, + enabled: shouldPrefetchTranslation, language: actionLanguage, withContent: view === FeedViewType.SocialMedia, }) 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 6f6164e4a..689d6ef07 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-content/hooks.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-content/hooks.tsx @@ -1,5 +1,7 @@ +import { UserRole } 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" import { tracker } from "@follow/tracker" import { createElement, useCallback, useMemo } from "react" import { useTranslation } from "react-i18next" @@ -47,6 +49,8 @@ export const useEntryContent = (entryId: string) => { const isReadabilitySuccess = useEntryIsInReadabilitySuccess(entryId) const enableTranslation = useShowAITranslation() + const userRole = useUserRole() + const shouldPrefetchTranslation = enableTranslation && userRole !== UserRole.Free const actionLanguage = useActionLanguage() const contentTranslated = useEntryTranslation({ entryId, @@ -55,7 +59,7 @@ export const useEntryContent = (entryId: string) => { }) usePrefetchEntryTranslation({ entryIds: [entryId], - enabled: enableTranslation, + enabled: shouldPrefetchTranslation, language: actionLanguage, withContent: true, target: isReadabilitySuccess ? "readabilityContent" : "content", diff --git a/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx b/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx index 5c15c1e58..99a6844f4 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx @@ -1,5 +1,7 @@ import type { FeedViewType } from "@follow/constants" +import { UserRole } 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" import type { ElementRef } from "react" import { useCallback, useImperativeHandle, useMemo, useRef } from "react" @@ -52,10 +54,12 @@ export const EntryListContentArticle = ({ const translation = useGeneralSettingKey("translation") const actionLanguage = useActionLanguage() + const userRole = useUserRole() + const translationPrefetchEnabled = translation && userRole !== UserRole.Free usePrefetchEntryTranslation({ entryIds: active ? viewableItems.map((item) => item.key) : [], language: actionLanguage, - enabled: translation, + enabled: translationPrefetchEnabled, }) const headerHeight = useHeaderHeight() diff --git a/apps/mobile/src/modules/entry-list/EntryListContentPicture.tsx b/apps/mobile/src/modules/entry-list/EntryListContentPicture.tsx index 7881b1d00..44d203dd4 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentPicture.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentPicture.tsx @@ -1,5 +1,7 @@ +import { UserRole } from "@follow/constants" import { useTypeScriptHappyCallback } from "@follow/hooks" import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks" +import { useUserRole } from "@follow/store/user/hooks" import type { FlashListProps, FlashListRef } from "@shopify/flash-list" import type { ElementRef } from "react" import { useImperativeHandle, useRef } from "react" @@ -35,10 +37,12 @@ export const EntryListContentPicture = ({ }) const translation = useGeneralSettingKey("translation") const actionLanguage = useActionLanguage() + const userRole = useUserRole() + const translationPrefetchEnabled = translation && userRole !== UserRole.Free usePrefetchEntryTranslation({ entryIds: active ? viewableItems.map((item) => item.key) : [], language: actionLanguage, - enabled: translation, + enabled: translationPrefetchEnabled, }) const renderItem = useTypeScriptHappyCallback(({ item }: { item: string }) => { diff --git a/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx b/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx index 339307886..4892f0efc 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx @@ -1,4 +1,6 @@ +import { UserRole } 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" import type { ElementRef } from "react" import { useCallback, useImperativeHandle, useMemo, useRef } from "react" @@ -45,10 +47,12 @@ export const EntryListContentSocial = ({ const translation = useGeneralSettingKey("translation") const actionLanguage = useActionLanguage() + const userRole = useUserRole() + const translationPrefetchEnabled = translation && userRole !== UserRole.Free usePrefetchEntryTranslation({ entryIds: active ? viewableItems.map((item) => item.key) : [], language: actionLanguage, - enabled: translation, + enabled: translationPrefetchEnabled, }) // Show loading skeleton when entries are not ready and no data yet diff --git a/apps/mobile/src/modules/entry-list/EntryListContentVideo.tsx b/apps/mobile/src/modules/entry-list/EntryListContentVideo.tsx index 4fb543a12..b38f027d4 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentVideo.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentVideo.tsx @@ -1,5 +1,7 @@ +import { UserRole } from "@follow/constants" import { useTypeScriptHappyCallback } from "@follow/hooks" import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks" +import { useUserRole } from "@follow/store/user/hooks" import type { FlashListProps, FlashListRef } from "@shopify/flash-list" import type { ElementRef } from "react" import { useImperativeHandle, useMemo, useRef } from "react" @@ -33,10 +35,12 @@ export const EntryListContentVideo = ({ const translation = useGeneralSettingKey("translation") const actionLanguage = useActionLanguage() + const userRole = useUserRole() + const translationPrefetchEnabled = translation && userRole !== UserRole.Free usePrefetchEntryTranslation({ entryIds: active ? viewableItems.map((item) => item.key) : [], language: actionLanguage, - enabled: translation, + enabled: translationPrefetchEnabled, }) const ListFooterComponent = useMemo( diff --git a/apps/mobile/src/screens/(stack)/entries/[entryId]/EntryDetailScreen.tsx b/apps/mobile/src/screens/(stack)/entries/[entryId]/EntryDetailScreen.tsx index 49c826a04..73f1fcab8 100644 --- a/apps/mobile/src/screens/(stack)/entries/[entryId]/EntryDetailScreen.tsx +++ b/apps/mobile/src/screens/(stack)/entries/[entryId]/EntryDetailScreen.tsx @@ -1,9 +1,10 @@ -import { FeedViewType } from "@follow/constants" +import { FeedViewType, UserRole } 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" import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks" import { useAutoMarkAsRead } from "@follow/store/unread/hooks" +import { useUserRole } from "@follow/store/user/hooks" import { PortalProvider } from "@gorhom/portal" import * as WebBrowser from "expo-web-browser" import { atom, useAtomValue, useSetAtom } from "jotai" @@ -143,7 +144,8 @@ const EntryContentWebViewWithContext = ({ entryId }: { entryId: string }) => { const translationSetting = useGeneralSettingKey("translation") const showTranslationOnce = useAtomValue(showAITranslationAtom) const actionLanguage = useActionLanguage() - const translation = useGeneralSettingKey("translation") + const userRole = useUserRole() + const translationPrefetchEnabled = translationSetting && userRole !== UserRole.Free const entry = useEntry(entryId, (state) => ({ content: state.content, readabilityContent: state.readabilityContent, @@ -154,7 +156,7 @@ const EntryContentWebViewWithContext = ({ entryId }: { entryId: string }) => { withContent: true, target: showReadabilityOnce && entry?.readabilityContent ? "readabilityContent" : "content", language: actionLanguage, - enabled: translation, + enabled: translationPrefetchEnabled, }) // Auto toggle readability when content is empty diff --git a/packages/internal/store/src/modules/translation/store.ts b/packages/internal/store/src/modules/translation/store.ts index 8ee635c83..e73afce40 100644 --- a/packages/internal/store/src/modules/translation/store.ts +++ b/packages/internal/store/src/modules/translation/store.ts @@ -1,3 +1,4 @@ +import { UserRole } from "@follow/constants" import type { TranslationSchema } from "@follow/database/schemas/types" import { TranslationService } from "@follow/database/services/translation" import type { SupportedActionLanguage } from "@follow/shared" @@ -9,6 +10,7 @@ import type { Hydratable, Resetable } from "../../lib/base" import { createImmerSetter, createTransaction, createZustandStore } from "../../lib/helper" import { readNdjsonStream } from "../../lib/stream" import { getEntry } from "../entry/getter" +import { useUserStore } from "../user/store" import type { EntryTranslation, TranslationFieldArray } from "./types" import { translationFields } from "./types" @@ -189,6 +191,10 @@ class TranslationSyncService { withContent?: boolean target: "content" | "readabilityContent" }) { + const userRole = useUserStore.getState().role + + if (userRole === UserRole.Free) return null + const entry = getEntry(entryId) if (!entry) return