Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manager API: Fix api.getAddonStatedefault value #23804

Merged
merged 4 commits into from
Aug 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions code/lib/manager-api/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ const addonStateCache: {
} = {};

// shared state
export function useSharedState<S>(stateId: string, defaultState?: S) {
export function useSharedState<S extends object>(stateId: string, defaultState?: S) {
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
const api = useStorybookApi();
const existingState = api.getAddonState<S>(stateId) || addonStateCache[stateId];
const state = orDefault<S>(
Expand All @@ -431,7 +431,13 @@ export function useSharedState<S>(stateId: string, defaultState?: S) {
}

const setState = (s: S | API_StateMerger<S>, options?: Options) => {
const result = api.setAddonState<S>(stateId, s, options);
let sFinal = s;
if (typeof s === 'function') {
sFinal = (_state) => {
return s(_state || defaultState);
};
}
const result = api.setAddonState<S>(stateId, sFinal, options);
addonStateCache[stateId] = result;
return result;
};
Expand Down Expand Up @@ -483,7 +489,7 @@ export function useSharedState<S>(stateId: string, defaultState?: S) {
] as [S, (newStateOrMerger: S | API_StateMerger<S>, options?: Options) => void];
}

export function useAddonState<S>(addonId: string, defaultState?: S) {
export function useAddonState<S extends object>(addonId: string, defaultState?: S) {
return useSharedState<S>(addonId, defaultState);
}

Expand Down