55 * LICENSE file in the root directory of this source tree.
66 */
77
8- import { parse } from 'graphql' ;
98import React from 'react' ;
10- import QueryStore , { QueryStoreItem } from '../utility/QueryStore' ;
9+ import { QueryStoreItem } from '../utility/QueryStore' ;
1110import HistoryQuery , {
1211 HandleEditLabelFn ,
13- HandleToggleFavoriteFn ,
1412 HandleSelectQueryFn ,
13+ HandleToggleFavoriteFn ,
1514} from './HistoryQuery' ;
1615import 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' ;
6117
6218type QueryHistoryProps = {
6319 query ?: string ;
@@ -67,6 +23,7 @@ type QueryHistoryProps = {
6723 queryID ?: number ;
6824 onSelectQuery : HandleSelectQueryFn ;
6925 storage : StorageAPI ;
26+ maxHistoryLength : number ;
7027} ;
7128
7229type QueryHistoryState = {
@@ -77,129 +34,87 @@ export class QueryHistory extends React.Component<
7734 QueryHistoryProps ,
7835 QueryHistoryState
7936> {
80- historyStore : QueryStore ;
81- favoriteStore : QueryStore ;
37+ historyStore : HistoryStore ;
8238
8339 constructor ( props : QueryHistoryProps ) {
8440 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 ,
8944 ) ;
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 ;
9546 this . state = { queries } ;
9647 }
9748
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 = (
12450 query ?: string ,
12551 variables ?: string ,
12652 headers ?: string ,
12753 operationName ?: string ,
12854 ) => {
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 } ) ;
15057 } ;
15158
152- // Public API
153- toggleFavorite : HandleToggleFavoriteFn = (
59+ onHandleEditLabel : HandleEditLabelFn = (
15460 query ,
15561 variables ,
15662 headers ,
15763 operationName ,
15864 label ,
15965 favorite ,
16066 ) => {
161- const item : QueryStoreItem = {
67+ this . historyStore . editLabel (
16268 query ,
16369 variables ,
16470 headers ,
16571 operationName ,
16672 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 } ) ;
17876 } ;
17977
180- // Public API
181- editLabel : HandleEditLabelFn = (
78+ onToggleFavorite : HandleToggleFavoriteFn = (
18279 query ,
18380 variables ,
18481 headers ,
18582 operationName ,
18683 label ,
18784 favorite ,
18885 ) => {
189- const item = {
86+ this . historyStore . toggleFavorite (
19087 query ,
19188 variables ,
19289 headers ,
19390 operationName ,
19491 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 } ) ;
20495 } ;
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+ }
205120}
0 commit comments