2018-12-22 03:10:49 +03:00
|
|
|
import React, { Component } from 'react';
|
2020-08-31 00:49:06 +03:00
|
|
|
import { Base16Theme } from 'redux-devtools-themes';
|
|
|
|
import { Action } from 'redux';
|
|
|
|
import { StylingFunction } from 'react-base16-styling';
|
2020-12-21 17:08:08 +03:00
|
|
|
import { PerformAction } from '@redux-devtools/core';
|
2020-08-31 00:49:06 +03:00
|
|
|
import { Delta } from 'jsondiffpatch';
|
|
|
|
import { DEFAULT_STATE, DevtoolsInspectorState } from './redux';
|
2018-12-22 03:10:49 +03:00
|
|
|
import ActionPreviewHeader from './ActionPreviewHeader';
|
|
|
|
import DiffTab from './tabs/DiffTab';
|
|
|
|
import StateTab from './tabs/StateTab';
|
|
|
|
import ActionTab from './tabs/ActionTab';
|
|
|
|
|
2020-08-31 00:49:06 +03:00
|
|
|
export interface TabComponentProps<S, A extends Action<unknown>> {
|
|
|
|
labelRenderer: (
|
|
|
|
keyPath: (string | number)[],
|
|
|
|
nodeType: string,
|
|
|
|
expanded: boolean,
|
|
|
|
expandable: boolean
|
|
|
|
) => React.ReactNode;
|
|
|
|
styling: StylingFunction;
|
|
|
|
computedStates: { state: S; error?: string }[];
|
|
|
|
actions: { [actionId: number]: PerformAction<A> };
|
|
|
|
selectedActionId: number | null;
|
|
|
|
startActionId: number | null;
|
|
|
|
base16Theme: Base16Theme;
|
|
|
|
invertTheme: boolean;
|
|
|
|
isWideLayout: boolean;
|
2020-10-26 02:32:04 +03:00
|
|
|
dataTypeKey: string | symbol | undefined;
|
2020-08-31 00:49:06 +03:00
|
|
|
delta: Delta | null | undefined | false;
|
|
|
|
action: A;
|
|
|
|
nextState: S;
|
|
|
|
monitorState: DevtoolsInspectorState;
|
|
|
|
updateMonitorState: (monitorState: Partial<DevtoolsInspectorState>) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Tab<S, A extends Action<unknown>> {
|
|
|
|
name: string;
|
|
|
|
component: React.ComponentType<TabComponentProps<S, A>>;
|
|
|
|
}
|
|
|
|
|
2019-01-10 21:51:14 +03:00
|
|
|
const DEFAULT_TABS = [
|
|
|
|
{
|
|
|
|
name: 'Action',
|
2020-08-08 23:26:39 +03:00
|
|
|
component: ActionTab,
|
2019-01-10 21:51:14 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Diff',
|
2020-08-08 23:26:39 +03:00
|
|
|
component: DiffTab,
|
2019-01-10 21:51:14 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'State',
|
2020-08-08 23:26:39 +03:00
|
|
|
component: StateTab,
|
|
|
|
},
|
2019-01-10 21:51:14 +03:00
|
|
|
];
|
2018-12-22 03:10:49 +03:00
|
|
|
|
2020-08-31 00:49:06 +03:00
|
|
|
interface Props<S, A extends Action<unknown>> {
|
|
|
|
base16Theme: Base16Theme;
|
|
|
|
invertTheme: boolean;
|
|
|
|
isWideLayout: boolean;
|
|
|
|
tabs: Tab<S, A>[] | ((tabs: Tab<S, A>[]) => Tab<S, A>[]);
|
|
|
|
tabName: string;
|
|
|
|
delta: Delta | null | undefined | false;
|
|
|
|
error: string | undefined;
|
|
|
|
nextState: S;
|
|
|
|
computedStates: { state: S; error?: string }[];
|
|
|
|
action: A;
|
|
|
|
actions: { [actionId: number]: PerformAction<A> };
|
|
|
|
selectedActionId: number | null;
|
|
|
|
startActionId: number | null;
|
2020-10-26 02:32:04 +03:00
|
|
|
dataTypeKey: string | symbol | undefined;
|
2020-08-31 00:49:06 +03:00
|
|
|
monitorState: DevtoolsInspectorState;
|
|
|
|
updateMonitorState: (monitorState: Partial<DevtoolsInspectorState>) => void;
|
|
|
|
styling: StylingFunction;
|
|
|
|
onInspectPath: (path: (string | number)[]) => void;
|
|
|
|
inspectedPath: (string | number)[];
|
|
|
|
onSelectTab: (tabName: string) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ActionPreview<S, A extends Action<unknown>> extends Component<
|
|
|
|
Props<S, A>
|
|
|
|
> {
|
2018-12-22 03:10:49 +03:00
|
|
|
static defaultProps = {
|
2020-08-08 23:26:39 +03:00
|
|
|
tabName: DEFAULT_STATE.tabName,
|
2019-01-10 21:51:14 +03:00
|
|
|
};
|
2018-12-22 03:10:49 +03:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
2019-01-10 21:51:14 +03:00
|
|
|
styling,
|
|
|
|
delta,
|
|
|
|
error,
|
|
|
|
nextState,
|
|
|
|
onInspectPath,
|
|
|
|
inspectedPath,
|
|
|
|
tabName,
|
|
|
|
isWideLayout,
|
|
|
|
onSelectTab,
|
|
|
|
action,
|
|
|
|
actions,
|
|
|
|
selectedActionId,
|
|
|
|
startActionId,
|
|
|
|
computedStates,
|
|
|
|
base16Theme,
|
|
|
|
invertTheme,
|
|
|
|
tabs,
|
|
|
|
dataTypeKey,
|
|
|
|
monitorState,
|
2020-08-08 23:26:39 +03:00
|
|
|
updateMonitorState,
|
2018-12-22 03:10:49 +03:00
|
|
|
} = this.props;
|
|
|
|
|
2020-08-31 00:49:06 +03:00
|
|
|
const renderedTabs: Tab<S, A>[] =
|
2019-01-10 21:51:14 +03:00
|
|
|
typeof tabs === 'function'
|
2020-08-31 00:49:06 +03:00
|
|
|
? tabs(DEFAULT_TABS as Tab<S, A>[])
|
2019-01-10 21:51:14 +03:00
|
|
|
: tabs
|
|
|
|
? tabs
|
2020-08-31 00:49:06 +03:00
|
|
|
: (DEFAULT_TABS as Tab<S, A>[]);
|
2018-12-22 03:10:49 +03:00
|
|
|
|
2019-01-10 21:51:14 +03:00
|
|
|
const { component: TabComponent } =
|
2020-08-08 23:26:39 +03:00
|
|
|
renderedTabs.find((tab) => tab.name === tabName) ||
|
2020-08-31 00:49:06 +03:00
|
|
|
renderedTabs.find((tab) => tab.name === DEFAULT_STATE.tabName)!;
|
2018-12-22 03:10:49 +03:00
|
|
|
|
|
|
|
return (
|
2019-01-10 20:23:33 +03:00
|
|
|
<div key="actionPreview" {...styling('actionPreview')}>
|
2018-12-22 03:10:49 +03:00
|
|
|
<ActionPreviewHeader
|
2020-08-31 00:49:06 +03:00
|
|
|
tabs={(renderedTabs as unknown) as Tab<unknown, Action<unknown>>[]}
|
2018-12-22 03:10:49 +03:00
|
|
|
{...{ styling, inspectedPath, onInspectPath, tabName, onSelectTab }}
|
|
|
|
/>
|
2019-01-10 21:51:14 +03:00
|
|
|
{!error && (
|
2019-01-10 20:23:33 +03:00
|
|
|
<div key="actionPreviewContent" {...styling('actionPreviewContent')}>
|
2018-12-22 03:10:49 +03:00
|
|
|
<TabComponent
|
|
|
|
labelRenderer={this.labelRenderer}
|
|
|
|
{...{
|
|
|
|
styling,
|
|
|
|
computedStates,
|
|
|
|
actions,
|
|
|
|
selectedActionId,
|
|
|
|
startActionId,
|
|
|
|
base16Theme,
|
|
|
|
invertTheme,
|
|
|
|
isWideLayout,
|
|
|
|
dataTypeKey,
|
|
|
|
delta,
|
|
|
|
action,
|
|
|
|
nextState,
|
|
|
|
monitorState,
|
2020-08-08 23:26:39 +03:00
|
|
|
updateMonitorState,
|
2018-12-22 03:10:49 +03:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
2019-01-10 21:51:14 +03:00
|
|
|
)}
|
|
|
|
{error && <div {...styling('stateError')}>{error}</div>}
|
2018-12-22 03:10:49 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-31 00:49:06 +03:00
|
|
|
labelRenderer = (
|
|
|
|
[key, ...rest]: (string | number)[],
|
|
|
|
nodeType: string,
|
|
|
|
expanded: boolean
|
|
|
|
) => {
|
2018-12-22 03:10:49 +03:00
|
|
|
const { styling, onInspectPath, inspectedPath } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span>
|
2019-01-10 21:51:14 +03:00
|
|
|
<span {...styling('treeItemKey')}>{key}</span>
|
|
|
|
<span
|
|
|
|
{...styling('treeItemPin')}
|
|
|
|
onClick={() =>
|
|
|
|
onInspectPath([
|
|
|
|
...inspectedPath.slice(0, inspectedPath.length - 1),
|
2020-08-08 23:26:39 +03:00
|
|
|
...[key, ...rest].reverse(),
|
2019-01-10 21:51:14 +03:00
|
|
|
])
|
|
|
|
}
|
|
|
|
>
|
2018-12-22 03:10:49 +03:00
|
|
|
{'(pin)'}
|
|
|
|
</span>
|
|
|
|
{!expanded && ': '}
|
|
|
|
</span>
|
|
|
|
);
|
2019-01-10 21:51:14 +03:00
|
|
|
};
|
2018-12-22 03:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ActionPreview;
|