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

Typescript: Improve @storybook/ui types #9820

Merged
merged 26 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2f38e8e
CLEANUP splitting into multiple files
ndelangen Feb 10, 2020
5bfcbf2
REFACTOR preview
ndelangen Feb 10, 2020
867a662
REFACTOR preview
ndelangen Feb 11, 2020
07ca09f
REFACTOR fakeProvider
ndelangen Feb 11, 2020
47fbee9
REFACTOR mockdate && MIGRATE stories to CSF
ndelangen Feb 11, 2020
5e04834
REFACTOR preview to use hooks and FunctionComponent
ndelangen Feb 11, 2020
59a01cd
MIGRATE stories to TS
ndelangen Feb 11, 2020
9dcf5f2
MIGRATE more stories to TS
ndelangen Feb 11, 2020
3325f48
RENAME Nav to Sidebar
ndelangen Feb 11, 2020
b7a57a2
MIGRATE more stories to TS && FIX linting
ndelangen Feb 11, 2020
1a3b481
IMPROVE types
ndelangen Feb 11, 2020
6b5ff27
IMPROVE types
ndelangen Feb 11, 2020
bd560a1
Merge branch 'next' into core/ui-cleanup
ndelangen Feb 11, 2020
6e7c182
Merge branch 'core/ui-cleanup' into core/ui-types-improvements
ndelangen Feb 11, 2020
db15015
FIX missing testresults
ndelangen Feb 11, 2020
bca6253
ADD back to gitignore
ndelangen Feb 11, 2020
661536a
Merge branch 'core/ui-types-improvements' into core/ui-cleanup
ndelangen Feb 12, 2020
e2565d7
FIX tests
ndelangen Feb 12, 2020
26b5640
FIX tests && CLEANUP
ndelangen Feb 12, 2020
57b7ace
Merge branch 'next' into core/ui-cleanup
ndelangen Feb 13, 2020
5ddd46e
CLEANUP
ndelangen Feb 13, 2020
f1da8aa
CLEANUP
ndelangen Feb 13, 2020
de5e8a9
ADD exports to lib/api for StoriesHash & Item types & typecheck-funct…
ndelangen Feb 13, 2020
cab7552
CHANGE usage of State.storiesHash to StoriesHash & import other types…
ndelangen Feb 13, 2020
bd37f1c
CLEANUP
ndelangen Feb 13, 2020
f079a57
CLEANUP
ndelangen Feb 13, 2020
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
2 changes: 1 addition & 1 deletion lib/api/src/lib/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface Story {
name: string;
kind: string;
children?: StoryId[];
isComponent: false;
isComponent: boolean;
isRoot: false;
isLeaf: true;
parameters?: {
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/src/components/sidebar/treeview/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function isStory(item: Item): item is Story {
export const get = memoize(1000)((id: string, dataset: Dataset) => dataset[id]);
export const getParent = memoize(1000)((id: string, dataset: Dataset) => {
const item = get(id, dataset);
if (!isRoot(item)) {
if (item && !isRoot(item)) {
return get(item.parent, dataset);
}
return undefined;
Expand Down
97 changes: 0 additions & 97 deletions lib/ui/src/containers/nav.test.js

This file was deleted.

206 changes: 206 additions & 0 deletions lib/ui/src/containers/sidebar.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import { State } from '@storybook/api';
import { collapseDocsOnlyStories, collapseAllStories } from './sidebar';

type StoriesHash = State['storiesHash'];
type Item = StoriesHash[keyof StoriesHash];

const docsOnly = { parameters: { docsOnly: true } };
const root: Item = {
id: 'root',
name: 'root',
depth: 0,
children: ['a', 'b'],
isRoot: true,
isComponent: false,
isLeaf: false,
};
const a: Item = {
id: 'a',
name: 'a',
depth: 1,
isComponent: true,
isRoot: false,
isLeaf: false,
parent: 'root',
children: ['a1'],
};
const a1: Item = {
id: 'a1',
name: 'a1',
kind: 'a',
depth: 2,
isLeaf: true,
isComponent: false,
isRoot: false,
parent: 'a',
};
const b: Item = {
id: 'b',
name: 'b',
depth: 1,
isRoot: false,
isComponent: true,
isLeaf: false,
parent: 'root',
children: ['b1', 'b2'],
};
const b1: Item = {
id: 'b1',
name: 'b1',
kind: 'b',
depth: 2,
isLeaf: true,
isRoot: false,
isComponent: false,
parent: 'b',
};
const b2: Item = {
id: 'b2',
name: 'b2',
kind: 'b',
depth: 2,
isLeaf: true,
isRoot: false,
isComponent: false,
parent: 'b',
};

const stories: StoriesHash = { root, a, a1, b, b1, b2 };

// this seems to work as intended
describe('collapse docs-only stories', () => {
it('ignores normal stories', () => {
const filtered = collapseDocsOnlyStories(stories);
expect(filtered).toEqual(stories);
});

it('filters out docs-only stories', () => {
const hasDocsOnly: StoriesHash = {
...stories,
a1: { ...a1, parameters: { ...a1.parameters, ...docsOnly.parameters } },
};
const filtered = collapseDocsOnlyStories(hasDocsOnly);

expect(filtered.root.children).not.toContain(hasDocsOnly.a.id);
expect(filtered.root.children).toContain(hasDocsOnly.a1.id);
expect(filtered.root.children).toContain(hasDocsOnly.b.id);

expect(filtered.a).not.toBeDefined();
});
});

describe('collapse all stories', () => {
it('collapses normal stories', () => {
const collapsed = collapseAllStories(stories);

const expected: StoriesHash = {
a1: {
id: 'a1',
depth: 1,
name: 'a',
kind: 'a',
parent: 'root',
children: [],
isRoot: false,
isComponent: true,
isLeaf: true,
},
b1: {
id: 'b1',
depth: 1,
name: 'b',
kind: 'b',
parent: 'root',
children: [],
isRoot: false,
isComponent: true,
isLeaf: true,
},
root: {
id: 'root',
name: 'root',
depth: 0,
children: ['a1', 'b1'],
isRoot: true,
isComponent: false,
isLeaf: false,
},
};

expect(collapsed).toEqual(expected);
});

it('collapses docs-only stories', () => {
const hasDocsOnly: StoriesHash = {
...stories,
a1: { ...a1, parameters: { ...a1.parameters, ...docsOnly.parameters } },
};

const collapsed = collapseAllStories(hasDocsOnly);

expect(collapsed.a1).toEqual({
id: 'a1',
name: 'a',
kind: 'a',
depth: 1,
isComponent: true,
isLeaf: true,
isRoot: false,
parent: 'root',
children: [],
});
});

it('collapses mixtures of leaf and non-leaf children', () => {
const mixedRoot: Item = {
id: 'root',
name: 'root',
depth: 0,
isRoot: true,
isComponent: false,
isLeaf: false,
children: ['a', 'b1'],
};

const mixed: StoriesHash = {
root: mixedRoot,
a,
a1,
b1: { ...b1, depth: 1, parent: 'root' },
};
const collapsed = collapseAllStories(mixed);

expect(collapsed).toEqual({
a1: {
id: 'a1',
depth: 1,
name: 'a',
kind: 'a',
isRoot: false,
isComponent: true,
isLeaf: true,
parent: 'root',
children: [],
},
b1: {
id: 'b1',
name: 'b1',
kind: 'b',
depth: 1,
isLeaf: true,
isComponent: false,
isRoot: false,
parent: 'root',
},
root: {
id: 'root',
name: 'root',
depth: 0,
children: ['a1', 'b1'],
isRoot: true,
isComponent: false,
isLeaf: false,
},
});
});
});
12 changes: 10 additions & 2 deletions lib/ui/src/containers/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import memoize from 'memoizerific';
import { Badge } from '@storybook/components';
import { Consumer, Combo, State } from '@storybook/api';

import { Group, Story } from '@storybook/api/dist/lib/stories';
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
import { shortcutToHumanString } from '../libs/shortcut';

import ListItemIcon from '../components/sidebar/ListItemIcon';
Expand Down Expand Up @@ -117,11 +118,13 @@ export const collapseAllStories = (stories: StoriesHash) => {
const componentIdToLeafId: Record<string, string> = {};

// 1) remove all leaves
const leavesRemoved = Object.values(stories).filter(item => isStory(item));
const leavesRemoved = Object.values(stories).filter(
item => !(item.isLeaf && stories[item.parent].isComponent)
);

// 2) make all components leaves and rewrite their ID's to the first leaf child
const componentsFlattened = leavesRemoved.map(item => {
const { id, isComponent, children, ...rest } = item;
const { id, isComponent, isRoot, children, ...rest } = item;

// this is a folder, so just leave it alone
if (!isComponent) {
Expand All @@ -140,6 +143,8 @@ export const collapseAllStories = (stories: StoriesHash) => {
const component = {
...rest,
id: leafId,
kind: (stories[leafId] as Story).kind,
isRoot: false,
isLeaf: true,
isComponent: true,
children: [] as string[],
Expand Down Expand Up @@ -185,6 +190,9 @@ export const collapseDocsOnlyStories = (storiesHash: StoriesHash) => {
}
return true;
});

// console.dir(docsOnlyStoriesRemoved, { depth: 10 });
ndelangen marked this conversation as resolved.
Show resolved Hide resolved

const docsOnlyComponentsCollapsed = docsOnlyStoriesRemoved.map(item => {
// collapse docs-only components
const { isComponent, children, id } = item;
Expand Down