Skip to content

Commit

Permalink
IMPROVE performance & typing of lib/api Consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelangen committed Feb 14, 2020
1 parent 7b9cd15 commit 79c9c43
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/api/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,12 @@ class ManagerProvider extends Component<ManagerProviderProps, State> {

interface ManagerConsumerProps<P = unknown> {
filter?: (combo: Combo) => P;
children: ReactNode | FunctionComponent<P>;
children: FunctionComponent<P> | ReactNode;
}

const defaultFilter = (c: Combo) => c;

function ManagerConsumerFunc<P = unknown>({
function ManagerConsumerFunc<P = Combo>({
// @ts-ignore
filter = defaultFilter,
children,
Expand Down
9 changes: 1 addition & 8 deletions lib/ui/src/components/preview/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,7 @@ const createCanvas = (id: string, baseUrl = 'iframe.html', withLoader = true): A
render: p => {
return (
<Consumer filter={mapper}>
{({
customCanvas,
storyId,
viewMode,
queryParams,
getElements,
isLoading,
}: ReturnType<typeof mapper>) => (
{({ customCanvas, storyId, viewMode, queryParams, getElements, isLoading }) => (
<ZoomConsumer>
{({ value: scale }) => {
const wrappers = [...defaultWrappers, ...getWrapper(getElements)];
Expand Down
8 changes: 4 additions & 4 deletions lib/ui/src/components/preview/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const fullScreenTool: Addon = {
match: p => p.viewMode === 'story',
render: () => (
<Consumer filter={fullScreenMapper}>
{({ toggle, value }: ReturnType<typeof fullScreenMapper>) => (
{({ toggle, value }) => (
<S.DesktopOnly>
<IconButton
key="full"
Expand Down Expand Up @@ -67,7 +67,7 @@ export const copyTool: Addon = {
match: p => p.viewMode === 'story',
render: () => (
<Consumer filter={copyMapper}>
{({ baseUrl, storyId, origin, pathname, queryParams }: ReturnType<typeof copyMapper>) => (
{({ baseUrl, storyId, origin, pathname, queryParams }) => (
<IconButton
key="copy"
onClick={() =>
Expand All @@ -93,7 +93,7 @@ export const ejectTool: Addon = {
match: p => p.viewMode === 'story',
render: () => (
<Consumer filter={ejectMapper}>
{({ baseUrl, storyId, queryParams }: ReturnType<typeof ejectMapper>) => (
{({ baseUrl, storyId, queryParams }) => (
<IconButton
key="opener"
href={`${baseUrl}?id=${storyId}${stringifyQueryParams(queryParams)}`}
Expand Down Expand Up @@ -133,7 +133,7 @@ export const createTabsTool = (tabs: Addon[]): Addon => ({
title: 'title',
render: () => (
<Consumer filter={tabsMapper}>
{({ viewMode, storyId, path, location }: ReturnType<typeof tabsMapper>) => (
{({ viewMode, storyId, path, location }) => (
<Fragment>
<TabBar key="tabs">
{tabs
Expand Down
4 changes: 1 addition & 3 deletions lib/ui/src/containers/notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ export const mapper = ({ state }: Combo) => {
};

const NotificationConnect: FunctionComponent<any> = props => (
<Consumer filter={mapper}>
{(fromState: ReturnType<typeof mapper>) => <Notifications {...props} {...fromState} />}
</Consumer>
<Consumer filter={mapper}>{fromState => <Notifications {...props} {...fromState} />}</Consumer>
);

export default NotificationConnect;
4 changes: 1 addition & 3 deletions lib/ui/src/containers/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ const mapper = ({ state, api }: Combo) => ({
});

const Panel: FunctionComponent<any> = props => (
<Consumer filter={mapper}>
{(customProps: ReturnType<typeof mapper>) => <AddonPanel {...props} {...customProps} />}
</Consumer>
<Consumer filter={mapper}>{customProps => <AddonPanel {...props} {...customProps} />}</Consumer>
);

export default Panel;
2 changes: 1 addition & 1 deletion lib/ui/src/containers/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function getBaseUrl(): string {

const PreviewConnected = React.memo<{ id: string; withLoader: boolean }>(props => (
<Consumer filter={mapper}>
{(fromState: ReturnType<typeof mapper>) => {
{fromState => {
const p = {
...props,
baseUrl: getBaseUrl(),
Expand Down
4 changes: 1 addition & 3 deletions lib/ui/src/containers/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ export const mapper = ({ state, api }: Combo) => {
};

const Sidebar: FunctionComponent<any> = props => (
<Consumer filter={mapper}>
{(fromState: ReturnType<typeof mapper>) => <SidebarComponent {...props} {...fromState} />}
</Consumer>
<Consumer filter={mapper}>{fromState => <SidebarComponent {...props} {...fromState} />}</Consumer>
);

export default Sidebar;

0 comments on commit 79c9c43

Please sign in to comment.