-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
addons.ts
150 lines (131 loc) · 3.72 KB
/
addons.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { ReactElement } from 'react';
import { WindowLocation } from '@reach/router';
import { Module } from '../index';
import { Options } from '../store';
export type ViewMode = 'story' | 'info' | 'settings' | undefined | string;
export enum types {
TAB = 'tab',
PANEL = 'panel',
TOOL = 'tool',
PREVIEW = 'preview',
NOTES_ELEMENT = 'notes-element',
}
export type Types = types | string;
export interface RenderOptions {
active: boolean;
key: string;
}
export interface RouteOptions {
storyId: string;
viewMode: ViewMode;
location: WindowLocation;
path: string;
}
export interface MatchOptions {
storyId: string;
viewMode: ViewMode;
location: WindowLocation;
path: string;
}
export interface Addon {
title: string;
type?: Types;
id?: string;
route?: (routeOptions: RouteOptions) => string;
match?: (matchOptions: MatchOptions) => boolean;
render: (renderOptions: RenderOptions) => ReactElement<any>;
paramKey?: string;
disabled?: boolean;
hidden?: boolean;
}
export interface Collection<T = Addon> {
[key: string]: T;
}
type Panels = Collection<Addon>;
type StateMerger<S> = (input: S) => S;
interface StoryInput {
parameters: {
[parameterName: string]: any;
};
}
export interface SubAPI {
getElements: <T>(type: Types) => Collection<T>;
getPanels: () => Panels;
getStoryPanels: () => Panels;
getSelectedPanel: () => string;
setSelectedPanel: (panelName: string) => void;
setAddonState<S>(
addonId: string,
newStateOrMerger: S | StateMerger<S>,
options?: Options
): Promise<S>;
getAddonState<S>(addonId: string): S;
}
export function ensurePanel(panels: Panels, selectedPanel?: string, currentPanel?: string) {
const keys = Object.keys(panels);
if (keys.indexOf(selectedPanel) >= 0) {
return selectedPanel;
}
if (keys.length) {
return keys[0];
}
return currentPanel;
}
export default ({ provider, store, fullAPI }: Module) => {
const api: SubAPI = {
getElements: type => provider.getElements(type),
getPanels: () => api.getElements(types.PANEL),
getStoryPanels: () => {
const allPanels = api.getPanels();
const { storyId } = store.getState();
const story = fullAPI.getData(storyId);
if (!allPanels || !story) {
return allPanels;
}
const { parameters } = story;
const filteredPanels: Collection = {};
Object.entries(allPanels).forEach(([id, panel]) => {
const { paramKey } = panel;
if (paramKey && parameters && parameters[paramKey] && parameters[paramKey].disabled) {
return;
}
filteredPanels[id] = panel;
});
return filteredPanels;
},
getSelectedPanel: () => {
const { selectedPanel } = store.getState();
return ensurePanel(api.getPanels(), selectedPanel, selectedPanel);
},
setSelectedPanel: panelName => {
store.setState({ selectedPanel: panelName }, { persistence: 'session' });
},
setAddonState<S>(
addonId: string,
newStateOrMerger: S | StateMerger<S>,
options?: Options
): Promise<S> {
let nextState;
const { addons: existing } = store.getState();
if (typeof newStateOrMerger === 'function') {
const merger = newStateOrMerger as StateMerger<S>;
nextState = merger(api.getAddonState<S>(addonId));
} else {
nextState = newStateOrMerger;
}
return store
.setState({ addons: { ...existing, [addonId]: nextState } }, options)
.then(() => api.getAddonState(addonId));
},
getAddonState: addonId => {
return store.getState().addons[addonId];
},
};
return {
api,
state: {
selectedPanel: ensurePanel(api.getPanels(), store.getState().selectedPanel),
addons: {},
},
};
};