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 <tukon479@gmail.com>
This commit is contained in:
Innei
2025-11-05 01:24:34 +08:00
parent e9553e40d0
commit 053f680dc0
8 changed files with 41 additions and 10 deletions
@@ -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,
})
@@ -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",
@@ -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()
@@ -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 }) => {
@@ -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
@@ -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(
@@ -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
@@ -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