Skip to content

Commit

Permalink
fix(ui): Disable Redo & Undo if no actions available (#679)
Browse files Browse the repository at this point in the history
* fix(ui): Disable Redo & Undo if no actions available

* feedback(ui): updated conditionals for handleWorkFlow Redo and Undo
  • Loading branch information
billcookie authored Dec 6, 2024
1 parent d0d6387 commit 3991f4e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ type CanvasAction = "undo" | "redo";
type Action = ToolboxItem<CanvasAction>;

type Props = {
undoDisabled?: boolean;
canUndo: boolean;
canRedo: boolean;
onRedo?: () => void;
onUndo?: () => void;
};

const Toolbox: React.FC<Props> = ({ onRedo, onUndo }) => {
const Toolbox: React.FC<Props> = ({ canUndo, canRedo, onRedo, onUndo }) => {
const t = useT();

const availableTools: Tool[] = [
Expand Down Expand Up @@ -134,6 +135,7 @@ const Toolbox: React.FC<Props> = ({ onRedo, onUndo }) => {
tooltipPosition="right"
tooltipText={action.name}
icon={action.icon}
disabled={action.id === "undo" ? !canUndo : !canRedo}
onClick={() =>
action.id === "redo"
? onRedo?.()
Expand Down
11 changes: 10 additions & 1 deletion ui/src/features/Editor/components/OverlayUI/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type OverlayUIProps = {
nodeType: ActionNodeType;
};
nodes: Node[];
canUndo: boolean;
canRedo: boolean;
onWorkflowDeployment: (
deploymentId?: string,
description?: string,
Expand All @@ -34,6 +36,8 @@ const OverlayUI: React.FC<OverlayUIProps> = ({
hoveredDetails,
nodePickerOpen,
nodes,
canUndo,
canRedo,
onWorkflowDeployment,
onNodesChange,
onNodePickerClose,
Expand All @@ -49,7 +53,12 @@ const OverlayUI: React.FC<OverlayUIProps> = ({
{/* {devMode && <DevTools />} */}
{canvas}
<Breadcrumb />
<Toolbox onRedo={onWorkflowRedo} onUndo={onWorkflowUndo} />
<Toolbox
canUndo={canUndo}
canRedo={canRedo}
onRedo={onWorkflowRedo}
onUndo={onWorkflowUndo}
/>
<ActionBar
allowedToDeploy={allowedToDeploy}
onWorkflowDeployment={onWorkflowDeployment}
Expand Down
4 changes: 4 additions & 0 deletions ui/src/features/Editor/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export default ({
handleWorkflowUndo,
handleWorkflowRedo,
handleWorkflowRename,
canUndo,
canRedo,
} = useYjsStore({
workflowId: currentWorkflowId,
yWorkflows,
Expand Down Expand Up @@ -196,5 +198,7 @@ export default ({
handleWorkflowRedo,
handleWorkflowUndo,
handleWorkflowRename,
canUndo,
canRedo,
};
};
4 changes: 4 additions & 0 deletions ui/src/features/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export default function Editor({
hoveredDetails,
nodePickerOpen,
openPanel,
canUndo,
canRedo,
handleWorkflowAdd,
handleWorkflowDeployment,
handlePanelOpen,
Expand Down Expand Up @@ -63,6 +65,8 @@ export default function Editor({
hoveredDetails={hoveredDetails}
nodePickerOpen={nodePickerOpen}
nodes={nodes}
canUndo={canUndo}
canRedo={canRedo}
onWorkflowDeployment={handleWorkflowDeployment}
onWorkflowUndo={handleWorkflowUndo}
onWorkflowRedo={handleWorkflowRedo}
Expand Down
17 changes: 15 additions & 2 deletions ui/src/lib/yjs/useYjsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,27 @@ export default ({
const { createDeployment, useUpdateDeployment } = useDeployment();

const handleWorkflowUndo = useCallback(() => {
if (undoManager?.undoStack && undoManager.undoStack.length > 0) {
const stackLength = undoManager?.undoStack?.length ?? 0;
if (stackLength > 0) {
undoManager?.undo();
}
}, [undoManager]);

const handleWorkflowRedo = useCallback(() => {
if (undoManager?.redoStack && undoManager.redoStack.length > 0) {
const stackLength = undoManager?.redoStack?.length ?? 0;
if (stackLength > 0) {
undoManager?.redo();
}
}, [undoManager]);
const canUndo = useMemo(() => {
const stackLength = undoManager?.undoStack?.length ?? 0;
return stackLength > 0;
}, [undoManager?.undoStack?.length]);

const canRedo = useMemo(() => {
const stackLength = undoManager?.redoStack?.length ?? 0;
return stackLength > 0;
}, [undoManager?.redoStack?.length]);

const rawWorkflows = useY(yWorkflows);

Expand Down Expand Up @@ -166,6 +177,8 @@ export default ({
handleEdgesUpdate,
handleWorkflowUndo,
handleWorkflowRedo,
canUndo,
canRedo,
handleWorkflowRename,
};
};

0 comments on commit 3991f4e

Please sign in to comment.