From 73312031835854e33a876e1f0ae3701e2a02818f Mon Sep 17 00:00:00 2001 From: Anna Commons Date: Mon, 2 May 2022 14:43:57 -0700 Subject: [PATCH 1/6] fix typing on useArgs --- lib/addons/src/hooks.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/addons/src/hooks.ts b/lib/addons/src/hooks.ts index dd329046ca4e..b40f987d1ab1 100644 --- a/lib/addons/src/hooks.ts +++ b/lib/addons/src/hooks.ts @@ -424,17 +424,17 @@ export function useParameter(parameterKey: string, defaultValue?: S): S | und } /* Returns current value of story args */ -export function useArgs(): [Args, (newArgs: Args) => void, (argNames?: [string]) => void] { +export function useArgs

(): [P, (newArgs: Partial

) => void, (argNames?: string[]) => void] { const channel = addons.getChannel(); const { id: storyId, args } = useStoryContext(); const updateArgs = useCallback( - (updatedArgs: Args) => channel.emit(UPDATE_STORY_ARGS, { storyId, updatedArgs }), + (updatedArgs: Partial

) => channel.emit(UPDATE_STORY_ARGS, { storyId, updatedArgs }), [channel, storyId] ); const resetArgs = useCallback( - (argNames?: [string]) => channel.emit(RESET_STORY_ARGS, { storyId, argNames }), + (argNames?: string[]) => channel.emit(RESET_STORY_ARGS, { storyId, argNames }), [channel, storyId] ); From a1e55bbe1e36df4d78efa1f73a8adb23d0e06637 Mon Sep 17 00:00:00 2001 From: Anna Commons Date: Wed, 18 May 2022 19:00:38 -0700 Subject: [PATCH 2/6] fix build error and type things correctly --- lib/addons/src/hooks.ts | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/addons/src/hooks.ts b/lib/addons/src/hooks.ts index b40f987d1ab1..9af373f8a640 100644 --- a/lib/addons/src/hooks.ts +++ b/lib/addons/src/hooks.ts @@ -33,7 +33,7 @@ interface Effect { type AbstractFunction = (...args: any[]) => any; -export class HooksContext { +export class HooksContext { hookListsMap: WeakMap; mountedDecorators: Set; @@ -54,7 +54,7 @@ export class HooksContext { hasUpdates: boolean; - currentContext: StoryContext | null; + currentContext: StoryContext | null; renderListener = (storyId: StoryId) => { if (storyId !== this.currentContext.id) return; @@ -216,12 +216,18 @@ const areDepsEqual = (deps: any[], nextDeps: any[]) => const invalidHooksError = () => new Error('Storybook preview hooks can only be called inside decorators and story functions.'); -function getHooksContextOrNull(): HooksContext | null { +function getHooksContextOrNull(): HooksContext< + TFramework, + TArgs +> | null { return globalWindow.STORYBOOK_HOOKS_CONTEXT || null; } -function getHooksContextOrThrow(): HooksContext { - const hooks = getHooksContextOrNull(); +function getHooksContextOrThrow(): HooksContext< + TFramework, + TArgs +> { + const hooks = getHooksContextOrNull(); if (hooks == null) { throw invalidHooksError(); } @@ -405,8 +411,11 @@ export function useChannel(eventMap: EventMap, deps: any[] = []) { } /* Returns current story context */ -export function useStoryContext(): StoryContext { - const { currentContext } = getHooksContextOrThrow(); +export function useStoryContext(): StoryContext< + TFramework, + TArgs +> { + const { currentContext } = getHooksContextOrThrow(); if (currentContext == null) { throw invalidHooksError(); } @@ -424,12 +433,16 @@ export function useParameter(parameterKey: string, defaultValue?: S): S | und } /* Returns current value of story args */ -export function useArgs

(): [P, (newArgs: Partial

) => void, (argNames?: string[]) => void] { +export function useArgs(): [ + TArgs, + (newArgs: Partial) => void, + (argNames?: string[]) => void +] { const channel = addons.getChannel(); - const { id: storyId, args } = useStoryContext(); + const { id: storyId, args } = useStoryContext(); const updateArgs = useCallback( - (updatedArgs: Partial

) => channel.emit(UPDATE_STORY_ARGS, { storyId, updatedArgs }), + (updatedArgs: Partial) => channel.emit(UPDATE_STORY_ARGS, { storyId, updatedArgs }), [channel, storyId] ); From 081e3e191b415c30e3af137bfe6d083687b11881 Mon Sep 17 00:00:00 2001 From: Anna Commons Date: Wed, 18 May 2022 19:24:36 -0700 Subject: [PATCH 3/6] switch from string[] to (keyof TArgs)[] --- lib/addons/src/hooks.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/addons/src/hooks.ts b/lib/addons/src/hooks.ts index 9af373f8a640..8ddc988804b0 100644 --- a/lib/addons/src/hooks.ts +++ b/lib/addons/src/hooks.ts @@ -436,7 +436,7 @@ export function useParameter(parameterKey: string, defaultValue?: S): S | und export function useArgs(): [ TArgs, (newArgs: Partial) => void, - (argNames?: string[]) => void + (argNames?: (keyof TArgs)[]) => void ] { const channel = addons.getChannel(); const { id: storyId, args } = useStoryContext(); @@ -447,7 +447,7 @@ export function useArgs(): [ ); const resetArgs = useCallback( - (argNames?: string[]) => channel.emit(RESET_STORY_ARGS, { storyId, argNames }), + (argNames?: (keyof TArgs)[]) => channel.emit(RESET_STORY_ARGS, { storyId, argNames }), [channel, storyId] ); From fbba1b94c1ab7d2e9715b6414747287700c3009a Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Fri, 13 Jan 2023 12:03:59 +0100 Subject: [PATCH 4/6] Update lib/addons/src/hooks.ts Co-authored-by: Michael Shilman --- lib/addons/src/hooks.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/addons/src/hooks.ts b/lib/addons/src/hooks.ts index 8ddc988804b0..3cfea7c3f981 100644 --- a/lib/addons/src/hooks.ts +++ b/lib/addons/src/hooks.ts @@ -433,7 +433,7 @@ export function useParameter(parameterKey: string, defaultValue?: S): S | und } /* Returns current value of story args */ -export function useArgs(): [ +export function useArgs(): [ TArgs, (newArgs: Partial) => void, (argNames?: (keyof TArgs)[]) => void From 6568153a09e642f4f89db5a3faac1e3e40a1b652 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Fri, 13 Jan 2023 12:41:36 +0100 Subject: [PATCH 5/6] Update code/lib/preview-api/src/modules/addons/hooks.ts --- code/lib/preview-api/src/modules/addons/hooks.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/lib/preview-api/src/modules/addons/hooks.ts b/code/lib/preview-api/src/modules/addons/hooks.ts index 10a43b0e59ed..a65e18b9eb7e 100644 --- a/code/lib/preview-api/src/modules/addons/hooks.ts +++ b/code/lib/preview-api/src/modules/addons/hooks.ts @@ -31,7 +31,7 @@ interface Effect { type AbstractFunction = (...args: any[]) => any; -export class HooksContext { +export class HooksContext { hookListsMap: WeakMap = undefined as any; mountedDecorators: Set = undefined as any; From 66271605779f8cf5731de7c6a308829477677c40 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Fri, 13 Jan 2023 12:56:55 +0100 Subject: [PATCH 6/6] fix --- code/lib/preview-api/src/modules/addons/hooks.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/code/lib/preview-api/src/modules/addons/hooks.ts b/code/lib/preview-api/src/modules/addons/hooks.ts index a65e18b9eb7e..c5f5acb3eec3 100644 --- a/code/lib/preview-api/src/modules/addons/hooks.ts +++ b/code/lib/preview-api/src/modules/addons/hooks.ts @@ -216,17 +216,17 @@ const areDepsEqual = (deps: any[], nextDeps: any[]) => const invalidHooksError = () => new Error('Storybook preview hooks can only be called inside decorators and story functions.'); -function getHooksContextOrNull(): HooksContext< - TRenderer, - TArgs -> | null { +function getHooksContextOrNull< + TRenderer extends Renderer, + TArgs extends Args = Args +>(): HooksContext | null { return global.STORYBOOK_HOOKS_CONTEXT || null; } -function getHooksContextOrThrow(): HooksContext< - TRenderer, - TArgs -> { +function getHooksContextOrThrow< + TRenderer extends Renderer, + TArgs extends Args = Args +>(): HooksContext { const hooks = getHooksContextOrNull(); if (hooks == null) { throw invalidHooksError();