From d0bf0fb657cf7dfd7c93f6ab51c7c963878944df Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 17 Dec 2024 00:32:28 +0800 Subject: [PATCH] refactor: replace useGetCommand with getCommand for improved command retrieval - Updated the command retrieval method in various hooks and components to use the new getCommand function instead of the deprecated useGetCommand. - Refactored related tests to ensure type correctness with the new command retrieval approach. - Enhanced the PictureWaterFallItem component by optimizing mouse event handling with useCallback for better performance. - Improved animation handling in the PictureWaterFallItem component for smoother transitions. This change streamlines command access and enhances component performance. Signed-off-by: Innei --- .../src/hooks/biz/useEntryActions.tsx | 6 ++-- .../command/hooks/use-command.test-d.ts | 8 ++--- .../src/modules/command/hooks/use-command.ts | 22 ++++++------ .../command/hooks/use-register-hotkey.ts | 3 +- .../entry-column/Items/picture-item.tsx | 34 +++++++++++++------ 5 files changed, 42 insertions(+), 31 deletions(-) diff --git a/apps/renderer/src/hooks/biz/useEntryActions.tsx b/apps/renderer/src/hooks/biz/useEntryActions.tsx index 64b095f48..ff791fc5a 100644 --- a/apps/renderer/src/hooks/biz/useEntryActions.tsx +++ b/apps/renderer/src/hooks/biz/useEntryActions.tsx @@ -16,7 +16,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 { useGetCommand, useRunCommandFn } from "~/modules/command/hooks/use-command" +import { getCommand, useRunCommandFn } from "~/modules/command/hooks/use-command" import type { FollowCommandId } from "~/modules/command/types" import { useEntry } from "~/store/entry" import { useFeedById } from "~/store/feed" @@ -87,7 +87,6 @@ export const useEntryActions = ({ entryId, view }: { entryId: string; view?: Fee const isShowAISummary = useShowAISummary() const isShowAITranslation = useShowAITranslation() - const getCmd = useGetCommand() const runCmdFn = useRunCommandFn() const actionConfigs = useMemo(() => { if (!entryId) return [] @@ -202,7 +201,7 @@ export const useEntryActions = ({ entryId, view }: { entryId: string; view?: Fee ] .filter((config) => !config.hide) .map((config) => { - const cmd = getCmd(config.id) + const cmd = getCommand(config.id) if (!cmd) return null return { ...config, @@ -216,7 +215,6 @@ export const useEntryActions = ({ entryId, view }: { entryId: string; view?: Fee entryId, feed?.id, feed?.ownerUserId, - getCmd, inList, isInbox, isShowAISummary, diff --git a/apps/renderer/src/modules/command/hooks/use-command.test-d.ts b/apps/renderer/src/modules/command/hooks/use-command.test-d.ts index 316f3d3f6..04e6eef5a 100644 --- a/apps/renderer/src/modules/command/hooks/use-command.test-d.ts +++ b/apps/renderer/src/modules/command/hooks/use-command.test-d.ts @@ -2,12 +2,10 @@ import { assertType, expectTypeOf, test } from "vitest" import { COMMAND_ID } from "../commands/id" import type { TipCommand } from "../commands/types" -import { useCommand, useGetCommand, useRunCommandFn } from "./use-command" +import { getCommand, useCommand, useRunCommandFn } from "./use-command" -test("useGetCommand types work properly", () => { - const getCmd = useGetCommand() - expectTypeOf(getCmd).toBeFunction() - expectTypeOf(getCmd(COMMAND_ID.entry.tip)).toMatchTypeOf() +test("getCommand types work properly", () => { + expectTypeOf(getCommand(COMMAND_ID.entry.tip)).toMatchTypeOf() // @ts-expect-error - get an unknown command should throw an error assertType(getCmd("unknown command")) diff --git a/apps/renderer/src/modules/command/hooks/use-command.ts b/apps/renderer/src/modules/command/hooks/use-command.ts index 0300e1319..ed1e9fa91 100644 --- a/apps/renderer/src/modules/command/hooks/use-command.ts +++ b/apps/renderer/src/modules/command/hooks/use-command.ts @@ -1,30 +1,32 @@ +import { jotaiStore } from "@follow/utils/jotai" import { useAtomValue } from "jotai" -import { useCallback } from "react" +import { selectAtom } from "jotai/utils" +import { useCallback, useMemo } from "react" import { CommandRegistry } from "../registry/registry" import type { FollowCommandId, FollowCommandMap } from "../types" -export function useGetCommand() { - const commands = useAtomValue(CommandRegistry.atom) as FollowCommandMap - return (id: T): FollowCommandMap[T] | null => - id in commands ? commands[id] : null +export const getCommand = (id: T) => { + const commands = jotaiStore.get(CommandRegistry.atom) as FollowCommandMap + return id in commands ? commands[id] : null } export function useCommand(id: T): FollowCommandMap[T] | null { - const getCmd = useGetCommand() - return getCmd(id) + const commands = useAtomValue( + useMemo(() => selectAtom(CommandRegistry.atom, (commands) => commands[id]), [id]), + ) + return commands as FollowCommandMap[T] | null } const noop = () => {} export function useRunCommandFn() { - const getCmd = useGetCommand() return useCallback( (id: T, args: Parameters) => { - const cmd = getCmd(id) + const cmd = getCommand(id) if (!cmd) return noop // @ts-expect-error - The type should be discriminated return () => cmd.run(...args) }, - [getCmd], + [], ) } diff --git a/apps/renderer/src/modules/command/hooks/use-register-hotkey.ts b/apps/renderer/src/modules/command/hooks/use-register-hotkey.ts index 0e1fa1750..5c4f1ff50 100644 --- a/apps/renderer/src/modules/command/hooks/use-register-hotkey.ts +++ b/apps/renderer/src/modules/command/hooks/use-register-hotkey.ts @@ -1,7 +1,7 @@ import { useHotkeys } from "react-hotkeys-hook" import type { FollowCommand, FollowCommandId } from "../types" -import { useGetCommand } from "./use-command" +import { getCommand } from "./use-command" interface RegisterHotkeyOptions { shortcut: string @@ -17,7 +17,6 @@ export const useCommandHotkey = ({ when, args, }: RegisterHotkeyOptions) => { - const getCommand = useGetCommand() useHotkeys( shortcut, () => { diff --git a/apps/renderer/src/modules/entry-column/Items/picture-item.tsx b/apps/renderer/src/modules/entry-column/Items/picture-item.tsx index 57d84546a..903d13500 100644 --- a/apps/renderer/src/modules/entry-column/Items/picture-item.tsx +++ b/apps/renderer/src/modules/entry-column/Items/picture-item.tsx @@ -9,7 +9,7 @@ import { FeedViewType } from "@follow/constants" import { cn } from "@follow/utils/utils" import { AnimatePresence, m } from "framer-motion" import type { PropsWithChildren } from "react" -import { memo, useContext, useEffect, useMemo, useState } from "react" +import { memo, useCallback, useContext, useEffect, useMemo, useState } from "react" import { useTranslation } from "react-i18next" import { SwipeMedia } from "~/components/ui/media/SwipeMedia" @@ -64,6 +64,9 @@ const proxySize = { width: 600, height: 0, } + +const footerAnimate = { opacity: 1, y: 0 } +const footerExit = { opacity: 0, y: 10 } export const PictureWaterFallItem = memo(function PictureWaterFallItem({ entryId, entryPreview, @@ -93,19 +96,26 @@ export const PictureWaterFallItem = memo(function PictureWaterFallItem({ const [isMouseEnter, setIsMouseEnter] = useState(false) - if (!entry) return null + const media = useMemo(() => filterSmallMedia(entry?.entries.media || []), [entry?.entries.media]) - const media = filterSmallMedia(entry.entries.media) + const handleMouseEnter = useCallback(() => { + setIsMouseEnter(true) + }, []) + const handleMouseLeave = useCallback(() => { + setIsMouseEnter(false) + }, []) + if (media?.length === 0) return null + if (!entry) return null return (
setIsMouseEnter(true)} - onMouseLeave={() => setIsMouseEnter(false)} - onTouchMove={() => setIsMouseEnter(true)} - onTouchEnd={() => setIsMouseEnter(false)} + onMouseEnter={handleMouseEnter} + onMouseLeave={handleMouseLeave} + onTouchMove={handleMouseEnter} + onTouchEnd={handleMouseLeave} className={className} > {isMouseEnter && ( @@ -200,6 +210,10 @@ const MasonryItemFixedDimensionWrapper = ( ) } +MasonryItemFixedDimensionWrapper.whyDidYouRender = { + logOnDifferentValues: true, +} + export const PictureItemSkeleton = (