@@ -4,6 +4,7 @@ import { action } from '@ember/object';
4
4
import type Owner from ' @ember/owner' ;
5
5
import { service } from ' @ember/service' ;
6
6
import Component from ' @glimmer/component' ;
7
+ import { tracked } from ' @glimmer/tracking' ;
7
8
8
9
import Folder from ' @cardstack/boxel-icons/folder' ;
9
10
import { task } from ' ember-concurrency' ;
@@ -94,7 +95,7 @@ const SelectedItem: TemplateOnlyComponent<{ Args: { title?: string } }> =
94
95
< /style >
95
96
</template >;
96
97
97
- const BeforeOptions: TemplateOnlyComponent <{ Args : {} }> = <template >
98
+ const BeforeOptions: TemplateOnlyComponent = <template >
98
99
<div class =' before-options' >
99
100
<span class =' title' >
100
101
Recent
@@ -378,6 +379,7 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
378
379
@service private declare realm: RealmService ;
379
380
@service private declare realmServer: RealmServerService ;
380
381
@service declare recentFilesService: RecentFilesService ;
382
+ @tracked newCardJSON: LooseSingleCardDocument | undefined ;
381
383
private playgroundSelections: Record <
382
384
string , // moduleId
383
385
{ cardId: string ; format: Format }
@@ -430,7 +432,8 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
430
432
431
433
private cardResource = getCard (
432
434
this ,
433
- () => this .playgroundSelections [this .args .moduleId ]?.cardId ,
435
+ () =>
436
+ this .newCardJSON ?? this .playgroundSelections [this .args .moduleId ]?.cardId ,
434
437
{ isAutoSave : () => true },
435
438
);
436
439
@@ -477,6 +480,7 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
477
480
}
478
481
479
482
private persistSelections = (cardId : string , format = this .format ) => {
483
+ this .newCardJSON = undefined ;
480
484
this .playgroundSelections [this .args .moduleId ] = { cardId , format };
481
485
window .localStorage .setItem (
482
486
PlaygroundSelections ,
@@ -507,29 +511,20 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
507
511
}
508
512
});
509
513
514
+ // TODO: convert this to @action once we no longer need to await below
510
515
private createNew = task (async () => {
511
- let ref = this .args .codeRef ;
512
- let realmURL = this .operatorModeStateService .realmURL .href ; // creates in current workspace
513
- let doc: LooseSingleCardDocument = {
516
+ this .newCardJSON = {
514
517
data: {
515
518
meta: {
516
- adoptsFrom: ref ,
517
- realmURL ,
519
+ adoptsFrom: this . args . codeRef ,
520
+ realmURL: this . operatorModeStateService . realmURL . href ,
518
521
},
519
522
},
520
523
};
521
- try {
522
- let card = await this .cardService .createFromSerialized (doc .data , doc );
523
- if (! card ) {
524
- throw new Error (
525
- ` Failed to create card from ref "${ref .name }" from "${ref .module }" ` ,
526
- );
527
- }
528
- await this .cardService .saveModel (card );
529
- this .recentFilesService .addRecentFileUrl (` ${card .id }.json ` );
530
- this .persistSelections (card .id , ' edit' ); // open new instance in playground in edit format
531
- } catch (e : any ) {
532
- console .log (' Error saving' , e );
524
+ await this .cardResource .loaded ; // TODO: remove await when card-resource is refactored
525
+ if (this .card ) {
526
+ this .recentFilesService .addRecentFileUrl (` ${this .card .id }.json ` );
527
+ this .persistSelections (this .card .id , ' edit' ); // open new instance in playground in edit format
533
528
}
534
529
});
535
530
0 commit comments