import React, { Component } from 'react'; import { Base16Theme } from 'redux-devtools-themes'; import { Action } from 'redux'; import { StylingFunction } from 'react-base16-styling'; import { PerformAction } from '@redux-devtools/core'; import { Delta } from 'jsondiffpatch'; import { DEFAULT_STATE, DevtoolsInspectorState } from './redux'; import ActionPreviewHeader from './ActionPreviewHeader'; import DiffTab from './tabs/DiffTab'; import StateTab from './tabs/StateTab'; import ActionTab from './tabs/ActionTab'; export interface TabComponentProps> { labelRenderer: ( keyPath: (string | number)[], nodeType: string, expanded: boolean, expandable: boolean ) => React.ReactNode; styling: StylingFunction; computedStates: { state: S; error?: string }[]; actions: { [actionId: number]: PerformAction }; selectedActionId: number | null; startActionId: number | null; base16Theme: Base16Theme; invertTheme: boolean; isWideLayout: boolean; dataTypeKey: string | symbol | undefined; delta: Delta | null | undefined | false; action: A; nextState: S; monitorState: DevtoolsInspectorState; updateMonitorState: (monitorState: Partial) => void; } export interface Tab> { name: string; component: React.ComponentType>; } const DEFAULT_TABS = [ { name: 'Action', component: ActionTab, }, { name: 'Diff', component: DiffTab, }, { name: 'State', component: StateTab, }, ]; interface Props> { base16Theme: Base16Theme; invertTheme: boolean; isWideLayout: boolean; tabs: Tab[] | ((tabs: Tab[]) => Tab[]); tabName: string; delta: Delta | null | undefined | false; error: string | undefined; nextState: S; computedStates: { state: S; error?: string }[]; action: A; actions: { [actionId: number]: PerformAction }; selectedActionId: number | null; startActionId: number | null; dataTypeKey: string | symbol | undefined; monitorState: DevtoolsInspectorState; updateMonitorState: (monitorState: Partial) => void; styling: StylingFunction; onInspectPath: (path: (string | number)[]) => void; inspectedPath: (string | number)[]; onSelectTab: (tabName: string) => void; } class ActionPreview> extends Component< Props > { static defaultProps = { tabName: DEFAULT_STATE.tabName, }; render() { const { styling, delta, error, nextState, onInspectPath, inspectedPath, tabName, isWideLayout, onSelectTab, action, actions, selectedActionId, startActionId, computedStates, base16Theme, invertTheme, tabs, dataTypeKey, monitorState, updateMonitorState, } = this.props; const renderedTabs: Tab[] = typeof tabs === 'function' ? tabs(DEFAULT_TABS as Tab[]) : tabs ? tabs : (DEFAULT_TABS as Tab[]); const { component: TabComponent } = renderedTabs.find((tab) => tab.name === tabName) || renderedTabs.find((tab) => tab.name === DEFAULT_STATE.tabName)!; return (
>[]} {...{ styling, inspectedPath, onInspectPath, tabName, onSelectTab }} /> {!error && (
)} {error &&
{error}
}
); } labelRenderer = ( [key, ...rest]: (string | number)[], nodeType: string, expanded: boolean ) => { const { styling, onInspectPath, inspectedPath } = this.props; return ( {key} onInspectPath([ ...inspectedPath.slice(0, inspectedPath.length - 1), ...[key, ...rest].reverse(), ]) } > {'(pin)'} {!expanded && ': '} ); }; } export default ActionPreview;