6
6
import { Disposable } from 'vs/base/common/lifecycle' ;
7
7
import { URI } from 'vs/base/common/uri' ;
8
8
import { localize } from 'vs/nls' ;
9
+ import { Action2 , MenuId , registerAction2 } from 'vs/platform/actions/common/actions' ;
9
10
import { IEnvironmentService } from 'vs/platform/environment/common/environment' ;
10
11
import { IFileService } from 'vs/platform/files/common/files' ;
11
12
import { ILogService } from 'vs/platform/log/common/log' ;
12
13
import { IProductService } from 'vs/platform/product/common/productService' ;
13
- import { IQuickInputService , IQuickPickItem } from 'vs/platform/quickinput/common/quickInput' ;
14
+ import { IQuickInputService , IQuickPickItem , IQuickPickSeparator } from 'vs/platform/quickinput/common/quickInput' ;
14
15
import { IRequestService } from 'vs/platform/request/common/request' ;
15
16
import { IStorageService , IStorageValueChangeEvent , StorageScope , StorageTarget } from 'vs/platform/storage/common/storage' ;
16
17
import { IAuthenticationProvider } from 'vs/platform/userDataSync/common/userDataSync' ;
17
18
import { UserDataSyncStoreClient } from 'vs/platform/userDataSync/common/userDataSyncStoreService' ;
18
19
import { AuthenticationSession , AuthenticationSessionsChangeEvent , IAuthenticationService } from 'vs/workbench/services/authentication/common/authentication' ;
19
20
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions' ;
20
- import { EditSession , ISessionSyncWorkbenchService } from 'vs/workbench/services/sessionSync/common/sessionSync' ;
21
+ import { EditSession , EDIT_SESSION_SYNC_TITLE , ISessionSyncWorkbenchService } from 'vs/workbench/services/sessionSync/common/sessionSync' ;
22
+
23
+ type ExistingSession = IQuickPickItem & { session : AuthenticationSession & { providerId : string } } ;
24
+ type AuthenticationProviderOption = IQuickPickItem & { provider : IAuthenticationProvider } ;
21
25
22
26
export class SessionSyncWorkbenchService extends Disposable implements ISessionSyncWorkbenchService {
23
27
@@ -49,6 +53,8 @@ export class SessionSyncWorkbenchService extends Disposable implements ISessionS
49
53
50
54
// If another window changes the preferred session storage, reset our cached auth state in memory
51
55
this . _register ( this . storageService . onDidChangeValue ( e => this . onDidChangeStorage ( e ) ) ) ;
56
+
57
+ this . registerResetAuthenticationAction ( ) ;
52
58
}
53
59
54
60
/**
@@ -58,7 +64,7 @@ export class SessionSyncWorkbenchService extends Disposable implements ISessionS
58
64
async write ( editSession : EditSession ) : Promise < void > {
59
65
this . initialized = await this . waitAndInitialize ( ) ;
60
66
if ( ! this . initialized ) {
61
- throw new Error ( 'Unable to store edit session.' ) ;
67
+ throw new Error ( 'Please sign in to store your edit session.' ) ;
62
68
}
63
69
64
70
await this . storeClient ?. write ( 'editSessions' , JSON . stringify ( editSession ) , null ) ;
@@ -71,7 +77,7 @@ export class SessionSyncWorkbenchService extends Disposable implements ISessionS
71
77
async read ( ) : Promise < EditSession | undefined > {
72
78
this . initialized = await this . waitAndInitialize ( ) ;
73
79
if ( ! this . initialized ) {
74
- throw new Error ( 'Unable to apply latest edit session.' ) ;
80
+ throw new Error ( 'Please sign in to apply your latest edit session.' ) ;
75
81
}
76
82
77
83
// Pull latest session data from service
@@ -134,30 +140,57 @@ export class SessionSyncWorkbenchService extends Disposable implements ISessionS
134
140
* Prompts the user to pick an authentication option for storing and getting edit sessions.
135
141
*/
136
142
private async getAccountPreference ( ) : Promise < AuthenticationSession & { providerId : string } | undefined > {
137
- const quickpick = this . quickInputService . createQuickPick < IQuickPickItem & { session : AuthenticationSession & { providerId : string } } > ( ) ;
143
+ const quickpick = this . quickInputService . createQuickPick < ExistingSession | AuthenticationProviderOption > ( ) ;
138
144
quickpick . title = localize ( 'account preference' , 'Edit Sessions' ) ;
139
145
quickpick . ok = false ;
140
146
quickpick . placeholder = localize ( 'choose account placeholder' , "Select an account to sign in" ) ;
141
147
quickpick . ignoreFocusOut = true ;
142
- // TODO@joyceerhl Should we be showing sessions here?
143
- quickpick . items = await this . getAllSessions ( ) ;
148
+ quickpick . items = await this . createQuickpickItems ( ) ;
144
149
145
150
return new Promise ( ( resolve , reject ) => {
146
- quickpick . onDidHide ( ( e ) => quickpick . dispose ( ) ) ;
147
- quickpick . onDidAccept ( ( e ) => {
148
- resolve ( quickpick . selectedItems [ 0 ] . session ) ;
151
+ quickpick . onDidHide ( ( e ) => {
152
+ resolve ( undefined ) ;
153
+ quickpick . dispose ( ) ;
154
+ } ) ;
155
+
156
+ quickpick . onDidAccept ( async ( e ) => {
157
+ const selection = quickpick . selectedItems [ 0 ] ;
158
+ const session = 'provider' in selection ? { ...await this . authenticationService . createSession ( selection . provider . id , selection . provider . scopes ) , providerId : selection . provider . id } : selection . session ;
159
+ resolve ( session ) ;
149
160
quickpick . hide ( ) ;
150
161
} ) ;
162
+
151
163
quickpick . show ( ) ;
152
164
} ) ;
153
165
}
154
166
167
+ private async createQuickpickItems ( ) : Promise < ( ExistingSession | AuthenticationProviderOption | IQuickPickSeparator ) [ ] > {
168
+ const options : ( ExistingSession | AuthenticationProviderOption | IQuickPickSeparator ) [ ] = [ ] ;
169
+
170
+ options . push ( { type : 'separator' , label : localize ( 'signed in' , "Signed In" ) } ) ;
171
+
172
+ const sessions = await this . getAllSessions ( ) ;
173
+ options . push ( ...sessions ) ;
174
+
175
+ options . push ( { type : 'separator' , label : localize ( 'others' , "Others" ) } ) ;
176
+
177
+ for ( const authenticationProvider of ( await this . getAuthenticationProviders ( ) ) ) {
178
+ const signedInForProvider = sessions . some ( account => account . session . providerId === authenticationProvider . id ) ;
179
+ if ( ! signedInForProvider || this . authenticationService . supportsMultipleAccounts ( authenticationProvider . id ) ) {
180
+ const providerName = this . authenticationService . getLabel ( authenticationProvider . id ) ;
181
+ options . push ( { label : localize ( 'sign in using account' , "Sign in with {0}" , providerName ) , provider : authenticationProvider } ) ;
182
+ }
183
+ }
184
+
185
+ return options ;
186
+ }
187
+
155
188
/**
156
189
*
157
190
* Returns all authentication sessions available from {@link getAuthenticationProviders}.
158
191
*/
159
192
private async getAllSessions ( ) {
160
- const options = [ ] ;
193
+ const options : ExistingSession [ ] = [ ] ;
161
194
const authenticationProviders = await this . getAuthenticationProviders ( ) ;
162
195
163
196
for ( const provider of authenticationProviders ) {
@@ -226,11 +259,34 @@ export class SessionSyncWorkbenchService extends Disposable implements ISessionS
226
259
}
227
260
}
228
261
262
+ private clearAuthenticationPreference ( ) : void {
263
+ this . #authenticationInfo = undefined ;
264
+ this . initialized = false ;
265
+ this . existingSessionId = undefined ;
266
+ }
267
+
229
268
private onDidChangeSessions ( e : AuthenticationSessionsChangeEvent ) : void {
230
269
if ( this . #authenticationInfo?. sessionId && e . removed . find ( session => session . id === this . #authenticationInfo?. sessionId ) ) {
231
- this . #authenticationInfo = undefined ;
232
- this . existingSessionId = undefined ;
233
- this . initialized = false ;
270
+ this . clearAuthenticationPreference ( ) ;
234
271
}
235
272
}
273
+
274
+ private registerResetAuthenticationAction ( ) {
275
+ const that = this ;
276
+ this . _register ( registerAction2 ( class ResetEditSessionAuthenticationAction extends Action2 {
277
+ constructor ( ) {
278
+ super ( {
279
+ id : 'workbench.sessionSync.actions.resetAuth' ,
280
+ title : localize ( 'reset auth' , '{0}: Reset Authentication State' , EDIT_SESSION_SYNC_TITLE ) ,
281
+ menu : {
282
+ id : MenuId . CommandPalette ,
283
+ }
284
+ } ) ;
285
+ }
286
+
287
+ run ( ) {
288
+ that . clearAuthenticationPreference ( ) ;
289
+ }
290
+ } ) ) ;
291
+ }
236
292
}
0 commit comments