@@ -32,33 +32,37 @@ export async function getSessionList(): Promise<ISession[]> {
32
32
return sessions ;
33
33
}
34
34
35
- export async function selectSession ( ) : Promise < void > {
36
- const choice : IQuickItemEx < string > | undefined = await vscode . window . showQuickPick ( parseSessionsToPicks ( true /* includeOperation */ ) ) ;
35
+ export async function manageSessions ( ) : Promise < void > {
36
+ const choice : IQuickItemEx < ISession | string > | undefined = await vscode . window . showQuickPick ( parseSessionsToPicks ( true /* includeOperation */ ) ) ;
37
37
if ( ! choice || choice . description === "Active" ) {
38
38
return ;
39
39
}
40
40
if ( choice . value === ":createSession" ) {
41
- await vscode . commands . executeCommand ( "leetcode.createSession" ) ;
41
+ await createSession ( ) ;
42
+ return ;
43
+ }
44
+ if ( choice . value === ":deleteSession" ) {
45
+ await deleteSession ( ) ;
42
46
return ;
43
47
}
44
48
try {
45
- await leetCodeExecutor . enableSession ( choice . value ) ;
49
+ await leetCodeExecutor . enableSession ( ( choice . value as ISession ) . id ) ;
46
50
vscode . window . showInformationMessage ( `Successfully switched to session '${ choice . label } '.` ) ;
47
51
await vscode . commands . executeCommand ( "leetcode.refreshExplorer" ) ;
48
52
} catch ( error ) {
49
53
await promptForOpenOutputChannel ( "Failed to switch session. Please open the output channel for details." , DialogType . error ) ;
50
54
}
51
55
}
52
56
53
- async function parseSessionsToPicks ( includeOperations : boolean = false ) : Promise < Array < IQuickItemEx < string > > > {
54
- return new Promise ( async ( resolve : ( res : Array < IQuickItemEx < string > > ) => void ) : Promise < void > => {
57
+ async function parseSessionsToPicks ( includeOperations : boolean = false ) : Promise < Array < IQuickItemEx < ISession | string > > > {
58
+ return new Promise ( async ( resolve : ( res : Array < IQuickItemEx < ISession | string > > ) => void ) : Promise < void > => {
55
59
try {
56
60
const sessions : ISession [ ] = await getSessionList ( ) ;
57
- const picks : Array < IQuickItemEx < string > > = sessions . map ( ( s : ISession ) => Object . assign ( { } , {
61
+ const picks : Array < IQuickItemEx < ISession | string > > = sessions . map ( ( s : ISession ) => Object . assign ( { } , {
58
62
label : `${ s . active ? "$(check) " : "" } ${ s . name } ` ,
59
63
description : s . active ? "Active" : "" ,
60
64
detail : `AC Questions: ${ s . acQuestions } , AC Submits: ${ s . acSubmits } ` ,
61
- value : s . id ,
65
+ value : s ,
62
66
} ) ) ;
63
67
64
68
if ( includeOperations ) {
@@ -85,7 +89,7 @@ function parseSessionManagementOperations(): Array<IQuickItemEx<string>> {
85
89
} ] ;
86
90
}
87
91
88
- export async function createSession ( ) : Promise < void > {
92
+ async function createSession ( ) : Promise < void > {
89
93
const session : string | undefined = await vscode . window . showInputBox ( {
90
94
prompt : "Enter the new session name." ,
91
95
validateInput : ( s : string ) : string | undefined => s && s . trim ( ) ? undefined : "Session name must not be empty" ,
@@ -101,6 +105,43 @@ export async function createSession(): Promise<void> {
101
105
}
102
106
}
103
107
108
+ async function deleteSession ( ) : Promise < void > {
109
+ const choice : IQuickItemEx < ISession | string > | undefined = await vscode . window . showQuickPick (
110
+ parseSessionsToPicks ( false /* includeOperation */ ) ,
111
+ { placeHolder : "Please select the session you want to delete" } ,
112
+ ) ;
113
+ if ( ! choice ) {
114
+ return ;
115
+ }
116
+
117
+ const selectedSession : ISession = choice . value as ISession ;
118
+ if ( selectedSession . active ) {
119
+ vscode . window . showInformationMessage ( "Cannot delete an active session." ) ;
120
+ return ;
121
+ }
122
+
123
+ const action : string | undefined = await vscode . window . showWarningMessage ( `This operation cannot be reverted. Are you sure to delete the session: ${ selectedSession . name } ?` , "Yes" ) ;
124
+ if ( action !== "Yes" ) {
125
+ return ;
126
+ }
127
+
128
+ const confirm : string | undefined = await vscode . window . showInputBox ( {
129
+ prompt : "Enter 'yes' to confirm deleting the session" ,
130
+ validateInput : ( value : string ) : string => {
131
+ if ( value === "yes" ) {
132
+ return "" ;
133
+ } else {
134
+ return "Enter 'yes' to confirm" ;
135
+ }
136
+ } ,
137
+ } ) ;
138
+
139
+ if ( confirm === "yes" ) {
140
+ await leetCodeExecutor . deleteSession ( selectedSession . id ) ;
141
+ vscode . window . showInformationMessage ( "The session has been successfully deleted." ) ;
142
+ }
143
+ }
144
+
104
145
export interface ISession {
105
146
active : boolean ;
106
147
id : string ;
0 commit comments