11import { createRequestActionTypes , createApiRequest } from '../utils' ;
22import '../../services/api' ;
3+ import { getValueFromLS , parseJson } from '../../utils/utils' ;
4+ import { QUERIES_HISTORY_KEY } from '../../utils/constants' ;
5+
6+ const MAXIMUM_QUERIES_IN_HISTORY = 20 ;
37
48const SEND_QUERY = createRequestActionTypes ( 'query' , 'SEND_QUERY' ) ;
59const CHANGE_USER_INPUT = 'query/CHANGE_USER_INPUT' ;
@@ -9,6 +13,10 @@ const GO_TO_NEXT_QUERY = 'query/GO_TO_NEXT_QUERY';
913const SELECT_RUN_ACTION = 'query/SELECT_RUN_ACTION' ;
1014const MONACO_HOT_KEY = 'query/MONACO_HOT_KEY' ;
1115
16+ const queriesHistoryInitial = parseJson ( getValueFromLS ( QUERIES_HISTORY_KEY , [ ] ) ) ;
17+
18+ const sliceLimit = queriesHistoryInitial . length - MAXIMUM_QUERIES_IN_HISTORY ;
19+
1220export const RUN_ACTIONS_VALUES = {
1321 script : 'execute-script' ,
1422 scan : 'execute-scan' ,
@@ -25,7 +33,7 @@ const initialState = {
2533 loading : false ,
2634 input : '' ,
2735 history : {
28- queries : [ ] ,
36+ queries : queriesHistoryInitial . slice ( sliceLimit < 0 ? 0 : sliceLimit ) ,
2937 currentIndex : - 1 ,
3038 } ,
3139 runAction : RUN_ACTIONS_VALUES . script ,
@@ -76,7 +84,10 @@ const executeQuery = (state = initialState, action) => {
7684
7785 case SAVE_QUERY_TO_HISTORY : {
7886 const query = action . data ;
79- const newQueries = [ ...state . history . queries , query ] ;
87+ const newQueries = [ ...state . history . queries , query ] . slice (
88+ state . history . queries . length >= MAXIMUM_QUERIES_IN_HISTORY ? 1 : 0 ,
89+ ) ;
90+ window . localStorage . setItem ( QUERIES_HISTORY_KEY , JSON . stringify ( newQueries ) ) ;
8091 const currentIndex = newQueries . length - 1 ;
8192
8293 return {
0 commit comments