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 <tukon479@gmail.com>
This commit is contained in:
Innei
2024-12-17 00:32:28 +08:00
parent edc9e3008e
commit d0bf0fb657
5 changed files with 42 additions and 31 deletions
@@ -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,
@@ -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<TipCommand | null>()
test("getCommand types work properly", () => {
expectTypeOf(getCommand(COMMAND_ID.entry.tip)).toMatchTypeOf<TipCommand | null>()
// @ts-expect-error - get an unknown command should throw an error
assertType(getCmd("unknown command"))
@@ -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 <T extends FollowCommandId>(id: T): FollowCommandMap[T] | null =>
id in commands ? commands[id] : null
export const getCommand = <T extends FollowCommandId>(id: T) => {
const commands = jotaiStore.get(CommandRegistry.atom) as FollowCommandMap
return id in commands ? commands[id] : null
}
export function useCommand<T extends FollowCommandId>(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(
<T extends FollowCommandId>(id: T, args: Parameters<FollowCommandMap[T]["run"]>) => {
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],
[],
)
}
@@ -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<T extends FollowCommandId> {
shortcut: string
@@ -17,7 +17,6 @@ export const useCommandHotkey = <T extends FollowCommandId>({
when,
args,
}: RegisterHotkeyOptions<T>) => {
const getCommand = useGetCommand()
useHotkeys(
shortcut,
() => {
@@ -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 (
<div
ref={setRef}
data-entry-id={entryId}
data-index={index}
onMouseEnter={() => setIsMouseEnter(true)}
onMouseLeave={() => setIsMouseEnter(false)}
onTouchMove={() => setIsMouseEnter(true)}
onTouchEnd={() => setIsMouseEnter(false)}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onTouchMove={handleMouseEnter}
onTouchEnd={handleMouseLeave}
className={className}
>
<EntryItemWrapper
@@ -129,9 +139,9 @@ export const PictureWaterFallItem = memo(function PictureWaterFallItem({
<AnimatePresence>
{isMouseEnter && (
<m.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: 10 }}
initial={footerExit}
animate={footerAnimate}
exit={footerExit}
className="absolute inset-x-0 -bottom-px z-[3] overflow-hidden rounded-b-md pb-1"
key="footer"
>
@@ -200,6 +210,10 @@ const MasonryItemFixedDimensionWrapper = (
)
}
MasonryItemFixedDimensionWrapper.whyDidYouRender = {
logOnDifferentValues: true,
}
export const PictureItemSkeleton = (
<div className="relative max-w-md rounded-md bg-theme-background text-zinc-700 transition-colors dark:text-neutral-400">
<div className="relative">