11import vscode , { MarkdownString , ThemeIcon , TreeItem , window , workspace } from "vscode" ;
22import { TreeDataProvider } from "vscode" ;
33import { Config } from "../config" ;
4+ import { QueryHistoryItem } from "../Storage" ;
45
56const openSqlDocumentCommand = `vscode-db2i.openSqlDocument` ;
67
@@ -22,7 +23,7 @@ export class queryHistory implements TreeDataProvider<any> {
2223 vscode . commands . registerCommand ( `vscode-db2i.queryHistory.prepend` , async ( newQuery ?: string ) => {
2324 if ( newQuery && Config . ready ) {
2425 let currentList = Config . getPastQueries ( ) ;
25- const existingQuery = currentList . findIndex ( query => query . trim ( ) === newQuery . trim ( ) ) ;
26+ const existingQuery = currentList . findIndex ( queryItem => queryItem . query . trim ( ) === newQuery . trim ( ) ) ;
2627
2728 // If it exists, remove it
2829 if ( existingQuery > 0 ) {
@@ -31,7 +32,10 @@ export class queryHistory implements TreeDataProvider<any> {
3132
3233 // If it's at the top, don't add it, it's already at the top
3334 if ( existingQuery !== 0 ) {
34- currentList . splice ( 0 , 0 , newQuery ) ;
35+ currentList . splice ( 0 , 0 , {
36+ query : newQuery ,
37+ unix : Math . floor ( Date . now ( ) / 1000 )
38+ } ) ;
3539 }
3640
3741 await Config . setPastQueries ( currentList ) ;
@@ -40,11 +44,13 @@ export class queryHistory implements TreeDataProvider<any> {
4044 }
4145 } ) ,
4246
43- vscode . commands . registerCommand ( `vscode-db2i.queryHistory.remove` , async ( node : PastQuery ) => {
47+ vscode . commands . registerCommand ( `vscode-db2i.queryHistory.remove` , async ( node : PastQueryNode ) => {
4448 if ( node && Config . ready ) {
4549 let currentList = Config . getPastQueries ( ) ;
4650 const chosenQuery = node . query ;
47- const existingQuery = currentList . findIndex ( query => query . trim ( ) === chosenQuery . trim ( ) ) ;
51+ const existingQuery = currentList . findIndex ( queryItem =>
52+ queryItem . query . trim ( ) === chosenQuery . trim ( )
53+ ) ;
4854
4955 // If it exists, remove it
5056 if ( existingQuery >= 0 ) {
@@ -72,17 +78,80 @@ export class queryHistory implements TreeDataProvider<any> {
7278 return element ;
7379 }
7480
75- async getChildren ( ) : Promise < vscode . TreeItem [ ] > {
81+ async getChildren ( timePeriod ?: TimePeriodNode ) : Promise < vscode . TreeItem [ ] > {
7682 if ( Config . ready ) {
77- return Config . getPastQueries ( ) . map ( query => new PastQuery ( query ) ) ;
83+ if ( timePeriod ) {
84+ return timePeriod . getChildren ( ) ;
85+
86+ } else {
87+ const currentList = Config . getPastQueries ( ) ;
88+
89+ const day = 60 * 60 * 24 ;
90+ const week = day * 7 ;
91+ const month = day * 30 ;
92+
93+ const now = Math . floor ( Date . now ( ) / 1000 ) ;
94+ const dayAgo = now - day ;
95+ const weekAgo = now - week ;
96+ const monthAgo = now - month ;
97+
98+ let pastDayQueries : PastQueryNode [ ] = [ ] ;
99+ let pastWeekQueries : PastQueryNode [ ] = [ ] ;
100+ let pastMonthQueries : PastQueryNode [ ] = [ ] ;
101+ let olderQueries : PastQueryNode [ ] = [ ] ;
102+
103+ currentList . forEach ( queryItem => {
104+ // The smaller the unix value, the older it is
105+ if ( queryItem . unix < monthAgo ) {
106+ olderQueries . push ( new PastQueryNode ( queryItem . query ) ) ;
107+ } else if ( queryItem . unix < weekAgo ) {
108+ pastMonthQueries . push ( new PastQueryNode ( queryItem . query ) ) ;
109+ } else if ( queryItem . unix < dayAgo ) {
110+ pastWeekQueries . push ( new PastQueryNode ( queryItem . query ) ) ;
111+ } else {
112+ pastDayQueries . push ( new PastQueryNode ( queryItem . query ) ) ;
113+ }
114+ } ) ;
115+
116+ let nodes : TimePeriodNode [ ] = [ ] ;
117+
118+ if ( pastDayQueries . length > 0 ) {
119+ nodes . push ( new TimePeriodNode ( `Past day` , pastDayQueries , true ) ) ;
120+ }
121+ if ( pastWeekQueries . length > 0 ) {
122+ nodes . push ( new TimePeriodNode ( `Past week` , pastWeekQueries ) ) ;
123+ }
124+ if ( pastMonthQueries . length > 0 ) {
125+ nodes . push ( new TimePeriodNode ( `Past month` , pastMonthQueries ) ) ;
126+ }
127+ if ( olderQueries . length > 0 ) {
128+ nodes . push ( new TimePeriodNode ( `Older` , olderQueries ) ) ;
129+ }
130+
131+ return nodes ;
132+ }
78133
79134 } else {
80135 return [ new TreeItem ( `A connection is required for query history` ) ] ;
81136 }
82137 }
83138}
84139
85- class PastQuery extends vscode . TreeItem {
140+ class TimePeriodNode extends vscode . TreeItem {
141+ constructor ( public period : string , private nodes : PastQueryNode [ ] , expanded = false ) {
142+ super ( period , expanded ? vscode . TreeItemCollapsibleState . Expanded : vscode . TreeItemCollapsibleState . Collapsed ) ;
143+
144+ this . contextValue = `timePeriod` ;
145+
146+ this . iconPath = new ThemeIcon ( `calendar` ) ;
147+ }
148+
149+ getChildren ( ) {
150+ return this . nodes ;
151+ }
152+ }
153+
154+ class PastQueryNode extends vscode . TreeItem {
86155 constructor ( public query : string ) {
87156 super ( query . length > 63 ? query . substring ( 0 , 60 ) + `...` : query ) ;
88157
0 commit comments