import React, { createContext, useContext } from 'react'; import { RouteComponentProps, withRouter } from 'react-router-dom'; import Dialog from 'wdk-client/Components/Overlays/Dialog'; import { cxStepBoxes as cx } from 'wdk-client/Views/Strategy/ClassNames'; import CombineStepDetails from 'wdk-client/Views/Strategy/CombineStepDetails'; import NestedStepDetails from 'wdk-client/Views/Strategy/NestedStepDetails'; import StepDetails from 'wdk-client/Views/Strategy/StepDetails'; import { getStepUrl, makeStepDetailsDisplayName } from 'wdk-client/Views/Strategy/StrategyUtils'; import { isCombineUiStepTree, isLeafUiStepTree, StepDetailProps, UiStepTree } from 'wdk-client/Views/Strategy/Types'; import { SaveableTextEditor } from 'wdk-client/Components'; type Props = RouteComponentProps & StepDetailProps; export interface StepAction { /** Unique identifier for the action */ key: string; /** Text of the button */ display: React.ReactType; /** Action to take when user clicks */ onClick: (props: Props) => void; /** The button should be disabled */ isDisabled?: (props: Props) => boolean; /** The button should be hidden */ isHidden?: (props: Props) => boolean; /** The dialog should be closed when the button is clicked */ closeOnClick?: boolean; /** Text to display in tooltip */ tooltip?: (props: Props) => string | undefined; } export const defaultActions: StepAction[] = [ { key: 'view', display: () => View, onClick: ({ history, stepTree }) => { history.push(getStepUrl(stepTree.step)); }, isDisabled: ({ location, stepTree }) => location.pathname.startsWith(getStepUrl(stepTree.step)), tooltip: () => 'View the results of this search' }, { key: 'analyze', display: () => Analyze, onClick: ({ showNewAnalysisTab }) => showNewAnalysisTab(), isDisabled: ({ location, stepTree }) => !location.pathname.startsWith(getStepUrl(stepTree.step)), isHidden: ({ isAnalyzable }) => !isAnalyzable, tooltip: () => 'Analyze the results of this search' }, { key: 'revise', display: () => Revise, onClick: ({ showReviseForm }) => showReviseForm(), isHidden: ({ allowRevise = true }) => !allowRevise, tooltip: () => 'Modify the configuration of this search' }, { key: 'nest', display: () => Make nested strategy, onClick: ({ makeNestedStrategy }) => { makeNestedStrategy(); }, isHidden: ({ stepTree, isNested }) => ( !isLeafUiStepTree(stepTree) || stepTree.nestedControlStep == null || isNested ), tooltip: () => 'Create a non-linear search strategy' }, { key: 'unnest', display: () => Unnest strategy, onClick: ({ makeUnnestStrategy }) => makeUnnestStrategy(), isDisabled: ({ stepTree }) => isCombineUiStepTree(stepTree), isHidden: ({ isNested }) => !isNested, tooltip: ({ stepTree }) => isCombineUiStepTree(stepTree) ? 'Nested strategies with more than one step cannot be unnested' : 'Convert nested strategy into a single step' }, { key: 'toggleNested', display: ({ stepTree }) => { stepTree.nestedControlStep && stepTree.nestedControlStep.expanded ? 'Hide nested' : 'Show nested' }, onClick: ({ isExpanded, collapseNestedStrategy, expandNestedStrategy }) => { if (isExpanded) collapseNestedStrategy(); else expandNestedStrategy(); }, isHidden: ({ isNested }) => !isNested, tooltip: ({ isExpanded }) => isExpanded ? 'Hide nested strategy details' : 'Show nested strategy details' }, { key: 'insertBefore', display: () => Insert step before, onClick: ({ insertStepBefore }) => insertStepBefore(), tooltip: () => 'Insert a search into your search strategy before this search' }, { key: 'delete', display: () => Delete, onClick: ({ deleteStep }) => deleteStep(), isDisabled: ({ isDeleteable }) => !isDeleteable, tooltip: ({ isDeleteable }) => isDeleteable ? 'Delete this search from your strategy' : 'Deleting this step will yield an invalid search strategy' } ]; export const StepDetailsActionContext = createContext(defaultActions); export default withRouter(function StepDetailsDialog(props: Props) { const { isNested, isOpen, stepTree, onClose, renameStep, } = props; const { step, nestedControlStep, recordClass } = stepTree; const displayName = makeStepDetailsDisplayName(step, isCombineUiStepTree(stepTree), nestedControlStep); const actions = useContext(StepDetailsActionContext); return ( {actions .filter(action => action.isHidden == null || !action.isHidden(props)) .map(action => (
))} )} open={isOpen} onClose={onClose} >
Details for step
{step.estimatedSize == null ? '?' : step.estimatedSize} {step.estimatedSize === 1 ? recordClass.displayName : recordClass.displayNamePlural}
{ isNested ? : isCombineUiStepTree(stepTree) ? : }
); });