Files
Folo/apps/renderer/src/modules/command/hooks/use-command.ts
T
Innei d0bf0fb657 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>
2024-12-17 00:32:28 +08:00

33 lines
1.1 KiB
TypeScript

import { jotaiStore } from "@follow/utils/jotai"
import { useAtomValue } from "jotai"
import { selectAtom } from "jotai/utils"
import { useCallback, useMemo } from "react"
import { CommandRegistry } from "../registry/registry"
import type { FollowCommandId, FollowCommandMap } from "../types"
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 commands = useAtomValue(
useMemo(() => selectAtom(CommandRegistry.atom, (commands) => commands[id]), [id]),
)
return commands as FollowCommandMap[T] | null
}
const noop = () => {}
export function useRunCommandFn() {
return useCallback(
<T extends FollowCommandId>(id: T, args: Parameters<FollowCommandMap[T]["run"]>) => {
const cmd = getCommand(id)
if (!cmd) return noop
// @ts-expect-error - The type should be discriminated
return () => cmd.run(...args)
},
[],
)
}