Skip to content

Commit 4cc0114

Browse files
committed
add create card instance command
1 parent 49377b2 commit 4cc0114

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

Diff for: packages/base/command.gts

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class WriteTextFileInput extends CardDef {
5858
}
5959

6060
export class CreateInstanceInput extends CardDef {
61+
@field parent = contains(JsonField);
6162
@field module = contains(CodeRefField);
6263
@field realm = contains(StringField);
6364
}

Diff for: packages/host/app/commands/create-card-json.ts renamed to packages/host/app/commands/create-card-instance.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { type LooseSingleCardDocument } from '@cardstack/runtime-common';
22

3+
import { getCard } from '@cardstack/host/resources/card-resource';
4+
5+
import type { CardDef } from 'https://cardstack.com/base/card-api';
36
import type * as BaseCommandModule from 'https://cardstack.com/base/command';
47

58
import HostBaseCommand from '../lib/host-base-command';
69

7-
export default class CreateCardJsonCommand extends HostBaseCommand<
8-
typeof BaseCommandModule.CreateInstanceInput
10+
export default class CreateCardInstanceCommand extends HostBaseCommand<
11+
typeof BaseCommandModule.CreateInstanceInput,
12+
typeof CardDef | undefined
913
> {
10-
description = `Create a new card json document given a card ref and realm.`;
14+
description = `Create a new card instance given a card ref and realm.`;
1115

1216
async getInputType() {
1317
let commandModule = await this.loadCommandModule();
@@ -17,8 +21,8 @@ export default class CreateCardJsonCommand extends HostBaseCommand<
1721

1822
protected async run(
1923
input: BaseCommandModule.CreateInstanceInput,
20-
): Promise<LooseSingleCardDocument> {
21-
if (!input.module || !input.realm) {
24+
): Promise<CardDef | undefined> {
25+
if (!input.parent || !input.module || !input.realm) {
2226
throw new Error(
2327
"Create instance command can't run because it doesn't have all the fields in arguments returned by open ai",
2428
);
@@ -31,6 +35,10 @@ export default class CreateCardJsonCommand extends HostBaseCommand<
3135
},
3236
},
3337
};
34-
return doc;
38+
let cardResource = getCard(input.parent, () => doc, {
39+
isAutoSave: () => true,
40+
});
41+
await cardResource.loaded; // this await should not be necessary when card-resource is refactored
42+
return cardResource.card;
3543
}
3644
}

Diff for: packages/host/app/commands/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { VirtualNetwork } from '@cardstack/runtime-common';
33
import * as AddFieldToCardDefinitionCommandModule from './add-field-to-card-definition';
44
import * as AddSkillsToRoomCommandModule from './add-skills-to-room';
55
import * as CopyCardCommandModule from './copy-card';
6-
import * as CreateCardJsonCommandModule from './create-card-json';
76
import * as CreateAIAssistantRoomCommandModule from './create-ai-assistant-room';
7+
import * as CreateCardInstanceCommandModule from './create-card-instance';
88
import * as OpenAiAssistantRoomCommandModule from './open-ai-assistant-room';
99
import * as PatchCardCommandModule from './patch-card';
1010
import * as ReloadCardCommandModule from './reload-card';
@@ -31,8 +31,8 @@ export function shimHostCommands(virtualNetwork: VirtualNetwork) {
3131
CopyCardCommandModule,
3232
);
3333
virtualNetwork.shimModule(
34-
'@cardstack/boxel-host/commands/create-card-json',
35-
CreateCardJsonCommandModule,
34+
'@cardstack/boxel-host/commands/create-card-instance',
35+
CreateCardInstanceCommandModule,
3636
);
3737
virtualNetwork.shimModule(
3838
'@cardstack/boxel-host/commands/create-ai-assistant-room',

Diff for: packages/host/app/components/operator-mode/code-submode/playground-panel.gts

+7-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { action } from '@ember/object';
44
import type Owner from '@ember/owner';
55
import { service } from '@ember/service';
66
import Component from '@glimmer/component';
7-
import { tracked } from '@glimmer/tracking';
87

98
import Folder from '@cardstack/boxel-icons/folder';
109
import { task } from 'ember-concurrency';
@@ -31,11 +30,10 @@ import {
3130
cardTypeIcon,
3231
chooseCard,
3332
type Query,
34-
type LooseSingleCardDocument,
3533
type ResolvedCodeRef,
3634
} from '@cardstack/runtime-common';
3735

38-
import CreateCardJsonCommand from '@cardstack/host/commands/create-card-json';
36+
import CreateCardInstanceCommand from '@cardstack/host/commands/create-card-instance';
3937

4038
import { getCard } from '@cardstack/host/resources/card-resource';
4139
import { getCodeRef, type CardType } from '@cardstack/host/resources/card-type';
@@ -382,7 +380,6 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
382380
@service private declare realm: RealmService;
383381
@service private declare realmServer: RealmServerService;
384382
@service declare recentFilesService: RecentFilesService;
385-
@tracked newCardJSON: LooseSingleCardDocument | undefined;
386383
private playgroundSelections: Record<
387384
string, // moduleId
388385
{ cardId: string; format: Format }
@@ -435,8 +432,7 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
435432

436433
private cardResource = getCard(
437434
this,
438-
() =>
439-
this.newCardJSON ?? this.playgroundSelections[this.args.moduleId]?.cardId,
435+
() => this.playgroundSelections[this.args.moduleId]?.cardId,
440436
{ isAutoSave: () => true },
441437
);
442438

@@ -483,7 +479,6 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
483479
}
484480

485481
private persistSelections = (cardId: string, format = this.format) => {
486-
this.newCardJSON = undefined;
487482
this.playgroundSelections[this.args.moduleId] = { cardId, format };
488483
window.localStorage.setItem(
489484
PlaygroundSelections,
@@ -514,17 +509,16 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
514509
}
515510
});
516511

517-
// TODO: convert this to @action once we no longer need to await below
518512
private createNew = task(async () => {
519513
let { commandContext } = this.commandService;
520-
this.newCardJSON = await new CreateCardJsonCommand(commandContext).execute({
514+
let card = await new CreateCardInstanceCommand(commandContext).execute({
515+
parent: this,
521516
module: this.args.codeRef,
522517
realm: this.operatorModeStateService.realmURL.href,
523518
});
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
519+
if (card) {
520+
this.recentFilesService.addRecentFileUrl(`${card.id}.json`);
521+
this.persistSelections(card.id, 'edit'); // open new instance in playground in edit format
528522
}
529523
});
530524

0 commit comments

Comments
 (0)