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:
@@ -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,
|
||||
() => {
|
||||
|
||||
Reference in New Issue
Block a user