5
5
* LICENSE file in the root directory of this source tree.
6
6
*/
7
7
8
- import { parse } from 'graphql' ;
9
8
import React from 'react' ;
10
- import QueryStore , { QueryStoreItem } from '../utility/QueryStore' ;
9
+ import { QueryStoreItem } from '../utility/QueryStore' ;
11
10
import HistoryQuery , {
12
11
HandleEditLabelFn ,
13
- HandleToggleFavoriteFn ,
14
12
HandleSelectQueryFn ,
13
+ HandleToggleFavoriteFn ,
15
14
} from './HistoryQuery' ;
16
15
import StorageAPI from '../utility/StorageAPI' ;
17
-
18
- const MAX_QUERY_SIZE = 100000 ;
19
- const MAX_HISTORY_LENGTH = 20 ;
20
-
21
- const shouldSaveQuery = (
22
- query ?: string ,
23
- variables ?: string ,
24
- headers ?: string ,
25
- lastQuerySaved ?: QueryStoreItem ,
26
- ) => {
27
- if ( ! query ) {
28
- return false ;
29
- }
30
-
31
- try {
32
- parse ( query ) ;
33
- } catch ( e ) {
34
- return false ;
35
- }
36
-
37
- // Don't try to save giant queries
38
- if ( query . length > MAX_QUERY_SIZE ) {
39
- return false ;
40
- }
41
- if ( ! lastQuerySaved ) {
42
- return true ;
43
- }
44
- if ( JSON . stringify ( query ) === JSON . stringify ( lastQuerySaved . query ) ) {
45
- if (
46
- JSON . stringify ( variables ) === JSON . stringify ( lastQuerySaved . variables )
47
- ) {
48
- if ( JSON . stringify ( headers ) === JSON . stringify ( lastQuerySaved . headers ) ) {
49
- return false ;
50
- }
51
- if ( headers && ! lastQuerySaved . headers ) {
52
- return false ;
53
- }
54
- }
55
- if ( variables && ! lastQuerySaved . variables ) {
56
- return false ;
57
- }
58
- }
59
- return true ;
60
- } ;
16
+ import HistoryStore from '../utility/HistoryStore' ;
61
17
62
18
type QueryHistoryProps = {
63
19
query ?: string ;
@@ -67,6 +23,7 @@ type QueryHistoryProps = {
67
23
queryID ?: number ;
68
24
onSelectQuery : HandleSelectQueryFn ;
69
25
storage : StorageAPI ;
26
+ maxHistoryLength : number ;
70
27
} ;
71
28
72
29
type QueryHistoryState = {
@@ -77,129 +34,87 @@ export class QueryHistory extends React.Component<
77
34
QueryHistoryProps ,
78
35
QueryHistoryState
79
36
> {
80
- historyStore : QueryStore ;
81
- favoriteStore : QueryStore ;
37
+ historyStore : HistoryStore ;
82
38
83
39
constructor ( props : QueryHistoryProps ) {
84
40
super ( props ) ;
85
- this . historyStore = new QueryStore (
86
- 'queries' ,
87
- props . storage ,
88
- MAX_HISTORY_LENGTH ,
41
+ this . historyStore = new HistoryStore (
42
+ this . props . storage ,
43
+ this . props . maxHistoryLength ,
89
44
) ;
90
- // favorites are not automatically deleted, so there's no need for a max length
91
- this . favoriteStore = new QueryStore ( 'favorites' , props . storage , null ) ;
92
- const historyQueries = this . historyStore . fetchAll ( ) ;
93
- const favoriteQueries = this . favoriteStore . fetchAll ( ) ;
94
- const queries = historyQueries . concat ( favoriteQueries ) ;
45
+ const queries = this . historyStore . queries ;
95
46
this . state = { queries } ;
96
47
}
97
48
98
- render ( ) {
99
- const queries = this . state . queries . slice ( ) . reverse ( ) ;
100
- const queryNodes = queries . map ( ( query , i ) => {
101
- return (
102
- < HistoryQuery
103
- handleEditLabel = { this . editLabel }
104
- handleToggleFavorite = { this . toggleFavorite }
105
- key = { `${ i } :${ query . label || query . query } ` }
106
- onSelect = { this . props . onSelectQuery }
107
- { ...query }
108
- />
109
- ) ;
110
- } ) ;
111
- return (
112
- < section aria-label = "History" >
113
- < div className = "history-title-bar" >
114
- < div className = "history-title" > { 'History' } </ div >
115
- < div className = "doc-explorer-rhs" > { this . props . children } </ div >
116
- </ div >
117
- < ul className = "history-contents" > { queryNodes } </ ul >
118
- </ section >
119
- ) ;
120
- }
121
-
122
- // Public API
123
- updateHistory = (
49
+ onUpdateHistory = (
124
50
query ?: string ,
125
51
variables ?: string ,
126
52
headers ?: string ,
127
53
operationName ?: string ,
128
54
) => {
129
- if (
130
- shouldSaveQuery (
131
- query ,
132
- variables ,
133
- headers ,
134
- this . historyStore . fetchRecent ( ) ,
135
- )
136
- ) {
137
- this . historyStore . push ( {
138
- query,
139
- variables,
140
- headers,
141
- operationName,
142
- } ) ;
143
- const historyQueries = this . historyStore . items ;
144
- const favoriteQueries = this . favoriteStore . items ;
145
- const queries = historyQueries . concat ( favoriteQueries ) ;
146
- this . setState ( {
147
- queries,
148
- } ) ;
149
- }
55
+ this . historyStore . updateHistory ( query , variables , headers , operationName ) ;
56
+ this . setState ( { queries : this . historyStore . queries } ) ;
150
57
} ;
151
58
152
- // Public API
153
- toggleFavorite : HandleToggleFavoriteFn = (
59
+ onHandleEditLabel : HandleEditLabelFn = (
154
60
query ,
155
61
variables ,
156
62
headers ,
157
63
operationName ,
158
64
label ,
159
65
favorite ,
160
66
) => {
161
- const item : QueryStoreItem = {
67
+ this . historyStore . editLabel (
162
68
query ,
163
69
variables ,
164
70
headers ,
165
71
operationName ,
166
72
label ,
167
- } ;
168
- if ( ! this . favoriteStore . contains ( item ) ) {
169
- item . favorite = true ;
170
- this . favoriteStore . push ( item ) ;
171
- } else if ( favorite ) {
172
- item . favorite = false ;
173
- this . favoriteStore . delete ( item ) ;
174
- }
175
- this . setState ( {
176
- queries : [ ...this . historyStore . items , ...this . favoriteStore . items ] ,
177
- } ) ;
73
+ favorite ,
74
+ ) ;
75
+ this . setState ( { queries : this . historyStore . queries } ) ;
178
76
} ;
179
77
180
- // Public API
181
- editLabel : HandleEditLabelFn = (
78
+ onToggleFavorite : HandleToggleFavoriteFn = (
182
79
query ,
183
80
variables ,
184
81
headers ,
185
82
operationName ,
186
83
label ,
187
84
favorite ,
188
85
) => {
189
- const item = {
86
+ this . historyStore . toggleFavorite (
190
87
query ,
191
88
variables ,
192
89
headers ,
193
90
operationName ,
194
91
label ,
195
- } ;
196
- if ( favorite ) {
197
- this . favoriteStore . edit ( { ...item , favorite } ) ;
198
- } else {
199
- this . historyStore . edit ( item ) ;
200
- }
201
- this . setState ( {
202
- queries : [ ...this . historyStore . items , ...this . favoriteStore . items ] ,
203
- } ) ;
92
+ favorite ,
93
+ ) ;
94
+ this . setState ( { queries : this . historyStore . queries } ) ;
204
95
} ;
96
+
97
+ render ( ) {
98
+ const queries = this . state . queries . slice ( ) . reverse ( ) ;
99
+ const queryNodes = queries . map ( ( query , i ) => {
100
+ return (
101
+ < HistoryQuery
102
+ handleEditLabel = { this . onHandleEditLabel }
103
+ handleToggleFavorite = { this . onToggleFavorite }
104
+ key = { `${ i } :${ query . label || query . query } ` }
105
+ onSelect = { this . props . onSelectQuery }
106
+ { ...query }
107
+ />
108
+ ) ;
109
+ } ) ;
110
+ return (
111
+ < section aria-label = "History" >
112
+ < div className = "history-title-bar" >
113
+ < div className = "history-title" > { 'History' } </ div >
114
+ < div className = "doc-explorer-rhs" > { this . props . children } </ div >
115
+ </ div >
116
+ < ul className = "history-contents" > { queryNodes } </ ul >
117
+ </ section >
118
+ ) ;
119
+ }
205
120
}
0 commit comments