55
66import { type PartitionKeyDefinition } from '@azure/cosmos' ;
77import { callWithTelemetryAndErrorHandling } from '@microsoft/vscode-azext-utils' ;
8+ import * as l10n from '@vscode/l10n' ;
89import * as crypto from 'crypto' ;
910import * as vscode from 'vscode' ;
10-
11- import * as l10n from '@vscode/l10n' ;
1211import { getCosmosDBClientByConnection , getCosmosDBKeyCredential } from '../cosmosdb/getCosmosClient' ;
1312import { type NoSqlQueryConnection } from '../cosmosdb/NoSqlCodeLensProvider' ;
1413import { DocumentSession } from '../cosmosdb/session/DocumentSession' ;
@@ -19,13 +18,23 @@ import {
1918 type SerializedQueryResult ,
2019} from '../cosmosdb/types/queryResult' ;
2120import { getNoSqlQueryConnection } from '../cosmosdb/utils/NoSqlQueryConnection' ;
21+ import { StorageNames , StorageService , type StorageItem } from '../services/storageService' ;
2222import { queryMetricsToCsv , queryResultToCsv } from '../utils/csvConverter' ;
2323import { getIsSurveyDisabledGlobally , openSurvey , promptAfterActionEventually } from '../utils/survey' ;
2424import { ExperienceKind , UsageImpact } from '../utils/surveyTypes' ;
2525import * as vscodeUtil from '../utils/vscodeUtils' ;
2626import { BaseTab , type CommandPayload } from './BaseTab' ;
2727import { DocumentTab } from './DocumentTab' ;
2828
29+ const QUERY_HISTORY_SIZE = 10 ;
30+ const HISTORY_STORAGE_KEY = 'ms-azuretools.vscode-cosmosdb.history' ;
31+
32+ type HistoryItem = StorageItem & {
33+ properties : {
34+ history : string [ ] ;
35+ } ;
36+ } ;
37+
2938export class QueryEditorTab extends BaseTab {
3039 public static readonly title = 'Query Editor' ;
3140 public static readonly viewType = 'cosmosDbQuery' ;
@@ -98,6 +107,7 @@ export class QueryEditorTab extends BaseTab {
98107
99108 this . channel . on < void > ( 'ready' , async ( ) => {
100109 await this . updateConnection ( this . connection ) ;
110+ await this . updateQueryHistory ( ) ;
101111 if ( this . query ) {
102112 await this . channel . postMessage ( {
103113 type : 'event' ,
@@ -168,6 +178,8 @@ export class QueryEditorTab extends BaseTab {
168178 ) ;
169179 case 'copyMetricsCSVToClipboard' :
170180 return this . copyMetricsCSVToClipboard ( payload . params [ 0 ] as SerializedQueryResult | null ) ;
181+ case 'updateQueryHistory' :
182+ return this . updateQueryHistory ( payload . params [ 0 ] as string ) ;
171183 }
172184
173185 return super . getCommand ( payload ) ;
@@ -257,6 +269,50 @@ export class QueryEditorTab extends BaseTab {
257269 } ) ;
258270 }
259271
272+ private async updateQueryHistory ( query ?: string ) : Promise < void > {
273+ await callWithTelemetryAndErrorHandling ( 'cosmosDB.nosql.queryEditor.updateQueryHistory' , async ( context ) => {
274+ context . telemetry . suppressIfSuccessful = true ;
275+
276+ if ( ! this . connection ) {
277+ throw new Error ( l10n . t ( 'No connection' ) ) ;
278+ }
279+
280+ const storage = StorageService . get ( StorageNames . Default ) ;
281+ const containerId = `${ this . connection . databaseId } /${ this . connection . containerId } ` ;
282+ const historyItems = ( await storage . getItems ( HISTORY_STORAGE_KEY ) ) as HistoryItem [ ] ;
283+ const historyData = historyItems . find ( ( item ) => item . id === containerId ) ?? {
284+ id : containerId ,
285+ name : containerId ,
286+ properties : {
287+ history : [ ] as string [ ] ,
288+ } ,
289+ } ;
290+
291+ // First remove any existing occurrences of this query
292+ const queryHistory = historyData . properties . history . filter ( ( item ) => item !== query ) ;
293+
294+ // Add the new query to the beginning (most recent first)
295+ if ( query ) {
296+ queryHistory . unshift ( query ) ;
297+ }
298+
299+ // Trim to max size if needed
300+ if ( queryHistory . length > QUERY_HISTORY_SIZE ) {
301+ queryHistory . length = QUERY_HISTORY_SIZE ;
302+ }
303+
304+ historyData . properties . history = queryHistory ;
305+ await storage . push ( HISTORY_STORAGE_KEY , historyData ) ;
306+
307+ // Update the webview with the new history
308+ await this . channel . postMessage ( {
309+ type : 'event' ,
310+ name : 'updateQueryHistory' ,
311+ params : [ historyData . properties . history ] ,
312+ } ) ;
313+ } ) ;
314+ }
315+
260316 private async duplicateTab ( text : string ) : Promise < void > {
261317 await callWithTelemetryAndErrorHandling ( 'cosmosDB.nosql.queryEditor.duplicateTab' , ( ) => {
262318 QueryEditorTab . render ( this . connection , this . panel . viewColumn , false , text ) ;
0 commit comments