1
1
import React , { useState , useEffect } from "react" ;
2
- import { useSelector , useDispatch } from ' react-redux'
2
+ import { useSelector , useDispatch } from " react-redux" ;
3
3
import { setIsSubmit , setQuery } from "../../actions" ;
4
- import loadLogs from "../../actions/loadLogs"
5
- import setLoading from "../../actions/setLoading"
4
+ import loadLogs from "../../actions/loadLogs" ;
5
+ import setLoading from "../../actions/setLoading" ;
6
6
import { setLabelsBrowserOpen } from "../../actions/setLabelsBrowserOpen" ;
7
-
7
+ import localService from "../../services/localService" ;
8
+ import setQueryHistory from "../../actions/setQueryHistory" ;
9
+ import HistoryIcon from "@mui/icons-material/History" ;
10
+ import styled from "@emotion/styled" ;
11
+ import setHistoryOpen from "../../actions/setHistoryOpen" ;
12
+ import { Tooltip } from "@mui/material" ;
13
+ import Badge from '@mui/material/Badge' ;
14
+
15
+ const HistoryButton = styled . button `
16
+ background: none;
17
+ color: #ddd;
18
+ font-size: 14px;
19
+ border: none;
20
+ cursor: pointer;
21
+ ` ;
8
22
export const QueryBar = ( ) => {
9
23
//const [query, setQuery] = useState(props.query);
10
24
11
- const dispatch = useDispatch ( )
12
- const labelsBrowserOpen = useSelector ( ( store ) => store . labelsBrowserOpen )
13
- const debug = useSelector ( store => store . debug )
14
- const query = useSelector ( ( store ) => store . query )
15
- const isSubmit = useSelector ( store => store . isSubmit )
16
- const [ queryInput , setQueryInput ] = useState ( query )
17
- const [ queryValid , setQueryValid ] = useState ( false )
18
- const SHOW_LOGS = "Show Logs"
19
- const LOG_BROWSER = "Log Browser"
20
- const onQueryValid = ( query ) => {
21
- return query !== '{' && query !== '}' && query !== '{}' && query !== '' // TODO: make a proper query validation
22
- }
25
+ const dispatch = useDispatch ( ) ;
26
+ const historyService = localService ( ) . historyStore ( ) ;
27
+ const labelsBrowserOpen = useSelector ( ( store ) => store . labelsBrowserOpen ) ;
28
+ const debug = useSelector ( ( store ) => store . debug ) ;
29
+ const query = useSelector ( ( store ) => store . query ) ;
30
+ const isSubmit = useSelector ( ( store ) => store . isSubmit ) ;
31
+ const historyOpen = useSelector ( ( store ) => store . historyOpen )
32
+ const [ queryInput , setQueryInput ] = useState ( query ) ;
33
+ const [ queryValid , setQueryValid ] = useState ( false ) ;
34
+
35
+ const SHOW_LOGS = "Show Logs" ;
36
+ const LOG_BROWSER = "Log Browser" ;
37
+ const queryHistory = useSelector ( ( store ) => store . queryHistory )
38
+ const [ historyItems , setHistoryItems ] = useState ( queryHistory . length > 0 )
23
39
24
-
25
40
useEffect ( ( ) => {
26
- // force a query to be run after load of component
27
- if ( debug ) console . log ( '🚧 LOGIC/QueryBar/' , typeof query , query . length )
28
-
29
- if ( onQueryValid ( query && isSubmit === "true" ) ) {
30
- if ( debug ) console . log ( '🚧 LOGIC/QueryBar/ dispatch ' , query !== "{}" , query . length > 0 , query !== "{}" || query . length > 1 )
31
- // here
32
- dispatch ( setLoading ( true ) )
33
-
34
- dispatch ( loadLogs ( ) )
35
-
36
- setTimeout ( ( ) => {
37
- dispatch ( setIsSubmit ( false ) )
38
- } , 200 )
39
-
40
- } else if ( ! onQueryValid ( query ) && isSubmit === "true" ) {
41
- dispatch ( setIsSubmit ( false ) )
42
- }
41
+ setHistoryItems ( queryHistory . length > 0 )
42
+ } , [ queryHistory ] )
43
43
44
- } , [ ] )
44
+ const onQueryValid = ( query ) => {
45
+ return query !== "{" && query !== "}" && query !== "{}" && query !== "" ; // TODO: make a proper query validation
46
+ } ;
47
+
48
+ useEffect ( ( ) => {
49
+ // force a query to be run after load of component
50
+ if ( debug )
51
+ console . log ( "🚧 LOGIC/QueryBar/" , typeof query , query . length ) ;
52
+
53
+ if ( onQueryValid ( query && isSubmit === "true" ) ) {
54
+ if ( debug )
55
+ console . log (
56
+ "🚧 LOGIC/QueryBar/ dispatch " ,
57
+ query !== "{}" ,
58
+ query . length > 0 ,
59
+ query !== "{}" || query . length > 1
60
+ ) ;
61
+ // here
62
+ dispatch ( setLoading ( true ) ) ;
63
+
64
+ dispatch ( loadLogs ( ) ) ;
65
+
66
+ setTimeout ( ( ) => {
67
+ dispatch ( setIsSubmit ( false ) ) ;
68
+ } , 200 ) ;
69
+ } else if ( ! onQueryValid ( query ) && isSubmit === "true" ) {
70
+ dispatch ( setIsSubmit ( false ) ) ;
71
+ }
72
+ } , [ ] ) ;
45
73
46
74
useEffect ( ( ) => {
47
75
setQueryInput ( query ) ;
48
- setQueryValid ( onQueryValid ( query ) )
76
+ setQueryValid ( onQueryValid ( query ) ) ;
49
77
} , [ query , queryInput ] ) ;
50
78
51
-
52
79
const onValueDisplay = ( e ) => {
53
- e . preventDefault ( )
80
+ e . preventDefault ( ) ;
54
81
const isOpen = labelsBrowserOpen ? false : true ;
55
- dispatch ( setLabelsBrowserOpen ( isOpen ) )
82
+ dispatch ( setLabelsBrowserOpen ( isOpen ) ) ;
56
83
} ;
57
84
58
85
const handleChange = ( e ) => {
59
86
const qr = e . target . value ;
60
- setQueryInput ( qr )
61
- dispatch ( setQuery ( qr ) )
87
+ setQueryInput ( qr ) ;
88
+ dispatch ( setQuery ( qr ) ) ;
62
89
} ;
63
90
64
91
const onBrowserActive = ( ) => {
65
- return ! labelsBrowserOpen ? ( {
66
- 'borderColor' : '#11abab'
67
- } ) : ( { } )
68
- }
92
+ return ! labelsBrowserOpen
93
+ ? {
94
+ borderColor : "#11abab" ,
95
+ }
96
+ : { } ;
97
+ } ;
69
98
70
99
const handleInputKeyDown = ( e ) => {
71
- if ( e . code === ' Enter' && e . ctrlKey ) {
72
- onSubmit ( e )
100
+ if ( e . code === " Enter" && e . ctrlKey ) {
101
+ onSubmit ( e ) ;
73
102
}
74
- }
103
+ } ;
75
104
76
105
const onSubmit = ( e ) => {
77
106
e . preventDefault ( ) ;
78
-
79
- dispatch ( setQuery ( queryInput ) )
80
107
81
- if ( onQueryValid ( query ) ) {
82
- dispatch ( setLabelsBrowserOpen ( false ) )
83
- dispatch ( loadLogs ( ) )
108
+ dispatch ( setQuery ( queryInput ) ) ;
84
109
110
+ if ( onQueryValid ( query ) ) {
111
+ try {
112
+ const historyUpdated = historyService . add ( {
113
+ data : query ,
114
+ url : window . location . hash ,
115
+ } ) ;
116
+ dispatch ( setQueryHistory ( historyUpdated ) ) ;
117
+ dispatch ( setLabelsBrowserOpen ( false ) ) ;
118
+ dispatch ( loadLogs ( ) ) ;
119
+ } catch ( e ) {
120
+ console . log ( e ) ;
121
+ }
85
122
} else {
86
-
87
123
console . log ( "Please make a log query" , query ) ;
88
-
89
124
}
90
125
} ;
91
-
92
-
93
-
126
+ function handleHistoryClick ( e ) {
127
+ dispatch ( ( setHistoryOpen ( ! historyOpen ) ) )
128
+ }
94
129
return (
95
130
< div className = { "query-bar-container" } >
96
131
< span
97
132
style = { onBrowserActive ( ) }
98
- className = { "show-log-browser" } onClick = { onValueDisplay } >
133
+ className = { "show-log-browser" }
134
+ onClick = { onValueDisplay }
135
+ >
99
136
{ LOG_BROWSER }
100
137
</ span >
101
138
@@ -104,10 +141,24 @@ export const QueryBar = () => {
104
141
placeholder = { "Enter a cLoki Query" }
105
142
onChange = { handleChange }
106
143
value = { queryInput }
107
- tabIndex = '0'
144
+ tabIndex = "0"
108
145
onKeyDown = { handleInputKeyDown }
109
146
/>
147
+ < Tooltip title = { 'Query History (' + queryHistory . length + ')' } >
148
+
149
+
150
+ < HistoryButton
151
+ style = { {
152
+ color : historyItems ? 'orange' : '#ddd'
153
+ } }
154
+ onClick = { e => handleHistoryClick ( e ) }
155
+ >
156
+ < HistoryIcon fontSize = { "small" } />
157
+ </ HistoryButton >
158
+
159
+
110
160
161
+ </ Tooltip >
111
162
< button
112
163
disabled = { ! queryValid }
113
164
type = "submit"
0 commit comments