-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathuseEditHistoryActions.ts
More file actions
108 lines (92 loc) · 3.64 KB
/
useEditHistoryActions.ts
File metadata and controls
108 lines (92 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import React from 'react';
import {ArrowUturnCcwLeft, ArrowUturnCwRight} from '@gravity-ui/icons';
import {I18n} from 'i18n';
import {useDispatch, useSelector} from 'react-redux';
import {EditHistoryQA} from 'shared';
import type {AdditionalButtonTemplate} from 'ui/components/ActionPanel/components/ChartSaveControls/types';
import {useAdditionalItems} from 'ui/components/ActionPanel/components/ChartSaveControls/useAdditionalItems';
import {REDO_HOTKEY, UNDO_HOTKEY} from 'ui/constants/misc';
import {useBindHotkey} from 'ui/hooks/useBindHotkey';
import type {DatalensGlobalState} from 'ui/index';
import {goBack, goForward} from 'ui/store/actions/editHistory';
import {selectCanGoBack, selectCanGoForward} from 'ui/store/selectors/editHistory';
export interface UseEditHistoryActionsOptions {
unitId: string;
hotkeyScope: string;
canGoBackSelector?: (state: DatalensGlobalState) => boolean;
canGoForwardSelector?: (state: DatalensGlobalState) => boolean;
iconSize?: number;
disabled?: boolean;
}
const ACTION_PANEL_ICON_SIZE = 16;
const i18n = I18n.keyset('component.action-panel.view');
export function useEditHistoryActions(options: UseEditHistoryActionsOptions) {
const {
unitId,
hotkeyScope,
canGoBackSelector: customCanGoBackSelector,
canGoForwardSelector: customCanGoForwardSelector,
iconSize = ACTION_PANEL_ICON_SIZE,
disabled = false,
} = options;
const dispatch = useDispatch();
const defaultCanGoBackSelector = React.useCallback(
(state: DatalensGlobalState) => !disabled && selectCanGoBack(state, {unitId}),
[disabled, unitId],
);
const defaultCanGoForwardSelector = React.useCallback(
(state: DatalensGlobalState) => !disabled && selectCanGoForward(state, {unitId}),
[disabled, unitId],
);
const canGoBack = useSelector(customCanGoBackSelector || defaultCanGoBackSelector);
const canGoForward = useSelector(customCanGoForwardSelector || defaultCanGoForwardSelector);
const handleGoBack = React.useCallback(() => {
if (canGoBack) {
dispatch(goBack({unitId}));
}
}, [canGoBack, dispatch, unitId]);
const handleGoForward = React.useCallback(() => {
if (canGoForward) {
dispatch(goForward({unitId}));
}
}, [canGoForward, dispatch, unitId]);
const items: AdditionalButtonTemplate[] = React.useMemo(() => {
return [
{
key: 'undo',
action: handleGoBack,
icon: {data: ArrowUturnCcwLeft, size: iconSize},
view: 'flat',
disabled: !canGoBack,
title: i18n('button_undo'),
hotkey: UNDO_HOTKEY.join('+'),
qa: EditHistoryQA.UndoBtn,
},
{
key: 'redo',
action: handleGoForward,
icon: {data: ArrowUturnCwRight, size: iconSize},
view: 'flat',
disabled: !canGoForward,
title: i18n('button_redo'),
hotkey: REDO_HOTKEY.join('+'),
qa: EditHistoryQA.RedoBtn,
},
];
}, [canGoBack, canGoForward, handleGoBack, handleGoForward, iconSize]);
const actions = useAdditionalItems({items});
useBindHotkey({
key: UNDO_HOTKEY,
handler: handleGoBack,
options: {enabled: !disabled && canGoBack, scopes: hotkeyScope},
});
useBindHotkey({
key: REDO_HOTKEY,
handler: handleGoForward,
options: {enabled: !disabled && canGoForward, scopes: hotkeyScope},
});
if (disabled) {
return null;
}
return actions;
}