feat: add support for initialSelection param

This commit is contained in:
Estevão Lucas 2018-11-06 23:45:01 -05:00
parent a8497064ca
commit 4808646ca1
2 changed files with 27 additions and 6 deletions

View File

@ -121,8 +121,11 @@ You can pass these parameters to getStorybookUI call in your storybook entry poi
-- should the ui be closed initialy.
tabOpen: Number (0)
-- which tab should be open. -1 Navigator, 0 Preview, 1 Addons
initialSelection: Object (null)
-- initialize storybook with a specific story. In case a valid object is passed, it will take precedence over `shouldPersistSelection. ex: `{ kind: 'Knobs', story: 'with knobs' }`
shouldPersistSelection: Boolean (true)
-- initialize storybook with the last selected story.
-- initialize storybook with the last selected story.`
)
}
```

View File

@ -67,7 +67,7 @@ export default class Preview {
const port = params.port !== false ? `:${params.port || 7007}` : '';
const query = params.query || '';
const { secured, shouldPersistSelection } = params;
const { initialSelection, secured, shouldPersistSelection } = params;
const websocketType = secured ? 'wss' : 'ws';
const httpType = secured ? 'https' : 'http';
@ -77,7 +77,7 @@ export default class Preview {
url,
async: onDeviceUI,
onError: () => {
this._setInitialStory(shouldPersistSelection);
this._setInitialStory(initialSelection, shouldPersistSelection);
setInitialStory = true;
},
@ -135,14 +135,16 @@ export default class Preview {
channel.emit(Events.GET_CURRENT_STORY);
}
_setInitialStory = async (shouldPersistSelection = true) => {
_setInitialStory = async (initialSelection, shouldPersistSelection = true) => {
let story = this._getInitialStory();
if (shouldPersistSelection) {
if (initialSelection && this._checkStory(initialSelection)) {
story = initialSelection;
} else if (shouldPersistSelection) {
const value = await AsyncStorage.getItem(STORAGE_KEY);
const previousStory = JSON.parse(value);
if (typeof previousStory === 'object' && previousStory.story) {
if (this._checkStory(previousStory)) {
story = previousStory;
}
}
@ -175,4 +177,20 @@ export default class Preview {
channel.emit(Events.SELECT_STORY, this._getStory(selection));
AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(selection));
}
_checkStory(selection) {
if (!selection || typeof selection !== 'object' || !selection.kind || !selection.story) {
console.warn('invalid storybook selection'); // eslint-disable-line no-console
return null;
}
const story = this._getStory(selection);
if (story.storyFn === null) {
console.warn('invalid storybook selection'); // eslint-disable-line no-console
return null;
}
return story;
}
}