refactor(desktop): move electron actions to entry actions (#3483)

Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>
This commit is contained in:
Konv Suu
2025-04-14 16:16:40 +08:00
committed by GitHub
co-authored by Stephen Zhou
parent ecf1c0f660
commit 5cde69212c
9 changed files with 135 additions and 170 deletions
@@ -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(
() =>
@@ -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: <i className="i-mgc-voice-cute-re" />,
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) => (
<i className={props?.isActive ? "i-mgc-docment-cute-fi" : "i-mgc-docment-cute-re"} />
),
run: async ({ entryId, entryUrl }) => {
return toggleEntryReadability({
id: entryId,
url: entryUrl,
})
},
},
])
useRegisterFollowCommand(
@@ -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",
@@ -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
@@ -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) => (
<ActionButton
disabled={item.disabled}
icon={<i className={item.className} />}
active={item.active}
shortcut={item.shortcut}
onClick={item.onClick}
tooltip={item.name}
key={item.name}
/>
))}
<DividerVertical className="mx-2 w-px" />
</>
)
}
: noop
@@ -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()
@@ -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
</div>
<div className="relative flex shrink-0 items-center justify-end gap-2">
{!compact && <ElectronAdditionActions view={view} entry={entry} key={entry.entries.id} />}
<EntryHeaderActions entryId={entry.entries.id} view={view} />
<EntryHeaderActions entryId={entry.entries.id} view={view} compact={compact} />
<MoreActions entryId={entry.entries.id} />
</div>
</div>
@@ -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
}
+1
View File
@@ -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,