From 77fa6088fb2887806b3bcc49e71bce8b1a3aeab6 Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 17 Dec 2024 20:07:33 +0800 Subject: [PATCH] refactor: streamline entry action handling and replace ActionButton with CommandActionButton - Removed unused imports and simplified the use of commands in `useEntryActions`. - Updated `EntryItemWrapper`, `HeaderActions`, and `MoreActions` components to utilize `CommandActionButton` for better command handling. - Introduced `CommandDropdownMenuItem` for consistent command representation in dropdowns. - Enhanced readability and maintainability by reducing redundant code and improving type safety. Signed-off-by: Innei --- .../components/ui/button/command-button.tsx | 28 +++ .../src/hooks/biz/useEntryActions.tsx | 46 ++--- .../entry-column/Items/social-media-item.tsx | 9 +- .../entry-column/layouts/EntryItemWrapper.tsx | 19 +- .../entry-content/actions/header-actions.tsx | 11 +- .../entry-content/actions/more-actions.tsx | 27 ++- .../modules/entry-content/header.mobile.tsx | 48 +++-- .../src/ui/button/action-button.tsx | 169 ++++++++++++++++++ packages/components/src/ui/button/index.tsx | 168 +---------------- .../components/src/ui/button/interface.ts | 25 +++ 10 files changed, 316 insertions(+), 234 deletions(-) create mode 100644 apps/renderer/src/components/ui/button/command-button.tsx create mode 100644 packages/components/src/ui/button/action-button.tsx create mode 100644 packages/components/src/ui/button/interface.ts diff --git a/apps/renderer/src/components/ui/button/command-button.tsx b/apps/renderer/src/components/ui/button/command-button.tsx new file mode 100644 index 000000000..e2f6a254e --- /dev/null +++ b/apps/renderer/src/components/ui/button/command-button.tsx @@ -0,0 +1,28 @@ +import type { ActionButtonProps } from "@follow/components/ui/button/action-button.js" +import { ActionButton } from "@follow/components/ui/button/action-button.js" +import { forwardRef } from "react" + +import { useCommand } from "~/modules/command/hooks/use-command" +import type { FollowCommandId } from "~/modules/command/types" + +export interface CommandActionButtonProps extends ActionButtonProps { + commandId: FollowCommandId + onClick: () => void +} +export const CommandActionButton = forwardRef( + (props, ref) => { + const command = useCommand(props.commandId) + if (!command) return null + const { icon, label } = command + + return ( + + ) + }, +) diff --git a/apps/renderer/src/hooks/biz/useEntryActions.tsx b/apps/renderer/src/hooks/biz/useEntryActions.tsx index 709e07296..a6332c666 100644 --- a/apps/renderer/src/hooks/biz/useEntryActions.tsx +++ b/apps/renderer/src/hooks/biz/useEntryActions.tsx @@ -1,6 +1,5 @@ import { isMobile } from "@follow/components/hooks/useMobile.js" import { FeedViewType } from "@follow/constants" -import type { ReactNode } from "react" import { useCallback, useMemo } from "react" import { useShowAISummary } from "~/atoms/ai-summary" @@ -16,7 +15,7 @@ import { whoami } from "~/atoms/user" import { shortcuts } from "~/constants/shortcuts" import { tipcClient } from "~/lib/client" import { COMMAND_ID } from "~/modules/command/commands/id" -import { getCommand, useCommands, useRunCommandFn } from "~/modules/command/hooks/use-command" +import { useRunCommandFn } from "~/modules/command/hooks/use-command" import type { FollowCommandId } from "~/modules/command/types" import { useEntry } from "~/store/entry" import { useFeedById } from "~/store/feed" @@ -62,11 +61,10 @@ export const useEntryReadabilityToggle = ({ id, url }: { id: string; url: string export type EntryActionItem = { id: FollowCommandId - name: string - icon?: ReactNode - active?: boolean - shortcut?: string onClick: () => void + hide?: boolean + shortcut?: string + active?: boolean } export const useEntryActions = ({ entryId, view }: { entryId: string; view?: FeedViewType }) => { @@ -87,12 +85,11 @@ export const useEntryActions = ({ entryId, view }: { entryId: string; view?: Fee const isShowAISummary = useShowAISummary() const isShowAITranslation = useShowAITranslation() - const commands = useCommands() - const runCmdFn = useRunCommandFn() - const actionConfigs = useMemo(() => { - void commands - if (!entryId) return [] + const hasEntry = !!entry + + const actionConfigs: EntryActionItem[] = useMemo(() => { + if (!hasEntry) return [] return [ { id: COMMAND_ID.integration.saveToEagle, @@ -192,32 +189,27 @@ export const useEntryActions = ({ entryId, view }: { entryId: string; view?: Fee { id: COMMAND_ID.entry.read, onClick: runCmdFn(COMMAND_ID.entry.read, [{ entryId }]), - hide: !entry || !!entry.read || !!entry.collections || !!inList, + hide: !hasEntry || !entry.read || !!entry.collections || !!inList, shortcut: shortcuts.entry.toggleRead.key, }, { id: COMMAND_ID.entry.unread, onClick: runCmdFn(COMMAND_ID.entry.unread, [{ entryId }]), - hide: !entry || !entry.read || !!entry.collections || !!inList, + hide: !hasEntry || !entry.read || !!entry.collections || !!inList, shortcut: shortcuts.entry.toggleRead.key, }, - ] - .filter((config) => !config.hide) - .map((config) => { - const cmd = getCommand(config.id) - if (!cmd) return null - return { - ...config, - name: cmd.label.title, - icon: cmd.icon, - } - }) - .filter((i) => i !== null) + ].filter((config) => !config.hide) }, [ - entry, + entry?.collections, + entry?.entries.url, + entry?.read, + entry?.settings?.summary, + entry?.settings?.translation, + entry?.view, entryId, feed?.id, feed?.ownerUserId, + hasEntry, inList, isInbox, isShowAISummary, @@ -225,8 +217,6 @@ export const useEntryActions = ({ entryId, view }: { entryId: string; view?: Fee isShowSourceContent, runCmdFn, view, - - commands, ]) return actionConfigs diff --git a/apps/renderer/src/modules/entry-column/Items/social-media-item.tsx b/apps/renderer/src/modules/entry-column/Items/social-media-item.tsx index 35665fc7b..98db5a781 100644 --- a/apps/renderer/src/modules/entry-column/Items/social-media-item.tsx +++ b/apps/renderer/src/modules/entry-column/Items/social-media-item.tsx @@ -1,7 +1,6 @@ import { PassviseFragment } from "@follow/components/common/Fragment.js" import { useMobile } from "@follow/components/hooks/useMobile.js" import { AutoResizeHeight } from "@follow/components/ui/auto-resize-height/index.js" -import { ActionButton } from "@follow/components/ui/button/index.js" import { Skeleton } from "@follow/components/ui/skeleton/index.jsx" import type { MediaModel } from "@follow/shared/hono" import { LRUCache } from "@follow/utils/lru-cache" @@ -11,6 +10,7 @@ import { useLayoutEffect, useMemo, useRef, useState } from "react" import { useTranslation } from "react-i18next" import { useGeneralSettingKey } from "~/atoms/settings/general" +import { CommandActionButton } from "~/components/ui/button/command-button" import { RelativeTime } from "~/components/ui/datetime" import { Media } from "~/components/ui/media" import { usePreviewMedia } from "~/components/ui/media/hooks" @@ -143,12 +143,7 @@ const ActionBar = ({ entryId }: { entryId: string }) => { item.id !== COMMAND_ID.entry.openInBrowser, ) .map((item) => ( - + ))} ) diff --git a/apps/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx b/apps/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx index 306c8feb2..e38c42d79 100644 --- a/apps/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx +++ b/apps/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx @@ -15,6 +15,7 @@ import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry" import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams" import { useContextMenu } from "~/hooks/common/useContextMenu" import { COMMAND_ID } from "~/modules/command/commands/id" +import { getCommand } from "~/modules/command/hooks/use-command" import type { FlatEntryModel } from "~/store/entry" import { entryActions } from "~/store/entry" @@ -106,12 +107,18 @@ export const EntryItemWrapper: FC< ] as string[] ).includes(item.id), ) - .map((item) => ({ - type: "text" as const, - label: item.name, - click: () => item.onClick(), - shortcut: item.shortcut, - })), + .map((item) => { + const cmd = getCommand(item.id) + + if (!cmd) return null + + return { + type: "text" as const, + label: cmd?.label.title || "aa", + click: () => item.onClick(), + shortcut: item.shortcut, + } + }), { type: "separator" as const, }, diff --git a/apps/renderer/src/modules/entry-content/actions/header-actions.tsx b/apps/renderer/src/modules/entry-content/actions/header-actions.tsx index fa9125507..df00a3292 100644 --- a/apps/renderer/src/modules/entry-content/actions/header-actions.tsx +++ b/apps/renderer/src/modules/entry-content/actions/header-actions.tsx @@ -1,6 +1,6 @@ -import { ActionButton } from "@follow/components/ui/button/index.js" import type { FeedViewType } from "@follow/constants" +import { CommandActionButton } from "~/components/ui/button/command-button" import { useHasModal } from "~/components/ui/modal/stacked/hooks" import { shortcuts } from "~/constants/shortcuts" import { useEntryActions } from "~/hooks/biz/useEntryActions" @@ -36,14 +36,13 @@ export const EntryHeaderActions = ({ entryId, view }: { entryId: string; view?: ) .map((config) => { return ( - ) }) diff --git a/apps/renderer/src/modules/entry-content/actions/more-actions.tsx b/apps/renderer/src/modules/entry-content/actions/more-actions.tsx index e007e3be0..cab0b16dd 100644 --- a/apps/renderer/src/modules/entry-content/actions/more-actions.tsx +++ b/apps/renderer/src/modules/entry-content/actions/more-actions.tsx @@ -10,6 +10,8 @@ import { } from "~/components/ui/dropdown-menu/dropdown-menu" import { useEntryActions } from "~/hooks/biz/useEntryActions" import { COMMAND_ID } from "~/modules/command/commands/id" +import { useCommand } from "~/modules/command/hooks/use-command" +import type { FollowCommandId } from "~/modules/command/types" export const MoreActions = ({ entryId, view }: { entryId: string; view?: FeedViewType }) => { const actionConfigs = useEntryActions({ entryId, view }) @@ -36,16 +38,25 @@ export const MoreActions = ({ entryId, view }: { entryId: string; view?: FeedVie {availableActions.map((config) => ( - - {config.name} - + ))} ) } + +const CommandDropdownMenuItem = ({ + commandId, + onClick, +}: { + commandId: FollowCommandId + onClick: () => void +}) => { + const command = useCommand(commandId) + if (!command) return null + return ( + + {command.label.title} + + ) +} diff --git a/apps/renderer/src/modules/entry-content/header.mobile.tsx b/apps/renderer/src/modules/entry-content/header.mobile.tsx index f39949736..96e08b351 100644 --- a/apps/renderer/src/modules/entry-content/header.mobile.tsx +++ b/apps/renderer/src/modules/entry-content/header.mobile.tsx @@ -1,4 +1,4 @@ -import { ActionButton, MotionButtonBase } from "@follow/components/ui/button/index.js" +import { MotionButtonBase } from "@follow/components/ui/button/index.js" import { RootPortal } from "@follow/components/ui/portal/index.js" import { findElementInShadowDOM } from "@follow/utils/dom" import { clsx, cn } from "@follow/utils/utils" @@ -10,6 +10,7 @@ import { useEventCallback } from "usehooks-ts" import { useUISettingKey } from "~/atoms/settings/ui" import { HeaderTopReturnBackButton } from "~/components/mobile/button" +import { CommandActionButton } from "~/components/ui/button/command-button" import { useScrollTracking, useTocItems } from "~/components/ui/markdown/components/hooks" import { ENTRY_CONTENT_RENDER_CONTAINER_ID } from "~/constants/dom" import type { EntryActionItem } from "~/hooks/biz/useEntryActions" @@ -17,6 +18,8 @@ import { useEntryActions } from "~/hooks/biz/useEntryActions" import { useEntry } from "~/store/entry/hooks" import { COMMAND_ID } from "../command/commands/id" +import { useCommand } from "../command/hooks/use-command" +import type { FollowCommandId } from "../command/types" import { useEntryContentScrollToTop, useEntryTitleMeta } from "./atoms" import type { EntryHeaderProps } from "./header.shared" @@ -87,13 +90,12 @@ function EntryHeaderImpl({ view, entryId, className }: EntryHeaderProps) { )} > {actionConfigs.map((item) => ( - ))} @@ -177,18 +179,14 @@ const HeaderRightActions = ({ >
{actions.map((item) => ( - { setCtxOpen(false) item.onClick?.() }} - key={item.name} - layout={false} - className="flex w-full items-center gap-2 px-4 py-2" - > - {item.icon} - {item.name} - + /> ))}
@@ -200,6 +198,28 @@ const HeaderRightActions = ({ ) } + +const CommandMotionButton = ({ + commandId, + onClick, +}: { + commandId: FollowCommandId + onClick: () => void +}) => { + const command = useCommand(commandId) + if (!command) return null + return ( + + {command.icon} + {command.label.title} + + ) +} + const TableOfContentsIcon = ({ className = "size-6" }) => { return ( + tooltip?: React.ReactNode + tooltipSide?: "top" | "bottom" + active?: boolean + disabled?: boolean + shortcut?: string + disableTriggerShortcut?: boolean + enableHoverableContent?: boolean + size?: "sm" | "md" | "base" + + /** + * @description only trigger shortcut when focus with in `` + * @default false + */ + shortcutOnlyFocusWithIn?: boolean +} + +const actionButtonStyleVariant = { + size: { + base: tw`text-xl size-8`, + sm: tw`text-sm size-6`, + }, +} + +export const ActionButton = React.forwardRef< + HTMLButtonElement, + ComponentType & React.HTMLAttributes +>( + ( + { + icon, + + tooltip, + className, + tooltipSide, + children, + active, + shortcut, + disabled, + disableTriggerShortcut, + enableHoverableContent, + size = "base", + shortcutOnlyFocusWithIn, + onClick, + ...rest + }, + ref, + ) => { + const finalShortcut = + getOS() === "Windows" ? shortcut?.replace("meta", "ctrl").replace("Meta", "Ctrl") : shortcut + const buttonRef = React.useRef(null) + React.useImperativeHandle(ref, () => buttonRef.current!) + + const [loading, setLoading] = useState(false) + + const Trigger = ( + + ) : typeof icon === "function" ? ( + React.createElement(icon, { + className: "size-4 grayscale text-current", + }) + ) : ( + icon + )} + + {children} + + ) + + return ( + <> + {finalShortcut && !disableTriggerShortcut && ( + buttonRef.current?.click()} + shortcutOnlyFocusWithIn={shortcutOnlyFocusWithIn} + /> + )} + {tooltip ? ( + + + {Trigger} + + + + {tooltip} + {!!finalShortcut && ( +
+ {finalShortcut} +
+ )} +
+
+
+ ) : ( + Trigger + )} + + ) + }, +) + +const HotKeyTrigger = ({ + shortcut, + fn, + options, + shortcutOnlyFocusWithIn, +}: { + shortcut: string + fn: () => void + options?: Options + shortcutOnlyFocusWithIn?: boolean +}) => { + const isFocusWithIn = useFocusable() + const enabledInOptions = options?.enabled || true + + useHotkeys(shortcut, fn, { + preventDefault: true, + enabled: shortcutOnlyFocusWithIn + ? isFocusWithIn + ? enabledInOptions + : false + : enabledInOptions, + ...options, + }) + return null +} diff --git a/packages/components/src/ui/button/index.tsx b/packages/components/src/ui/button/index.tsx index b47183700..4cf502e57 100644 --- a/packages/components/src/ui/button/index.tsx +++ b/packages/components/src/ui/button/index.tsx @@ -1,18 +1,11 @@ -import { useFocusable } from "@follow/components/common/Focusable.jsx" import { useMobile } from "@follow/components/hooks/useMobile.js" import { LoadingCircle } from "@follow/components/ui/loading/index.jsx" -import { stopPropagation } from "@follow/utils/dom" -import { cn, getOS } from "@follow/utils/utils" +import { cn } from "@follow/utils/utils" import type { VariantProps } from "class-variance-authority" import type { HTMLMotionProps } from "framer-motion" import { m } from "framer-motion" import * as React from "react" -import { useState } from "react" -import type { Options } from "react-hotkeys-hook" -import { useHotkeys } from "react-hotkeys-hook" -import { KbdCombined } from "../kbd/Kbd" -import { Tooltip, TooltipContent, TooltipPortal, TooltipTrigger } from "../tooltip" import { styledButtonVariant } from "./variants" export interface BaseButtonProps { @@ -21,163 +14,6 @@ export interface BaseButtonProps { // BIZ buttons -export interface ActionButtonProps { - icon?: React.ReactNode | React.FC - tooltip?: React.ReactNode - tooltipSide?: "top" | "bottom" - active?: boolean - disabled?: boolean - shortcut?: string - disableTriggerShortcut?: boolean - enableHoverableContent?: boolean - size?: "sm" | "md" | "base" - - /** - * @description only trigger shortcut when focus with in `` - * @default false - */ - shortcutOnlyFocusWithIn?: boolean -} - -const actionButtonStyleVariant = { - size: { - base: tw`text-xl size-8`, - sm: tw`text-sm size-6`, - }, -} -export const ActionButton = React.forwardRef< - HTMLButtonElement, - ComponentType & React.HTMLAttributes ->( - ( - { - icon, - - tooltip, - className, - tooltipSide, - children, - active, - shortcut, - disabled, - disableTriggerShortcut, - enableHoverableContent, - size = "base", - shortcutOnlyFocusWithIn, - onClick, - ...rest - }, - ref, - ) => { - const finalShortcut = - getOS() === "Windows" ? shortcut?.replace("meta", "ctrl").replace("Meta", "Ctrl") : shortcut - const buttonRef = React.useRef(null) - React.useImperativeHandle(ref, () => buttonRef.current!) - - const [loading, setLoading] = useState(false) - - const Trigger = ( - - ) - - return ( - <> - {finalShortcut && !disableTriggerShortcut && ( - buttonRef.current?.click()} - shortcutOnlyFocusWithIn={shortcutOnlyFocusWithIn} - /> - )} - {tooltip ? ( - - - {Trigger} - - - - {tooltip} - {!!finalShortcut && ( -
- {finalShortcut} -
- )} -
-
-
- ) : ( - Trigger - )} - - ) - }, -) - -const HotKeyTrigger = ({ - shortcut, - fn, - options, - shortcutOnlyFocusWithIn, -}: { - shortcut: string - fn: () => void - options?: Options - shortcutOnlyFocusWithIn?: boolean -}) => { - const isFocusWithIn = useFocusable() - const enabledInOptions = options?.enabled || true - - useHotkeys(shortcut, fn, { - preventDefault: true, - enabled: shortcutOnlyFocusWithIn - ? isFocusWithIn - ? enabledInOptions - : false - : enabledInOptions, - ...options, - }) - return null -} const motionBaseMap = { pc: { whileFocus: { scale: 1.02 }, @@ -298,3 +134,5 @@ export const IconButton = React.forwardRef< ) }) + +export { ActionButton, type ActionButtonProps } from "./action-button" diff --git a/packages/components/src/ui/button/interface.ts b/packages/components/src/ui/button/interface.ts new file mode 100644 index 000000000..cebfd37be --- /dev/null +++ b/packages/components/src/ui/button/interface.ts @@ -0,0 +1,25 @@ +import type * as React from "react" + +export interface BaseButtonProps { + isLoading?: boolean +} + +// BIZ buttons + +export interface ActionButtonProps { + icon?: React.ReactNode | React.FC + tooltip?: React.ReactNode + tooltipSide?: "top" | "bottom" + active?: boolean + disabled?: boolean + shortcut?: string + disableTriggerShortcut?: boolean + enableHoverableContent?: boolean + size?: "sm" | "md" | "base" + + /** + * @description only trigger shortcut when focus with in `` + * @default false + */ + shortcutOnlyFocusWithIn?: boolean +}