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

Tech/improvements extracted from composition #10040

Merged
merged 24 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d28b8d9
CHANGE api.getdata of manual destructure of state
ndelangen Mar 3, 2020
8343c37
IMPROVE typings of config
ndelangen Mar 3, 2020
0547e69
ADD fullAPI to module in lib/api
ndelangen Mar 3, 2020
f26b02f
FIX mutation of storieshash
ndelangen Mar 3, 2020
a7693be
FIX viewMode in docsMode
ndelangen Mar 3, 2020
49d0ee4
CHANGE addon module to use fullAPI.getData
ndelangen Mar 3, 2020
7691ed3
FIX test
ndelangen Mar 3, 2020
7cc1cd9
CHANGE postmessage to use native Message type over a custom one
ndelangen Mar 3, 2020
07c7b72
ADD ability for listeners to get the original message event via `this`
ndelangen Mar 3, 2020
b479efa
IMPROVE typings of SVG icon
ndelangen Mar 3, 2020
a38152b
REMOVE log statements that serve no value to the end-user
ndelangen Mar 3, 2020
dd8ed59
CHANGE to use useMemo of memoizerific
ndelangen Mar 3, 2020
ae02d61
CHANGE FUZZY_SEARCH_THRESHOLD to 0.35
ndelangen Mar 3, 2020
09fe2c3
CHANGE exactMatch to case insensitive && ADD safety to parameters check
ndelangen Mar 3, 2020
9f98813
CHANGE getDescription to properly typed & have a fallback for if ther…
ndelangen Mar 3, 2020
b22e902
REFACTOR sidebar menu construction & memoization
ndelangen Mar 3, 2020
a94ca83
CHANGE svg for dummy logo
ndelangen Mar 3, 2020
f07b5fb
REMOVE propTypes on TS files
ndelangen Mar 3, 2020
6cc9a7a
ADD white background for dev-kits example
ndelangen Mar 3, 2020
89cebd3
ADD dependency as is required
ndelangen Mar 3, 2020
31493de
ADD typings for rfdc
ndelangen Mar 3, 2020
770fbb8
FIX deepscan issues
ndelangen Mar 3, 2020
5b8a15b
Merge branch 'next' into tech/use-getdata
ndelangen Mar 3, 2020
3db537c
CHANGE exactMatch so the memoization isn't needed
ndelangen Mar 4, 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 addons/backgrounds/src/containers/BackgroundSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const getSelectedBackgroundColor = (list: Input[], currentSelectedValue: string)
};

