forked from RSSNext/Follow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify useRegisterCommand types (RSSNext#1678)
- Loading branch information
Showing
9 changed files
with
187 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
apps/renderer/src/modules/command/hooks/use-register-command-effect.ts
This file was deleted.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
apps/renderer/src/modules/command/hooks/use-register-command.test-d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { assertType, expectTypeOf, test } from "vitest" | ||
|
||
import { COMMAND_ID } from "../commands/id" | ||
import { useRegisterFollowCommand } from "./use-register-command" | ||
|
||
test("useRegisterFollowCommand types", () => { | ||
assertType( | ||
useRegisterFollowCommand({ | ||
id: COMMAND_ID.entry.openInBrowser, | ||
label: "", | ||
run: ({ entryId }) => { | ||
expectTypeOf(entryId).toEqualTypeOf<string>() | ||
}, | ||
}), | ||
) | ||
|
||
assertType( | ||
useRegisterFollowCommand({ | ||
id: "unknown id", | ||
label: "", | ||
run: (...args) => { | ||
expectTypeOf(args).toEqualTypeOf<[]>() | ||
}, | ||
}), | ||
) | ||
|
||
assertType( | ||
useRegisterFollowCommand([ | ||
{ | ||
id: COMMAND_ID.entry.star, | ||
label: "", | ||
run: ({ entryId }) => { | ||
expectTypeOf(entryId).toEqualTypeOf<string>() | ||
}, | ||
}, | ||
{ | ||
id: COMMAND_ID.entry.viewEntryContent, | ||
label: "", | ||
run: (...args) => { | ||
expectTypeOf(args).toEqualTypeOf<[]>() | ||
}, | ||
}, | ||
]), | ||
) | ||
|
||
assertType( | ||
useRegisterFollowCommand([ | ||
{ | ||
id: "unknown id", | ||
label: "", | ||
run: (...args) => { | ||
expectTypeOf(args).toEqualTypeOf<[]>() | ||
}, | ||
}, | ||
]), | ||
) | ||
|
||
assertType( | ||
useRegisterFollowCommand([ | ||
{ | ||
id: "unknown id", | ||
label: "", | ||
run: (...args) => { | ||
expectTypeOf(args).toEqualTypeOf<[]>() | ||
}, | ||
}, | ||
{ | ||
id: COMMAND_ID.entry.star, | ||
label: "", | ||
run: ({ entryId }) => { | ||
expectTypeOf(entryId).toEqualTypeOf<string>() | ||
}, | ||
}, | ||
{ | ||
id: COMMAND_ID.entry.viewEntryContent, | ||
label: "", | ||
run: (...args) => { | ||
expectTypeOf(args).toEqualTypeOf<[]>() | ||
}, | ||
}, | ||
]), | ||
) | ||
}) |
66 changes: 66 additions & 0 deletions
66
apps/renderer/src/modules/command/hooks/use-register-command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useEffect } from "react" | ||
import { useTranslation } from "react-i18next" | ||
|
||
import { registerCommand } from "../registry/registry" | ||
import type { CommandOptions, FollowCommandId, FollowCommandMap } from "../types" | ||
|
||
export type RegisterOptions = { | ||
deps?: unknown[] | ||
enabled?: boolean | ||
// forceMountSection?: boolean | ||
// sectionMeta?: Record<string, unknown> | ||
// orderSection?: OrderSectionInstruction | ||
// orderCommands?: OrderCommandsInstruction | ||
} | ||
|
||
export const useRegisterCommandEffect = ( | ||
options: CommandOptions | CommandOptions[], | ||
registerOptions?: RegisterOptions, | ||
) => { | ||
const { t } = useTranslation() | ||
useEffect(() => { | ||
if (!Array.isArray(options)) { | ||
return registerCommand(options) | ||
} | ||
|
||
const unsubscribes = options.map((option) => registerCommand(option)) | ||
return () => { | ||
unsubscribes.forEach((unsubscribe) => unsubscribe()) | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps -- we want to run this effect only once | ||
}, [t, ...(registerOptions?.deps ?? [])]) | ||
} | ||
|
||
/** | ||
* Register a follow command. | ||
*/ | ||
export function useRegisterFollowCommand<T extends FollowCommandId>( | ||
options: CommandOptions<{ id: T; fn: FollowCommandMap[T]["run"] }>, | ||
registerOptions?: RegisterOptions, | ||
): void | ||
/** | ||
* Register a unknown command. | ||
*/ | ||
export function useRegisterFollowCommand<T extends string>( | ||
options: CommandOptions<{ id: T; fn: () => void }>, | ||
registerOptions?: RegisterOptions, | ||
): void | ||
/** | ||
* Register multiple follow commands or unknown commands. | ||
*/ | ||
export function useRegisterFollowCommand<T extends (FollowCommandId | string)[]>( | ||
options: [ | ||
...{ | ||
[K in keyof T]: T[K] extends FollowCommandId | ||
? CommandOptions<{ id: T[K]; fn: FollowCommandMap[T[K]]["run"] }> | ||
: CommandOptions<{ id: T[K]; fn: () => void }> | ||
}, | ||
], | ||
registerOptions?: RegisterOptions, | ||
): void | ||
export function useRegisterFollowCommand( | ||
options: CommandOptions | CommandOptions[], | ||
registerOptions?: RegisterOptions, | ||
) { | ||
return useRegisterCommandEffect(options as CommandOptions | CommandOptions[], registerOptions) | ||
} |
Oops, something went wrong.