@@ -27,30 +27,33 @@ import { KeyboardShortcut } from 'uiSrc/components'
27
27
import { ThemeContext } from 'uiSrc/contexts/themeContext'
28
28
import { appRedisCommandsSelector } from 'uiSrc/slices/app/redis-commands'
29
29
import { IEditorMount , ISnippetController } from 'uiSrc/pages/workbench/interfaces'
30
+ import { CommandExecutionUI } from 'uiSrc/slices/interfaces'
30
31
32
+ import { workbenchResultsSelector } from 'uiSrc/slices/workbench/wb-results'
31
33
import styles from './styles.module.scss'
32
34
33
35
export interface Props {
34
- query : string ;
35
- loading : boolean ;
36
- setQueryEl : Function ;
37
- setQuery : ( script : string ) => void ;
38
- onSubmit : ( query ?: string ) => void ;
39
- onKeyDown ?: ( e : React . KeyboardEvent , script : string ) => void ;
36
+ query : string
37
+ loading : boolean
38
+ setQueryEl : Function
39
+ setQuery : ( script : string ) => void
40
+ onSubmit : ( query ?: string ) => void
41
+ onKeyDown ?: ( e : React . KeyboardEvent , script : string ) => void
40
42
}
41
43
42
44
let decorations : string [ ] = [ ]
45
+ let execHistoryPos : number = 0
46
+ let execHistory : CommandExecutionUI [ ] = [ ]
43
47
44
48
const Query = ( props : Props ) => {
45
49
const { query = '' , setQuery, onKeyDown, onSubmit, setQueryEl } = props
46
50
let contribution : Nullable < ISnippetController > = null
47
51
48
- const {
49
- commandsArray : REDIS_COMMANDS_ARRAY ,
50
- spec : REDIS_COMMANDS_SPEC
51
- } = useSelector ( appRedisCommandsSelector )
52
+ const { commandsArray : REDIS_COMMANDS_ARRAY , spec : REDIS_COMMANDS_SPEC } = useSelector ( appRedisCommandsSelector )
53
+ const { items : execHistoryItems } = useSelector ( workbenchResultsSelector )
52
54
const { theme } = useContext ( ThemeContext )
53
55
const monacoObjects = useRef < Nullable < IEditorMount > > ( null )
56
+
54
57
let disposeCompletionItemProvider = ( ) => { }
55
58
let disposeSignatureHelpProvider = ( ) => { }
56
59
@@ -63,6 +66,12 @@ const Query = (props: Props) => {
63
66
} ,
64
67
[ ] )
65
68
69
+ useEffect ( ( ) => {
70
+ // HACK: The Monaco editor memoize the state and ignores updates to it
71
+ execHistory = execHistoryItems
72
+ execHistoryPos = 0
73
+ } , [ execHistoryItems ] )
74
+
66
75
useEffect ( ( ) => {
67
76
if ( ! monacoObjects . current ) return
68
77
const commands = query . split ( '\n' )
@@ -93,6 +102,7 @@ const Query = (props: Props) => {
93
102
}
94
103
95
104
const handleSubmit = ( value ?: string ) => {
105
+ execHistoryPos = 0
96
106
onSubmit ( value )
97
107
}
98
108
@@ -112,6 +122,20 @@ const Query = (props: Props) => {
112
122
}
113
123
}
114
124
125
+ const onQuickHistoryAccess = ( ) => {
126
+ if ( ! monacoObjects . current ) return
127
+ const { editor } = monacoObjects ?. current
128
+
129
+ const position = editor . getPosition ( )
130
+ if ( position ?. lineNumber !== 1 ) return
131
+
132
+ if ( execHistory [ execHistoryPos ] ) {
133
+ const command = execHistory [ execHistoryPos ] . command || ''
134
+ editor . setValue ( command )
135
+ execHistoryPos ++
136
+ }
137
+ }
138
+
115
139
const onKeyDownMonaco = ( e : monacoEditor . IKeyboardEvent ) => {
116
140
// trigger parameter hints
117
141
if (
@@ -123,6 +147,12 @@ const Query = (props: Props) => {
123
147
onTriggerParameterHints ( )
124
148
}
125
149
150
+ if (
151
+ e . keyCode === monaco . KeyCode . UpArrow
152
+ ) {
153
+ onQuickHistoryAccess ( )
154
+ }
155
+
126
156
if ( e . keyCode === monaco . KeyCode . Enter || e . keyCode === monaco . KeyCode . Space ) {
127
157
onExitSnippetMode ( )
128
158
}
0 commit comments