const mapper = ({ api, state }: Combo): { items: Input[]; selected: string | null } => {
const story = state.storiesHash[state.storyId];
const story = api.getData(state.storyId);
const list = story ? api.getParameters(story.id, PARAM_KEY) : [];
const selected = state.addons[PARAM_KEY] || null;

Expand Down
15 changes: 1 addition & 14 deletions addons/events/src/components/Event.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { ChangeEvent, Component } from 'react';
import PropTypes from 'prop-types';
import { polyfill } from 'react-lifecycles-compat';
import isEqual from 'lodash/isEqual';

Expand Down Expand Up @@ -109,19 +108,7 @@ interface ItemState {
}

class Item extends Component<ItemProps, ItemState> {
static propTypes = {
name: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
onEmit: PropTypes.func.isRequired,
// eslint-disable-next-line react/forbid-prop-types
payload: PropTypes.any,
};

static defaultProps = {
payload: {},
};

static getDerivedStateFromProps = ({ payload }: ItemProps, { prevPayload }: ItemState) => {
static getDerivedStateFromProps = ({ payload = {} }: ItemProps, { prevPayload }: ItemState) => {
if (!isEqual(payload, prevPayload)) {
const payloadString = json.plain(payload);
const refinedPayload = getJSONFromString(payloadString);
Expand Down
10 changes: 0 additions & 10 deletions addons/events/src/components/Panel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';

import { styled } from '@storybook/theming';
import { API } from '@storybook/api';
Expand All @@ -24,15 +23,6 @@ interface EventsPanelState {
}

export default class EventsPanel extends Component<EventsPanelProps, EventsPanelState> {
static propTypes = {
active: PropTypes.bool.isRequired,
api: PropTypes.shape({
emit: PropTypes.func,
off: PropTypes.func,
on: PropTypes.func,
}).isRequired,
};

state: EventsPanelState = {
events: [],
};
Expand Down
4 changes: 0 additions & 4 deletions addons/graphql/src/components/FullScreen/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import React, { FunctionComponent } from 'react';
import PropTypes from 'prop-types';
import { style } from './style';

export const FullScreen: FunctionComponent = ({ children }) => {
return <div style={style.wrapper}>{children}</div>;
};

FullScreen.defaultProps = { children: null };
FullScreen.propTypes = { children: PropTypes.node };
6 changes: 1 addition & 5 deletions addons/graphql/src/manager.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { FunctionComponent } from 'react';
import PropTypes from 'prop-types';
import GraphiQL from 'graphiql';
import 'graphiql/graphiql.css';

Expand All @@ -16,7 +15,7 @@ const GQL: FunctionComponent<GQLProps> = ({ active }) => {
return active ? (
<Consumer>
{({ api, state }: Combo) => {
const story = state.storiesHash[state.storyId];
const story = api.getData(state.storyId);
const parameters = story ? api.getParameters(story.id, PARAM_KEY) : null;

if (parameters) {
Expand All @@ -32,8 +31,5 @@ const GQL: FunctionComponent<GQLProps> = ({ active }) => {
</Consumer>
) : null;
};
GQL.propTypes = {
active: PropTypes.bool.isRequired,
};

export default GQL;
24 changes: 0 additions & 24 deletions app/react/src/demo/Welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import React, {
FunctionComponent,
HTMLAttributes,
} from 'react';
import PropTypes from 'prop-types';

type MainProps = Omit<DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement>, 'style'>;
const Main: FunctionComponent<MainProps> = props => (
Expand All @@ -25,12 +24,6 @@ type TitleProps = DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHead
const Title: FunctionComponent<TitleProps> = ({ children, ...props }) => (
<h1 {...props}>{children}</h1>
);
Title.propTypes = {
children: PropTypes.node,
};
Title.defaultProps = {
children: undefined,
};

type NoteProps = Omit<
DetailedHTMLProps<HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>,
Expand Down Expand Up @@ -85,14 +78,6 @@ const Link: FunctionComponent<LinkProps> = ({ children, href, target, rel, ...pr
{children}
</a>
);
Link.propTypes = {
href: PropTypes.string,
children: PropTypes.node,
};
Link.defaultProps = {
href: undefined,
children: undefined,
};

type NavButtonProps = Omit<
DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>,
Expand Down Expand Up @@ -120,12 +105,6 @@ const NavButton: FunctionComponent<NavButtonProps> = ({ children, onClick, ...pr
{children}
</button>
);
NavButton.propTypes = {
children: PropTypes.node,
};
NavButton.defaultProps = {
children: undefined,
};

type WelcomeProps = {
showApp: () => void;
Expand Down Expand Up @@ -176,8 +155,5 @@ const Welcome: FunctionComponent<WelcomeProps> = ({ showApp }) => (
</Main>
);
Welcome.displayName = 'Welcome';
Welcome.defaultProps = {
showApp: null,
};

export { Welcome as default };
4 changes: 3 additions & 1 deletion examples/dev-kits/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/dev-kits/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ addons.setConfig({
brandImage: logo,
brandTitle: 'Custom - Storybook',
...themes.dark,
appContentBg: 'white',
},
panelPosition: 'bottom',
selectedPanel: 'storybook/roundtrip',
Expand Down
1 change: 1 addition & 0 deletions lib/addons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@storybook/client-logger": "6.0.0-alpha.20",
"@storybook/core-events": "6.0.0-alpha.20",
"@storybook/router": "6.0.0-alpha.20",
"@storybook/theming": "6.0.0-alpha.20",
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
"core-js": "^3.0.1",
"global": "^4.3.2",
"regenerator-runtime": "^0.13.3",
Expand Down
2 changes: 2 additions & 0 deletions lib/addons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Channel } from '@storybook/channels';
import { API } from '@storybook/api';
import { RenderData as RouterData } from '@storybook/router';
import { logger } from '@storybook/client-logger';
import { ThemeVars } from '@storybook/theming';
import { types, Types } from './types';

export { Channel };
Expand Down Expand Up @@ -38,6 +39,7 @@ interface Elements {
}

interface Config {
theme?: ThemeVars;
[key: string]: any;
}

Expand Down
14 changes: 9 additions & 5 deletions lib/api/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export type Module = StoreData &
ProviderData & {
mode?: 'production' | 'development';
state: State;
fullAPI: API;
};

export type State = Other &
Expand Down Expand Up @@ -124,7 +125,7 @@ type StatePartial = Partial<State>;
export type ManagerProviderProps = Children & RouterData & ProviderData & DocsModeData;

class ManagerProvider extends Component<ManagerProviderProps, State> {
api: API;
api: API = {} as API;

modules: any[];

Expand Down Expand Up @@ -178,15 +179,17 @@ class ManagerProvider extends Component<ManagerProviderProps, State> {
initStories,
initURL,
initVersions,
].map(initModule => initModule({ ...routeData, ...apiData, state: this.state }));
].map(initModule =>
initModule({ ...routeData, ...apiData, state: this.state, fullAPI: this.api })
);
tmeasday marked this conversation as resolved.
Show resolved Hide resolved

// Create our initial state by combining the initial state of all modules, then overlaying any saved state
const state = getInitialState(...this.modules.map(m => m.state));

// Get our API by combining the APIs exported by each module
const combo = Object.assign({ navigate }, ...this.modules.map(m => m.api));
const api: API = Object.assign(this.api, { navigate }, ...this.modules.map(m => m.api));

const api = initProviderApi({ provider, store, api: combo });
initProviderApi({ provider, store, api });

api.on(STORY_CHANGED, (id: string) => {
const options = api.getParameters(id, 'options');
Expand Down Expand Up @@ -223,7 +226,8 @@ class ManagerProvider extends Component<ManagerProviderProps, State> {
...state,
location: props.location,
path: props.path,
viewMode: props.viewMode,
// if its a docsOnly page, even the 'story' view mode is considered 'docs'
viewMode: (props.docsMode && props.viewMode) === 'story' ? 'docs' : props.viewMode,
storyId: props.storyId,
};
}
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/lib/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export const transformStoriesRawToStoriesHash = (
return acc;
}

return Object.values(storiesHashOutOfOrder).reduce(addItem, base);
return Object.values(storiesHashOutOfOrder).reduce(addItem, { ...base });
};

export type Item = StoriesHash[keyof StoriesHash];
Expand Down
10 changes: 5 additions & 5 deletions lib/api/src/modules/addons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,20 @@ export function ensurePanel(panels: Panels, selectedPanel?: string, currentPanel
return currentPanel;
}

export default ({ provider, store }: Module) => {
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, storiesHash } = store.getState();
const storyInput = storyId && (storiesHash[storyId] as StoryInput);
const { storyId } = store.getState();
const story = fullAPI.getData(storyId);
tmeasday marked this conversation as resolved.
Show resolved Hide resolved

if (!allPanels || !storyInput) {
if (!allPanels || !story) {
return allPanels;
}

const { parameters } = storyInput;
const { parameters } = story;

const filteredPanels: Collection = {};
Object.entries(allPanels).forEach(([id, panel]) => {
Expand Down
24 changes: 15 additions & 9 deletions lib/api/src/tests/addons.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('Addons API', () => {
describe('#getStoryPanels', () => {
it('should return all panels by default', () => {
// given
const { api } = initAddons({ provider, store });
const { api } = initAddons({ provider, store, fullAPI: { getData: () => undefined } });

// when
const filteredPanels = api.getStoryPanels();
Expand All @@ -73,21 +73,27 @@ describe('Addons API', () => {
it('should filter disabled addons', () => {
// given
const storyId = 'story 1';
const storiesHash = {
[storyId]: {
parameters: {
a11y: { disabled: true },
},
},
};

const storeWithStory = {
getState: () => ({
storyId,
storiesHash: {
[storyId]: {
parameters: {
a11y: { disabled: true },
},
},
},
storiesHash,
}),
setState: jest.fn(),
};

const { api } = initAddons({ provider, store: storeWithStory });
const { api } = initAddons({
provider,
store: storeWithStory,
fullAPI: { getData: id => storiesHash[id] },
});

// when
const filteredPanels = api.getStoryPanels();
Expand Down
6 changes: 1 addition & 5 deletions lib/channel-postmessage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import { logger } from '@storybook/client-logger';

import { isJSON, parse, stringify } from 'telejson';

interface RawEvent {
data: string;
}

interface Config {
page: 'manager' | 'preview';
}
Expand Down Expand Up @@ -106,7 +102,7 @@ export class PostmsgTransport {
return window.parent;
}

private handleEvent(rawEvent: RawEvent): void {
private handleEvent(rawEvent: MessageEvent): void {
try {
const { data } = rawEvent;
const { key, event } = typeof data === 'string' && isJSON(data) ? parse(data) : data;
Expand Down
2 changes: 1 addition & 1 deletion lib/channels/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class Channel {
private handleEvent(event: ChannelEvent, isPeer = false) {
const listeners = this.listeners(event.type);
if (listeners && (isPeer || event.from !== this.sender)) {
listeners.forEach(fn => !(isPeer && fn.ignorePeer) && fn(...event.args));
listeners.forEach(fn => !(isPeer && fn.ignorePeer) && fn.apply(event, event.args));
}
this.data[event.type] = event.args;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/components/src/icon/icon.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { FunctionComponent } from 'react';
import React, { FunctionComponent, ComponentProps } from 'react';
import { styled } from '@storybook/theming';
import icons, { IconKey } from './icons';

import Svg, { SvgProps } from './svg';
import Svg from './svg';

const Path = styled.path({
fill: 'currentColor',
});

export interface IconsProps extends SvgProps {
export interface IconsProps extends ComponentProps<typeof Svg> {
icon: IconKey;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/components/src/icon/svg.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { styled } from '@storybook/theming';

export interface SvgProps {
interface SvgProps {
inline?: boolean;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/components/src/tooltip/WithTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ const WithToolTipState: FunctionComponent<WithTooltipPureProps & {
try {
iframe.contentWindow.document.removeEventListener('click', hide);
} catch (e) {
logger.warn('Removing a click listener from iframe failed: ', e);
logger.debug('Removing a click listener from iframe failed: ', e);
}
});
}
} catch (e) {
logger.warn('Adding a click listener to iframe failed: ', e);
logger.debug('Adding a click listener to iframe failed: ', e);
}
};

Expand Down
Loading