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
@@ -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],
[],
)
}