- 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>
33 lines
1.1 KiB
TypeScript
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)
|
|
},
|
|
[],
|
|
)
|
|
}
|