|
| 1 | +import { css, html } from "lit"; |
| 2 | +import { property, state } from "lit/decorators.js"; |
| 3 | +import { BaseView } from "../views/BaseView.ts"; |
| 4 | +import { CharmsController } from "@commontools/charm/ops"; |
| 5 | +import { Task } from "@lit/task"; |
| 6 | +import * as DefaultRecipe from "../lib/default-recipe.ts"; |
| 7 | + |
| 8 | +type CharmData = { |
| 9 | + name: string; |
| 10 | + id: string; |
| 11 | + href: string; |
| 12 | +}; |
| 13 | + |
| 14 | +export class XSpaceElement extends BaseView { |
| 15 | + static override styles = css` |
| 16 | + :host { |
| 17 | + display: block; |
| 18 | + width: 100%; |
| 19 | + height: 100%; |
| 20 | + background-color: white; |
| 21 | + padding: 1rem; |
| 22 | + } |
| 23 | + `; |
| 24 | + |
| 25 | + @property({ attribute: false }) |
| 26 | + cc?: CharmsController; |
| 27 | + |
| 28 | + @state() |
| 29 | + showCharmList = false; |
| 30 | + |
| 31 | + @state() |
| 32 | + creatingDefaultRecipe = false; |
| 33 | + |
| 34 | + private _charms = new Task(this, { |
| 35 | + task: async ([cc]) => { |
| 36 | + if (!cc) return undefined; |
| 37 | + |
| 38 | + // Ensure charms are synced before checking |
| 39 | + const manager = cc.manager(); |
| 40 | + await manager.synced(); |
| 41 | + return await cc.getAllCharms(); |
| 42 | + }, |
| 43 | + args: () => [this.cc], |
| 44 | + }); |
| 45 | + |
| 46 | + async onRequestDefaultRecipe(e: Event) { |
| 47 | + e.preventDefault(); |
| 48 | + if (this.creatingDefaultRecipe) { |
| 49 | + return; |
| 50 | + } |
| 51 | + if (!this.cc) { |
| 52 | + throw new Error( |
| 53 | + "Cannot create default recipe without a charms controller.", |
| 54 | + ); |
| 55 | + } |
| 56 | + this.creatingDefaultRecipe = true; |
| 57 | + try { |
| 58 | + await DefaultRecipe.create(this.cc); |
| 59 | + } catch (e) { |
| 60 | + console.error(`Could not create default recipe: ${e}`); |
| 61 | + } finally { |
| 62 | + this.creatingDefaultRecipe = false; |
| 63 | + this._charms.run(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + onViewToggle(e: Event) { |
| 68 | + e.preventDefault(); |
| 69 | + this.showCharmList = !this.showCharmList; |
| 70 | + } |
| 71 | + |
| 72 | + override render() { |
| 73 | + const spaceName = this.cc ? this.cc.manager().getSpaceName() : undefined; |
| 74 | + const charms = this._charms.value; |
| 75 | + const defaultRecipe = charms |
| 76 | + ? DefaultRecipe.getDefaultRecipe(charms) |
| 77 | + : undefined; |
| 78 | + |
| 79 | + const inner = !charms |
| 80 | + ? html` |
| 81 | + <x-spinner></x-spinner> |
| 82 | + ` |
| 83 | + : this.showCharmList |
| 84 | + ? html` |
| 85 | + <x-charm-list .charms="${charms}" .spaceName="${spaceName}"></x-charm-list> |
| 86 | + ` |
| 87 | + : !defaultRecipe |
| 88 | + ? (this.creatingDefaultRecipe |
| 89 | + ? html` |
| 90 | + <div> |
| 91 | + <span>Creating default recipe...</span> |
| 92 | + <x-spinner></x-spinner> |
| 93 | + </div> |
| 94 | + ` |
| 95 | + : html` |
| 96 | + <div> |
| 97 | + <span>Create default recipe?</span> |
| 98 | + <button @click="${this.onRequestDefaultRecipe}">Go!</button> |
| 99 | + </div> |
| 100 | + `) |
| 101 | + // TBD if we want to use x-charm or ct-render directly here |
| 102 | + : html` |
| 103 | + <x-charm .charmId="${defaultRecipe.id}" .cc="${this.cc}"></x-charm> |
| 104 | + `; |
| 105 | + |
| 106 | + return html` |
| 107 | + <div> |
| 108 | + <button @click="${this.onViewToggle}">${this.showCharmList |
| 109 | + ? "show default" |
| 110 | + : "show list"}</button> |
| 111 | + ${inner} |
| 112 | + </div> |
| 113 | + `; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +globalThis.customElements.define("x-space", XSpaceElement); |
0 commit comments