11import { css , html } from "lit" ;
22import { property , state } from "lit/decorators.js" ;
33
4- import { AppState } from "../lib/app/mod.ts" ;
4+ import { AppState , AppView } from "../lib/app/mod.ts" ;
55import { BaseView } from "./BaseView.ts" ;
66import { KeyStore } from "@commontools/identity" ;
77import { RuntimeInternals } from "../lib/runtime.ts" ;
88import { DebuggerController } from "../lib/debugger-controller.ts" ;
99import "./DebuggerView.ts" ;
1010import { Task } from "@lit/task" ;
11- import { CharmController } from "@commontools/charm/ops" ;
11+ import { CharmController , CharmsController } from "@commontools/charm/ops" ;
1212import { CellEventTarget , CellUpdateEvent } from "../lib/cell-event-target.ts" ;
1313import { NAME } from "@commontools/runner" ;
1414import { type NameSchema , nameSchema } from "@commontools/charm" ;
@@ -129,45 +129,6 @@ export class XAppView extends BaseView {
129129 this . hasSidebarContent = event . detail . hasSidebarContent ;
130130 } ;
131131
132- // Maps the app level view to a specific charm to load
133- // as the primary, active charm.
134- _activeCharmId = new Task ( this , {
135- task : async ( [ app , rt ] ) : Promise < string | undefined > => {
136- if ( ! app || ! rt ) {
137- return ;
138- }
139- if ( "builtin" in app . view ) {
140- if ( app . view . builtin !== "home" ) {
141- console . warn ( "Unsupported view type" ) ;
142- return ;
143- }
144- {
145- await rt . cc ( ) . manager ( ) . synced ( ) ;
146- const pattern = await PatternFactory . getPattern (
147- rt . cc ( ) . getAllCharms ( ) ,
148- "home" ,
149- ) ;
150- if ( pattern ) {
151- return pattern . id ;
152- }
153- }
154- const pattern = await PatternFactory . create (
155- rt . cc ( ) ,
156- "home" ,
157- ) ;
158- return pattern . id ;
159- } else if ( "spaceDid" in app . view ) {
160- console . warn ( "Unsupported view type" ) ;
161- } else if ( "spaceName" in app . view ) {
162- // eventually, this should load the default pattern
163- // for a space if needed, but for now is handled
164- // in BodyView, and only set the active charm ID
165- // for explicit charms set in the URL.
166- return app . view . charmId ;
167- }
168- } ,
169- args : ( ) => [ this . app , this . rt ] ,
170- } ) ;
171132 private handleCellWatch = ( e : Event ) => {
172133 const event = e as CustomEvent < { cell : unknown ; label ?: string } > ;
173134 const { cell, label } = event . detail ;
@@ -192,30 +153,42 @@ export class XAppView extends BaseView {
192153 } ;
193154
194155 // Do not make private, integration tests access this directly.
195- _activeCharm = new Task ( this , {
196- task : async ( [ activeCharmId ] ) : Promise < CharmController | undefined > => {
197- if ( ! this . rt || ! this . app || ! activeCharmId ) {
156+ //
157+ // This fetches the active pattern and space default pattern derived
158+ // from the current view.
159+ _activePatterns = new Task ( this , {
160+ task : async (
161+ [ app , rt ] ,
162+ ) : Promise <
163+ | { activePattern : CharmController ; defaultPattern : CharmController }
164+ | undefined
165+ > => {
166+ if ( ! app || ! rt ) {
198167 this . #setTitleSubscription( ) ;
199168 return ;
200169 }
201- const current : CharmController | undefined = this . _activeCharm . value ;
202- if (
203- current && current . id === activeCharmId
204- ) {
205- return current ;
206- }
207- const activeCharm = await this . rt . cc ( ) . get (
208- activeCharmId ,
209- true ,
210- nameSchema ,
170+
171+ const patterns = await viewToPatterns (
172+ rt . cc ( ) ,
173+ app . view ,
174+ this . _activePatterns . value ?. activePattern ,
211175 ) ;
176+ if ( ! patterns ) {
177+ this . #setTitleSubscription( ) ;
178+ return ;
179+ }
180+
212181 // Record the charm as recently accessed so recents stay fresh.
213- await this . rt . cc ( ) . manager ( ) . trackRecentCharm ( activeCharm . getCell ( ) ) ;
214- this . #setTitleSubscription( activeCharm ) ;
182+ await rt . cc ( ) . manager ( ) . trackRecentCharm (
183+ patterns . activePattern . getCell ( ) ,
184+ ) ;
185+ this . #setTitleSubscription(
186+ patterns . activePattern as CharmController < NameSchema > ,
187+ ) ;
215188
216- return activeCharm ;
189+ return patterns ;
217190 } ,
218- args : ( ) => [ this . _activeCharmId . value ] ,
191+ args : ( ) => [ this . app , this . rt ] ,
219192 } ) ;
220193
221194 #setTitleSubscription( activeCharm ?: CharmController < NameSchema > ) {
@@ -280,10 +253,14 @@ export class XAppView extends BaseView {
280253 const unauthenticated = html `
281254 < x-login-view .keyStore ="${ this . keyStore } "> </ x-login-view >
282255 ` ;
256+ const patterns = this . _activePatterns . value ;
257+ const activePattern = patterns ?. activePattern ;
258+ const defaultPattern = patterns ?. defaultPattern ;
283259 const authenticated = html `
284260 < x-body-view
285261 .rt ="${ this . rt } "
286- .activeCharm ="${ this . _activeCharm . value } "
262+ .activeCharm ="${ activePattern } "
263+ .defaultCharm ="${ defaultPattern } "
287264 .showShellCharmListView ="${ app . showShellCharmListView ?? false } "
288265 .showSidebar ="${ app . showSidebar ?? false } "
289266 > </ x-body-view >
@@ -301,7 +278,7 @@ export class XAppView extends BaseView {
301278 .rt ="${ this . rt } "
302279 .keyStore ="${ this . keyStore } "
303280 .charmTitle ="${ this . charmTitle } "
304- .charmId ="${ this . _activeCharmId . value } "
281+ .charmId ="${ activePattern ?. id } "
305282 .showShellCharmListView ="${ app . showShellCharmListView ?? false } "
306283 .showDebuggerView ="${ app . showDebuggerView ?? false } "
307284 .showSidebar ="${ app . showSidebar ?? false } "
@@ -328,4 +305,49 @@ export class XAppView extends BaseView {
328305 }
329306}
330307
308+ async function viewToPatterns (
309+ cc : CharmsController ,
310+ view : AppView ,
311+ currentActive ?: CharmController < unknown > ,
312+ ) : Promise <
313+ {
314+ activePattern : CharmController < unknown > ;
315+ defaultPattern : CharmController < unknown > ;
316+ } | undefined
317+ > {
318+ if ( "builtin" in view ) {
319+ if ( view . builtin !== "home" ) {
320+ console . warn ( "Unsupported view type" ) ;
321+ return ;
322+ }
323+ const pattern = await PatternFactory . getOrCreate ( cc , "home" ) ;
324+ return { activePattern : pattern , defaultPattern : pattern } ;
325+ } else if ( "spaceDid" in view ) {
326+ console . warn ( "Unsupported view type" ) ;
327+ return ;
328+ } else if ( "spaceName" in view ) {
329+ const defaultPattern = await PatternFactory . getOrCreate (
330+ cc ,
331+ "space-default" ,
332+ ) ;
333+
334+ let activePattern : CharmController < unknown > | undefined ;
335+ // If viewing a specific charm, use it as active; otherwise use default
336+ if ( view . charmId ) {
337+ if ( currentActive && currentActive . id === view . charmId ) {
338+ activePattern = currentActive ;
339+ } else {
340+ activePattern = await cc . get (
341+ view . charmId ,
342+ true ,
343+ nameSchema ,
344+ ) ;
345+ }
346+ } else {
347+ activePattern = defaultPattern ;
348+ }
349+ return { activePattern, defaultPattern } ;
350+ }
351+ }
352+
331353globalThis . customElements . define ( "x-app-view" , XAppView ) ;
0 commit comments