Skip to content

Commit 0e00cd7

Browse files
authored
Merge pull request #433 from ajthinking/run-hotkey
Bypass run sidebar on shift+r
2 parents 9402142 + 9842946 commit 0e00cd7

File tree

3 files changed

+48
-29
lines changed

3 files changed

+48
-29
lines changed

packages/ui/src/components/DataStory/controls/RunControl.tsx

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,18 @@ import { useCallback, useState, useMemo, useEffect } from 'react';
55
import { AbortIcon } from '../icons/abortIcon';
66
import { DataStoryEvents, DataStoryEventType } from '../events/dataStoryEventType';
77
import { useDataStoryEvent } from '../events/eventManager';
8-
import { createDataStoryId, debounce } from '@data-story/core';
8+
import { useRunControl } from './useRunControl';
99

1010
export const RunControl = () => {
11-
const [isRunning, setIsRunning] = useState<boolean>(false);
12-
const onRun = useStore((state) => state.onRun);
13-
const abortExecution = useStore((state) => state.abortExecution);
14-
const [executionId, setExecutionId] = useState<string>('');
15-
16-
const handleRun = debounce(async () => {
17-
if (isRunning && executionId) {
18-
abortExecution(executionId);
19-
setIsRunning(false);
20-
} else {
21-
const tempExecutionId = createDataStoryId();
22-
setExecutionId(tempExecutionId);
23-
onRun(tempExecutionId);
24-
setIsRunning(true);
25-
}
26-
}, 300);
27-
28-
const dataStoryEvent = useCallback((event: DataStoryEventType) => {
29-
const stopRunning = event.type === DataStoryEvents.RUN_ABORT || event.type === DataStoryEvents.RUN_ERROR || event.type === DataStoryEvents.RUN_SUCCESS;
30-
if (stopRunning) {
31-
setIsRunning(false);
32-
setExecutionId('');
33-
}
34-
}, []);
35-
useDataStoryEvent(dataStoryEvent);
11+
const { isRunning, handleRun } = useRunControl();
3612

3713
return (
3814
<ControlButton
3915
title="Run"
4016
aria-label="run"
4117
onClick={handleRun}
4218
>
43-
{isRunning ? <AbortIcon /> : <RunIcon/> }
19+
{isRunning ? <AbortIcon /> : <RunIcon />}
4420
</ControlButton>
4521
);
4622
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useState, useCallback, useMemo } from 'react';
2+
import { createDataStoryId, debounce } from '@data-story/core';
3+
import { useStore } from '../store/store';
4+
import { DataStoryEvents, DataStoryEventType } from '../events/dataStoryEventType';
5+
import { useDataStoryEvent } from '../events/eventManager';
6+
7+
export function useRunControl() {
8+
const [isRunning, setIsRunning] = useState(false);
9+
const [executionId, setExecutionId] = useState('');
10+
const onRun = useStore((state) => state.onRun);
11+
const abortExecution = useStore((state) => state.abortExecution);
12+
13+
const handleRun = useMemo(
14+
() => debounce(async () => {
15+
if (isRunning && executionId) {
16+
abortExecution(executionId);
17+
setIsRunning(false);
18+
} else {
19+
const tempExecutionId = createDataStoryId();
20+
setExecutionId(tempExecutionId);
21+
onRun(tempExecutionId);
22+
setIsRunning(true);
23+
}
24+
}, 300),
25+
[isRunning, executionId, abortExecution, onRun],
26+
);
27+
28+
const dataStoryEvent = useCallback((event: DataStoryEventType) => {
29+
const stopRunning =
30+
event.type === DataStoryEvents.RUN_ABORT ||
31+
event.type === DataStoryEvents.RUN_ERROR ||
32+
event.type === DataStoryEvents.RUN_SUCCESS;
33+
if (stopRunning) {
34+
setIsRunning(false);
35+
setExecutionId('');
36+
}
37+
}, []);
38+
useDataStoryEvent(dataStoryEvent);
39+
40+
return { isRunning, handleRun };
41+
}

packages/ui/src/components/DataStory/useHotkeys.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect } from 'react';
2+
import { useRunControl } from './controls/useRunControl';
23
import { ReactFlowNode } from '../Node/ReactFlowNode';
34
import { Direction } from './getNodesWithNewSelection';
45
import { DataStoryCanvasProps } from './types';
@@ -89,10 +90,11 @@ export function useHotkeys({
8990
}
9091
}, [hotkeyManager, setShowAddNode, nodeDescriptions, addNodeFromDescription]);
9192

93+
const { handleRun } = useRunControl();
9294
useEffect(() => {
93-
hotkeyManager.register('Shift+KeyR', () => setShowRun(true));
95+
hotkeyManager.register('Shift+KeyR', handleRun);
9496
return () => hotkeyManager.unregister('Shift+KeyR');
95-
}, [hotkeyManager, setShowRun]);
97+
}, [hotkeyManager, handleRun]);
9698

9799
useEffect(() => {
98100
hotkeyManager.register('Ctrl+KeyS', () => onSave?.(toDiagram?.()));

0 commit comments

Comments
 (0)