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

TS improvements #9879

Merged
merged 1 commit into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions lib/addons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@storybook/channels": "6.0.0-alpha.13",
"@storybook/client-logger": "6.0.0-alpha.13",
"@storybook/core-events": "6.0.0-alpha.13",
"@storybook/router": "6.0.0-alpha.13",
"core-js": "^3.0.1",
"global": "^4.3.2",
"util-deprecate": "^1.0.2"
Expand Down
21 changes: 3 additions & 18 deletions lib/addons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,21 @@ import global from 'global';
import { ReactElement } from 'react';
import { Channel } from '@storybook/channels';
import { API } from '@storybook/api';
import { RenderData as RouterData } from '@storybook/router';
import { logger } from '@storybook/client-logger';
// eslint-disable-next-line import/no-extraneous-dependencies
import { WindowLocation } from '@reach/router';
import { types, Types } from './types';

export type ViewMode = 'story' | 'info' | 'settings' | undefined | 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;
route?: (routeOptions: RouterData) => string;
match?: (matchOptions: RouterData) => boolean;
render: (renderOptions: RenderOptions) => ReactElement<any>;
paramKey?: string;
disabled?: boolean;
Expand Down
7 changes: 6 additions & 1 deletion lib/api/src/init-provider-api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ReactNode } from 'react';
import { Channel } from '@storybook/channels';
import { ThemeVars } from '@storybook/theming';

import { API, State } from './index';
import Store from './store';
import { UIOptions } from './modules/layout';

type IframeRenderer = (
storyId: string,
Expand All @@ -17,7 +19,10 @@ export interface Provider {
channel?: Channel;
renderPreview?: IframeRenderer;
handleAPI(api: API): void;
getConfig(): Record<string, any>;
getConfig(): {
theme?: ThemeVars;
[k: string]: any;
} & Partial<UIOptions>;
[key: string]: any;
}

Expand Down
12 changes: 10 additions & 2 deletions lib/api/src/lib/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export interface Root {
isRoot: true;
isLeaf: false;
// MDX stories are "Group" type
parameters?: any;
parameters?: {
docsOnly?: boolean;
[k: string]: any;
};
}

export interface Group {
Expand All @@ -27,7 +30,10 @@ export interface Group {
isRoot: false;
isLeaf: false;
// MDX stories are "Group" type
parameters?: any;
parameters?: {
docsOnly?: boolean;
[k: string]: any;
};
}

export interface Story {
Expand All @@ -48,6 +54,7 @@ export interface Story {
showRoots?: boolean;
[k: string]: any;
};
docsOnly?: boolean;
[k: string]: any;
};
}
Expand All @@ -65,6 +72,7 @@ export interface StoryInput {
showRoots?: boolean;
[key: string]: any;
};
docsOnly?: boolean;
[parameterName: string]: any;
};
isLeaf: boolean;
Expand Down
28 changes: 15 additions & 13 deletions lib/api/src/modules/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ export interface SubAPI {
type PartialSubState = Partial<SubState>;
type PartialThemeVars = Partial<ThemeVars>;
type PartialLayout = Partial<Layout>;
type PartialUI = Partial<UI>;

interface Options extends ThemeVars {
export interface UIOptions {
name?: string;
url?: string;
goFullScreen: boolean;
Expand Down Expand Up @@ -96,16 +95,19 @@ const deprecationMessage = (optionsMap: OptionsMap, prefix = '') =>
prefix ? `${prefix}'s` : ''
} { ${Object.values(optionsMap).join(', ')} } instead.`;

const applyDeprecatedThemeOptions = deprecate(({ name, url, theme }: Options): PartialThemeVars => {
const { brandTitle, brandUrl, brandImage }: PartialThemeVars = theme || {};
return {
brandTitle: brandTitle || name,
brandUrl: brandUrl || url,
brandImage: brandImage || null,
};
}, deprecationMessage(deprecatedThemeOptions));
const applyDeprecatedThemeOptions = deprecate(
({ name, url, theme }: UIOptions): PartialThemeVars => {
const { brandTitle, brandUrl, brandImage }: PartialThemeVars = theme || {};
return {
brandTitle: brandTitle || name,
brandUrl: brandUrl || url,
brandImage: brandImage || null,
};
},
deprecationMessage(deprecatedThemeOptions)
);

const applyDeprecatedLayoutOptions = deprecate((options: Partial<Options>): PartialLayout => {
const applyDeprecatedLayoutOptions = deprecate((options: Partial<UIOptions>): PartialLayout => {
const layoutUpdate: PartialLayout = {};

['goFullScreen', 'showStoriesPanel', 'showAddonPanel'].forEach(
Expand All @@ -123,14 +125,14 @@ const applyDeprecatedLayoutOptions = deprecate((options: Partial<Options>): Part
return layoutUpdate;
}, deprecationMessage(deprecatedLayoutOptions));

const checkDeprecatedThemeOptions = (options: Options) => {
const checkDeprecatedThemeOptions = (options: UIOptions) => {
if (Object.keys(deprecatedThemeOptions).find(v => v in options)) {
return applyDeprecatedThemeOptions(options);
}
return {};
};

const checkDeprecatedLayoutOptions = (options: Partial<Options>) => {
const checkDeprecatedLayoutOptions = (options: Partial<UIOptions>) => {
if (Object.keys(deprecatedLayoutOptions).find(v => v in options)) {
return applyDeprecatedLayoutOptions(options);
}
Expand Down
10 changes: 7 additions & 3 deletions lib/router/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ import {
navigate,
LocationProvider,
RouteComponentProps,
LocationContext,
NavigateFn,
} from '@reach/router';
import { ToggleVisibility } from './visibility';
import { queryFromString, parsePath, getMatch } from './utils';

interface Other {
viewMode?: string;
storyId?: string;
viewMode: string;
storyId: string;
path: string;
}

export type RenderData = RouteComponentProps & Other;
export type RenderData = Pick<LocationContext, 'location'> &
Partial<Pick<LocationContext, 'navigate'>> &
Other;

interface MatchingData {
match: null | { path: string };
Expand Down