From 5cde69212c7d1f9a17c8fc086fa53e1565e12850 Mon Sep 17 00:00:00 2001 From: Konv Suu <2583695112@qq.com> Date: Mon, 14 Apr 2025 16:16:40 +0800 Subject: [PATCH] refactor(desktop): move electron actions to entry actions (#3483) Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> --- .../src/hooks/biz/useEntryActions.tsx | 95 ++++++++----- .../src/modules/command/commands/entry.tsx | 43 ++++++ .../src/modules/command/commands/id.ts | 2 + .../src/modules/command/commands/types.ts | 12 ++ .../actions/electron-actions.tsx | 125 ------------------ .../entry-content/actions/header-actions.tsx | 12 +- .../modules/entry-content/header.electron.tsx | 5 +- .../modules/entry-content/index.shared.tsx | 10 +- eslint.config.mjs | 1 + 9 files changed, 135 insertions(+), 170 deletions(-) delete mode 100644 apps/desktop/src/renderer/src/modules/entry-content/actions/electron-actions.tsx diff --git a/apps/desktop/src/renderer/src/hooks/biz/useEntryActions.tsx b/apps/desktop/src/renderer/src/hooks/biz/useEntryActions.tsx index eeb3b06bd..518a8d17f 100644 --- a/apps/desktop/src/renderer/src/hooks/biz/useEntryActions.tsx +++ b/apps/desktop/src/renderer/src/hooks/biz/useEntryActions.tsx @@ -1,15 +1,17 @@ import { isMobile } from "@follow/components/hooks/useMobile.js" -import { FeedViewType, UserRole } from "@follow/constants" +import { FeedViewType, UserRole, views } from "@follow/constants" import { IN_ELECTRON } from "@follow/shared/constants" -import { useCallback, useMemo } from "react" +import { useMemo } from "react" import { useShowAISummaryAuto, useShowAISummaryOnce } from "~/atoms/ai-summary" import { useShowAITranslationAuto, useShowAITranslationOnce } from "~/atoms/ai-translation" import { getReadabilityStatus, + isInReadability, ReadabilityStatus, setReadabilityContent, setReadabilityStatus, + useEntryInReadabilityStatus, } from "~/atoms/readability" import { useShowSourceContent } from "~/atoms/source-content" import { useUserRole, whoami } from "~/atoms/user" @@ -25,41 +27,40 @@ import { useInboxById } from "~/store/inbox" import { useRouteParamsSelector } from "./useRouteParams" -export const useEntryReadabilityToggle = ({ id, url }: { id: string; url: string }) => - useCallback(async () => { - const status = getReadabilityStatus()[id] - const isTurnOn = status !== ReadabilityStatus.INITIAL && !!status +export const toggleEntryReadability = async ({ id, url }: { id: string; url: string }) => { + const status = getReadabilityStatus()[id] + const isTurnOn = status !== ReadabilityStatus.INITIAL && !!status - if (!isTurnOn && url) { - setReadabilityStatus({ - [id]: ReadabilityStatus.WAITING, + if (!isTurnOn && url) { + setReadabilityStatus({ + [id]: ReadabilityStatus.WAITING, + }) + const result = await tipcClient + ?.readability({ + url, }) - const result = await tipcClient - ?.readability({ - url, - }) - .catch(() => { - setReadabilityStatus({ - [id]: ReadabilityStatus.FAILURE, - }) - }) - - if (result) { - const status = getReadabilityStatus()[id] - if (status !== ReadabilityStatus.WAITING) return + .catch(() => { setReadabilityStatus({ - [id]: ReadabilityStatus.SUCCESS, + [id]: ReadabilityStatus.FAILURE, }) - setReadabilityContent({ - [id]: result, - }) - } - } else { + }) + + if (result) { + const status = getReadabilityStatus()[id] + if (status !== ReadabilityStatus.WAITING) return setReadabilityStatus({ - [id]: ReadabilityStatus.INITIAL, + [id]: ReadabilityStatus.SUCCESS, + }) + setReadabilityContent({ + [id]: result, }) } - }, [id, url]) + } else { + setReadabilityStatus({ + [id]: ReadabilityStatus.INITIAL, + }) + } +} export type EntryActionItem = { id: FollowCommandId @@ -70,8 +71,17 @@ export type EntryActionItem = { disabled?: boolean } -export const useEntryActions = ({ entryId, view }: { entryId: string; view?: FeedViewType }) => { +export const useEntryActions = ({ + entryId, + view, + compact, +}: { + entryId: string + view?: FeedViewType + compact?: boolean +}) => { const entry = useEntry(entryId) + const entryReadabilityStatus = useEntryInReadabilityStatus(entry?.entries.id) const imageLength = entry?.entries.media?.filter((a) => a.type === "photo").length || 0 const feed = useFeedById(entry?.feedId, (feed) => { return { @@ -208,17 +218,36 @@ export const useEntryActions = ({ entryId, view }: { entryId: string; view?: Fee active: !!entry?.read, shortcut: shortcuts.entry.toggleRead.key, }, + { + id: COMMAND_ID.entry.tts, + onClick: runCmdFn(COMMAND_ID.entry.tts, [ + { entryId, entryContent: entry?.entries.content }, + ]), + hide: !IN_ELECTRON || compact || !entry?.entries.content, + shortcut: shortcuts.entry.tts.key, + }, + { + id: COMMAND_ID.entry.readability, + onClick: runCmdFn(COMMAND_ID.entry.readability, [ + { entryId, entryUrl: entry?.entries.url }, + ]), + hide: !IN_ELECTRON || compact || (view && views[view]!.wideMode) || !entry?.entries.url, + active: isInReadability(entryReadabilityStatus), + }, { id: COMMAND_ID.settings.customizeToolbar, onClick: runCmdFn(COMMAND_ID.settings.customizeToolbar, []), }, ].filter((config) => !config.hide) }, [ + compact, entry?.collections, + entry?.entries.content, entry?.entries.url, entry?.read, entry?.view, entryId, + entryReadabilityStatus, feed?.id, feed?.ownerUserId, hasEntry, @@ -241,11 +270,13 @@ export const useEntryActions = ({ entryId, view }: { entryId: string; view?: Fee export const useSortedEntryActions = ({ entryId, view, + compact, }: { entryId: string view?: FeedViewType + compact?: boolean }) => { - const entryActions = useEntryActions({ entryId, view }) + const entryActions = useEntryActions({ entryId, view, compact }) const orderMap = useToolbarOrderMap() const mainAction = useMemo( () => diff --git a/apps/desktop/src/renderer/src/modules/command/commands/entry.tsx b/apps/desktop/src/renderer/src/modules/command/commands/entry.tsx index 4be5033fa..3852f2243 100644 --- a/apps/desktop/src/renderer/src/modules/command/commands/entry.tsx +++ b/apps/desktop/src/renderer/src/modules/command/commands/entry.tsx @@ -7,15 +7,19 @@ import { toast } from "sonner" import { toggleShowAISummaryOnce } from "~/atoms/ai-summary" import { toggleShowAITranslationOnce } from "~/atoms/ai-translation" +import { AudioPlayer, getAudioPlayerAtomValue } from "~/atoms/player" +import { useGeneralSettingKey } from "~/atoms/settings/general" import { getShowSourceContent, toggleShowSourceContent, useSourceContentModal, } from "~/atoms/source-content" import { useUserRole } from "~/atoms/user" +import { toggleEntryReadability } from "~/hooks/biz/useEntryActions" import { navigateEntry } from "~/hooks/biz/useNavigateEntry" import { getRouteParams } from "~/hooks/biz/useRouteParams" import { tipcClient } from "~/lib/client" +import { parseHtml } from "~/lib/parse-html" import { useActivationModal } from "~/modules/activation" import { useGalleryModal } from "~/modules/entry-content/hooks" import { useTipModal } from "~/modules/wallet/hooks" @@ -100,6 +104,8 @@ export const useRegisterEntryCommands = () => { const role = useUserRole() const presentActivationModal = useActivationModal() + const voice = useGeneralSettingKey("voice") + useRegisterFollowCommand([ { id: COMMAND_ID.entry.tip, @@ -313,6 +319,43 @@ export const useRegisterEntryCommands = () => { openGalleryModal(entryId) }, }, + { + id: COMMAND_ID.entry.tts, + label: t("entry_content.header.play_tts"), + icon: , + run: async ({ entryId, entryContent }) => { + if (getAudioPlayerAtomValue().entryId === entryId) { + AudioPlayer.togglePlayAndPause() + } else { + const filePath = await tipcClient?.tts({ + id: entryId, + text: parseHtml(entryContent).toText(), + voice, + }) + if (filePath) { + AudioPlayer.mount({ + type: "audio", + entryId, + src: `file://${filePath}`, + currentTime: 0, + }) + } + } + }, + }, + { + id: COMMAND_ID.entry.readability, + label: t("entry_content.header.readability"), + icon: (props) => ( + + ), + run: async ({ entryId, entryUrl }) => { + return toggleEntryReadability({ + id: entryId, + url: entryUrl, + }) + }, + }, ]) useRegisterFollowCommand( diff --git a/apps/desktop/src/renderer/src/modules/command/commands/id.ts b/apps/desktop/src/renderer/src/modules/command/commands/id.ts index d2b50314f..9c7213f7b 100644 --- a/apps/desktop/src/renderer/src/modules/command/commands/id.ts +++ b/apps/desktop/src/renderer/src/modules/command/commands/id.ts @@ -13,6 +13,8 @@ export const COMMAND_ID = { toggleAITranslation: "entry:toggle-ai-translation", exportAsPDF: "entry:export-as-pdf", imageGallery: "entry:image-gallery", + tts: "entry:tts", + readability: "entry:ability", }, integration: { saveToEagle: "integration:save-to-eagle", diff --git a/apps/desktop/src/renderer/src/modules/command/commands/types.ts b/apps/desktop/src/renderer/src/modules/command/commands/types.ts index c0613a29a..81bc3cf8d 100644 --- a/apps/desktop/src/renderer/src/modules/command/commands/types.ts +++ b/apps/desktop/src/renderer/src/modules/command/commands/types.ts @@ -70,6 +70,16 @@ export type ImageGalleryCommand = Command<{ fn: ({ entryId }) => void }> +export type TTSCommand = Command<{ + id: typeof COMMAND_ID.entry.tts + fn: ({ entryId, entryContent }) => void +}> + +export type ReadabilityCommand = Command<{ + id: typeof COMMAND_ID.entry.readability + fn: ({ entryId, entryUrl }) => void +}> + export type EntryCommand = | TipCommand | StarCommand @@ -84,6 +94,8 @@ export type EntryCommand = | ToggleAISummaryCommand | ToggleAITranslationCommand | ImageGalleryCommand + | TTSCommand + | ReadabilityCommand // Settings commands diff --git a/apps/desktop/src/renderer/src/modules/entry-content/actions/electron-actions.tsx b/apps/desktop/src/renderer/src/modules/entry-content/actions/electron-actions.tsx deleted file mode 100644 index ea90ed5e0..000000000 --- a/apps/desktop/src/renderer/src/modules/entry-content/actions/electron-actions.tsx +++ /dev/null @@ -1,125 +0,0 @@ -import { ActionButton } from "@follow/components/ui/button/index.js" -import { DividerVertical } from "@follow/components/ui/divider/index.js" -import { FeedViewType, views } from "@follow/constants" -import type { CombinedEntryModel } from "@follow/models/types" -import { IN_ELECTRON } from "@follow/shared/constants" -import { cn } from "@follow/utils/utils" -import { noop } from "foxact/noop" -import { useMemo, useState } from "react" -import { useTranslation } from "react-i18next" - -import { AudioPlayer, getAudioPlayerAtomValue } from "~/atoms/player" -import { - isInReadability, - ReadabilityStatus, - useEntryInReadabilityStatus, -} from "~/atoms/readability" -import { useGeneralSettingKey } from "~/atoms/settings/general" -import { shortcuts } from "~/constants/shortcuts" -import { useEntryReadabilityToggle } from "~/hooks/biz/useEntryActions" -import { tipcClient } from "~/lib/client" -import { parseHtml } from "~/lib/parse-html" -import type { FlatEntryModel } from "~/store/entry" -import { useFeedById } from "~/store/feed" - -export const ElectronAdditionActions = IN_ELECTRON - ? ({ - view = FeedViewType.Articles, - entry, - }: { - view: FeedViewType - entry?: FlatEntryModel | null - }) => { - const entryReadabilityStatus = useEntryInReadabilityStatus(entry?.entries.id) - - const { t } = useTranslation() - - const feed = useFeedById(entry?.feedId) - - const populatedEntry = useMemo(() => { - if (!entry) return null - if (!feed) return null - return { - ...entry, - feeds: feed!, - } as CombinedEntryModel - }, [entry, feed]) - - const readabilityToggle = useEntryReadabilityToggle({ - id: populatedEntry?.entries.id ?? "", - url: populatedEntry?.entries.url ?? "", - }) - - const [ttsLoading, setTtsLoading] = useState(false) - const voice = useGeneralSettingKey("voice") - - if (!populatedEntry) return null - - const items = [ - { - key: "tts", - name: t("entry_content.header.play_tts"), - shortcut: shortcuts.entry.tts.key, - className: ttsLoading ? "i-mgc-loading-3-cute-re animate-spin" : "i-mgc-voice-cute-re", - - disabled: !populatedEntry.entries.content, - onClick: async () => { - if (ttsLoading) return - if (!populatedEntry.entries.content) return - setTtsLoading(true) - if (getAudioPlayerAtomValue().entryId === populatedEntry.entries.id) { - AudioPlayer.togglePlayAndPause() - } else { - const filePath = await tipcClient?.tts({ - id: populatedEntry.entries.id, - text: parseHtml(populatedEntry.entries.content).toText(), - voice, - }) - if (filePath) { - AudioPlayer.mount({ - type: "audio", - entryId: populatedEntry.entries.id, - src: `file://${filePath}`, - currentTime: 0, - }) - } - } - setTtsLoading(false) - }, - }, - { - name: t("entry_content.header.readability"), - className: cn( - isInReadability(entryReadabilityStatus) - ? `i-mgc-docment-cute-fi` - : `i-mgc-docment-cute-re`, - entryReadabilityStatus === ReadabilityStatus.WAITING ? `animate-pulse` : "", - ), - key: "readability", - hide: views[view]!.wideMode || !populatedEntry.entries.url, - active: isInReadability(entryReadabilityStatus), - onClick: readabilityToggle, - }, - ] - - if (items.length === 0) return null - return ( - <> - {items - .filter((item) => !item.hide) - .map((item) => ( - } - active={item.active} - shortcut={item.shortcut} - onClick={item.onClick} - tooltip={item.name} - key={item.name} - /> - ))} - - - ) - } - : noop diff --git a/apps/desktop/src/renderer/src/modules/entry-content/actions/header-actions.tsx b/apps/desktop/src/renderer/src/modules/entry-content/actions/header-actions.tsx index 24c774364..573ef7ed9 100644 --- a/apps/desktop/src/renderer/src/modules/entry-content/actions/header-actions.tsx +++ b/apps/desktop/src/renderer/src/modules/entry-content/actions/header-actions.tsx @@ -8,8 +8,16 @@ import { COMMAND_ID } from "~/modules/command/commands/id" import { useCommandHotkey } from "~/modules/command/hooks/use-register-hotkey" import { useEntry } from "~/store/entry/hooks" -export const EntryHeaderActions = ({ entryId, view }: { entryId: string; view?: FeedViewType }) => { - const { mainAction: actionConfigs } = useSortedEntryActions({ entryId, view }) +export const EntryHeaderActions = ({ + entryId, + view, + compact, +}: { + entryId: string + view?: FeedViewType + compact?: boolean +}) => { + const { mainAction: actionConfigs } = useSortedEntryActions({ entryId, view, compact }) const entry = useEntry(entryId) const hasModal = useHasModal() diff --git a/apps/desktop/src/renderer/src/modules/entry-content/header.electron.tsx b/apps/desktop/src/renderer/src/modules/entry-content/header.electron.tsx index 3748d1735..85b11427f 100644 --- a/apps/desktop/src/renderer/src/modules/entry-content/header.electron.tsx +++ b/apps/desktop/src/renderer/src/modules/entry-content/header.electron.tsx @@ -6,7 +6,6 @@ import { memo } from "react" import { useUISettingKey } from "~/atoms/settings/ui" import { useEntry } from "~/store/entry/hooks" -import { ElectronAdditionActions } from "./actions/electron-actions" import { EntryHeaderActions } from "./actions/header-actions" import { MoreActions } from "./actions/more-actions" import { useEntryContentScrollToTop, useEntryTitleMeta } from "./atoms" @@ -71,9 +70,7 @@ function EntryHeaderImpl({ view, entryId, className, compact }: EntryHeaderProps
- {!compact && } - - +
diff --git a/apps/desktop/src/renderer/src/modules/entry-content/index.shared.tsx b/apps/desktop/src/renderer/src/modules/entry-content/index.shared.tsx index 22be07ce4..4f1b2c09a 100644 --- a/apps/desktop/src/renderer/src/modules/entry-content/index.shared.tsx +++ b/apps/desktop/src/renderer/src/modules/entry-content/index.shared.tsx @@ -21,7 +21,7 @@ import { } from "~/atoms/readability" import { enableShowSourceContent } from "~/atoms/source-content" import { Toc } from "~/components/ui/markdown/components/Toc" -import { useEntryReadabilityToggle } from "~/hooks/biz/useEntryActions" +import { toggleEntryReadability } from "~/hooks/biz/useEntryActions" import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams" import { getNewIssueUrl } from "~/lib/issues" import { @@ -159,10 +159,6 @@ export const ViewSourceContentAutoToggleEffect = () => { } export const ReadabilityAutoToggleEffect = ({ url, id }: { url: string; id: string }) => { - const toggle = useEntryReadabilityToggle({ - id, - url, - }) const onceRef = useRef(false) useEffect(() => { @@ -171,9 +167,9 @@ export const ReadabilityAutoToggleEffect = ({ url, id }: { url: string; id: stri setReadabilityStatus({ [id]: ReadabilityStatus.INITIAL, }) - toggle() + toggleEntryReadability({ id, url }) } - }, [toggle]) + }, [id, url]) return null } diff --git a/eslint.config.mjs b/eslint.config.mjs index d21de4f80..e595ace97 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -43,6 +43,7 @@ export default defineConfig( "@eslint-react/no-clone-element": 0, "@eslint-react/hooks-extra/no-direct-set-state-in-use-effect": 0, "@eslint-react/dom/no-flush-sync": 1, + "@eslint-react/hooks-extra/no-unnecessary-use-callback": "warn", // NOTE: Disable this temporarily "react-compiler/react-compiler": 0, "no-restricted-syntax": 0,