@@ -44,7 +44,7 @@ import ProfileSelect from "./ProfileSelect"
44
44
import ProfileWatcher from "../tray/watchers/profile/list"
45
45
import ProfileStatusWatcher from "../tray/watchers/profile/status"
46
46
import UpdateFunction from "../tray/update"
47
- import { handleReset } from "../controller/profile/actions"
47
+ import { handleNew , handleDelete , handleReset } from "../controller/profile/actions"
48
48
49
49
import "../../web/scss/components/Dashboard/Description.scss"
50
50
import "../../web/scss/components/ProfileExplorer/_index.scss"
@@ -91,16 +91,37 @@ export default class ProfileExplorer extends React.PureComponent<Props, State> {
91
91
} ) )
92
92
}
93
93
94
- private readonly _handleProfileSelection = ( selectedProfile : string ) => {
95
- this . setState ( { selectedProfile } )
94
+ /** If given `null`, then we will attempt to use the lastUsed profile */
95
+ private readonly _handleProfileSelection = ( selectedProfile : string | null ) => {
96
+ if ( ! selectedProfile && this . state . profiles ) {
97
+ // last used, excluding the currently selected profile
98
+ const lastUsed = this . lastUsed ( this . state . profiles . filter ( ( _ ) => _ . name !== this . state . selectedProfile ) )
99
+ if ( lastUsed ) {
100
+ selectedProfile = lastUsed . name
101
+ }
102
+ }
103
+
104
+ if ( selectedProfile ) {
105
+ this . setState ( { selectedProfile } )
96
106
97
- if ( this . props . onSelectProfile ) {
98
- this . props . onSelectProfile ( selectedProfile )
107
+ if ( this . props . onSelectProfile ) {
108
+ this . props . onSelectProfile ( selectedProfile )
109
+ }
99
110
}
100
111
}
101
112
102
113
private updateDebouncer : null | ReturnType < typeof setTimeout > = null
103
114
115
+ private lastUsed ( profiles : Profiles . Profile [ ] ) {
116
+ return profiles . slice ( 1 ) . reduce ( ( lastUsed , profile ) => {
117
+ if ( lastUsed . lastUsedTime < profile . lastUsedTime ) {
118
+ return profile
119
+ } else {
120
+ return lastUsed
121
+ }
122
+ } , profiles [ 0 ] )
123
+ }
124
+
104
125
private readonly profileWatcherUpdateFn = ( ) => {
105
126
if ( this . updateDebouncer ) {
106
127
clearTimeout ( this . updateDebouncer )
@@ -122,13 +143,7 @@ export default class ProfileExplorer extends React.PureComponent<Props, State> {
122
143
let selectedProfile = curState . selectedProfile
123
144
if ( ! curState || ! curState . profiles || curState . profiles . length === 0 ) {
124
145
// use the last-used profile by default
125
- const newSelectedProfile = profiles . slice ( 1 ) . reduce ( ( lastUsed , profile ) => {
126
- if ( lastUsed . lastUsedTime < profile . lastUsedTime ) {
127
- return profile
128
- } else {
129
- return lastUsed
130
- }
131
- } , profiles [ 0 ] )
146
+ const newSelectedProfile = this . lastUsed ( profiles )
132
147
133
148
// also emit an initial profile selection event
134
149
if ( newSelectedProfile ) {
@@ -223,17 +238,14 @@ type ProfileCardProps = Partial<Diff> &
223
238
Pick < Props , "onSelectGuidebook" > & {
224
239
profile : string
225
240
profiles : Profiles . Profile [ ]
226
- onSelectProfile : ( profile : string ) => void
241
+ onSelectProfile : ( profile : string | null ) => void
227
242
228
243
profileReadiness : string
229
244
profileStatus : ProfileStatusWatcher
230
245
}
231
246
232
247
type ProfileCardState = {
233
248
isOpen : boolean
234
-
235
- /** Clear any "just changed" indicators after a brief delay */
236
- // clearDiff?: ReturnType<typeof setTimeout>
237
249
}
238
250
239
251
class ProfileCard extends React . PureComponent < ProfileCardProps , ProfileCardState > {
@@ -243,7 +255,24 @@ class ProfileCard extends React.PureComponent<ProfileCardProps, ProfileCardState
243
255
isOpen : false ,
244
256
}
245
257
}
246
- private readonly _handleReset = ( ) => handleReset ( this . props . profile )
258
+
259
+ /** Create new profile */
260
+ private readonly _handleNew = async ( ) => {
261
+ if ( this . props . profile ) {
262
+ const newProfile = await handleNew ( this . props . profile , this . props . profiles )
263
+ this . props . onSelectProfile ( newProfile )
264
+ }
265
+ }
266
+
267
+ /** Delete selected profile */
268
+ private readonly _handleDelete = async ( ) => {
269
+ if ( this . props . profile ) {
270
+ await handleDelete ( this . props . profile )
271
+ this . props . onSelectProfile ( null )
272
+ }
273
+ }
274
+
275
+ private readonly _handleReset = ( ) => this . props . profile && handleReset ( this . props . profile )
247
276
// private readonly _handleBoot = () => handleBoot(this.props.profile)
248
277
// private readonly _handleShutdown = () => handleShutdown(this.props.profile)
249
278
private readonly _onToggle = ( ) => this . setState ( { isOpen : ! this . state . isOpen } )
@@ -454,7 +483,7 @@ class ProfileCard extends React.PureComponent<ProfileCardProps, ProfileCardState
454
483
return < TreeView hasGuides defaultAllExpanded data = { data } variant = "compactNoBackground" />
455
484
}
456
485
457
- return "Internal Error"
486
+ return < Loading />
458
487
}
459
488
460
489
private sort ( data : TreeViewDataItem [ ] ) {
@@ -472,11 +501,24 @@ class ProfileCard extends React.PureComponent<ProfileCardProps, ProfileCardState
472
501
private footer ( ) {
473
502
return (
474
503
< Flex >
475
- < FlexItem flex = { { default : "flex_1" } } > </ FlexItem >
504
+ < FlexItem flex = { { default : "flex_1" } } >
505
+ < Tooltip content = "Create a new profile" >
506
+ < Button variant = "control" className = "codeflare--profile-explorer--new-btn" onClick = { this . _handleNew } >
507
+ < Icons icon = "PlusSquare" />
508
+ </ Button >
509
+ </ Tooltip >
510
+ </ FlexItem >
476
511
< FlexItem >
477
- < Button variant = "link" isSmall className = "codeflare--profile-explorer--reset-btn" onClick = { this . _handleReset } >
478
- Reset
479
- </ Button >
512
+ < Tooltip content = "Reset the choices in this profile" >
513
+ < Button variant = "link" className = "codeflare--profile-explorer--reset-btn" onClick = { this . _handleReset } >
514
+ Reset
515
+ </ Button >
516
+ </ Tooltip >
517
+ < Tooltip content = "Delete this profile" >
518
+ < Button variant = "link" className = "codeflare--profile-explorer--delete-btn" onClick = { this . _handleDelete } >
519
+ < Icons icon = "Trash" />
520
+ </ Button >
521
+ </ Tooltip >
480
522
</ FlexItem >
481
523
{ /*<FlexItem>
482
524
<Button variant="link" isSmall className="codeflare--profile-explorer--boot-btn" onClick={this._handleBoot}>
0 commit comments