Skip to content

Commit a831098

Browse files
authored
feat: Add home space to shell when on the root path (commontoolsinc#2170)
1 parent 62e0329 commit a831098

File tree

6 files changed

+88
-43
lines changed

6 files changed

+88
-43
lines changed

packages/shell/src/lib/default-pattern.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { CharmController, CharmsController } from "@commontools/charm/ops";
2+
import { HttpProgramResolver } from "@commontools/js-compiler";
3+
import { API_URL } from "./env.ts";
4+
5+
export type BuiltinPatternType = "home" | "space-default";
6+
7+
type BuiltinPatternConfig = {
8+
url: URL;
9+
cause: string;
10+
name: string;
11+
};
12+
13+
const Configs: Record<BuiltinPatternType, BuiltinPatternConfig> = {
14+
"home": {
15+
name: "Home",
16+
url: new URL(`/api/patterns/home.tsx`, API_URL),
17+
cause: "home-pattern",
18+
},
19+
"space-default": {
20+
name: "DefaultCharmList",
21+
url: new URL(`/api/patterns/default-app.tsx`, API_URL),
22+
cause: "default-charm",
23+
},
24+
};
25+
26+
export async function create(
27+
cc: CharmsController,
28+
type: BuiltinPatternType,
29+
): Promise<CharmController> {
30+
const config = Configs[type];
31+
const manager = cc.manager();
32+
const runtime = manager.runtime;
33+
34+
const program = await cc.manager().runtime.harness.resolve(
35+
new HttpProgramResolver(config.url.href),
36+
);
37+
38+
const charm = await cc.create(program, undefined, config.cause);
39+
40+
// Wait for the link to be processed
41+
await runtime.idle();
42+
await manager.synced();
43+
44+
if (type === "space-default") {
45+
// Link the default pattern to the space cell
46+
await manager.linkDefaultPattern(charm.getCell());
47+
}
48+
49+
return charm;
50+
}
51+
52+
export function getPattern(
53+
charms: CharmController[],
54+
type: BuiltinPatternType,
55+
): CharmController | undefined {
56+
const config = Configs[type];
57+
return charms.find((c) => {
58+
const name = c.name();
59+
return name && name.startsWith(config.name);
60+
});
61+
}

packages/shell/src/lib/runtime.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ export class RuntimeInternals extends EventTarget {
106106
): Promise<RuntimeInternals> {
107107
let session;
108108
let spaceName;
109-
if (typeof view === "string") {
110-
switch (view) {
109+
if ("builtin" in view) {
110+
switch (view.builtin) {
111111
case "home":
112112
session = await createSession({ identity, spaceDid: identity.did() });
113113
spaceName = "<home>";

packages/shell/src/views/AppView.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { navigate, updatePageTitle } from "../lib/navigate.ts";
1616
import { provide } from "@lit/context";
1717
import { KeyboardRouter } from "../lib/keyboard-router.ts";
1818
import { keyboardRouterContext } from "@commontools/ui";
19+
import * as PatternFactory from "../lib/pattern-factory.ts";
1920

2021
export class XAppView extends BaseView {
2122
static override styles = css`
@@ -126,12 +127,30 @@ export class XAppView extends BaseView {
126127
// Maps the app level view to a specific charm to load
127128
// as the primary, active charm.
128129
_activeCharmId = new Task(this, {
129-
task: ([app, rt]): string | undefined => {
130+
task: async ([app, rt]): Promise<string | undefined> => {
130131
if (!app || !rt) {
131132
return;
132133
}
133134
if ("builtin" in app.view) {
134-
console.warn("Unsupported view type");
135+
if (app.view.builtin !== "home") {
136+
console.warn("Unsupported view type");
137+
return;
138+
}
139+
{
140+
await rt.cc().manager().synced();
141+
const pattern = await PatternFactory.getPattern(
142+
rt.cc().getAllCharms(),
143+
"home",
144+
);
145+
if (pattern) {
146+
return pattern.id;
147+
}
148+
}
149+
const pattern = await PatternFactory.create(
150+
rt.cc(),
151+
"home",
152+
);
153+
return pattern.id;
135154
} else if ("spaceDid" in app.view) {
136155
console.warn("Unsupported view type");
137156
} else if ("spaceName" in app.view) {

packages/shell/src/views/BodyView.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Task } from "@lit/task";
44
import { BaseView } from "./BaseView.ts";
55
import { RuntimeInternals } from "../lib/runtime.ts";
66
import { CharmController } from "@commontools/charm/ops";
7-
import * as DefaultPattern from "../lib/default-pattern.ts";
7+
import * as PatternFactory from "../lib/pattern-factory.ts";
88
import "../components/OmniLayout.ts";
99

1010
export class XBodyView extends BaseView {
@@ -79,7 +79,7 @@ export class XBodyView extends BaseView {
7979

8080
this.creatingDefaultPattern = true;
8181
try {
82-
await DefaultPattern.create(this.rt.cc());
82+
await PatternFactory.create(this.rt.cc(), "space-default");
8383
} catch (error) {
8484
console.error("Could not create default pattern:", error);
8585
// Re-throw to expose errors instead of swallowing them
@@ -117,7 +117,7 @@ export class XBodyView extends BaseView {
117117
}
118118

119119
const defaultPattern = charms
120-
? DefaultPattern.getDefaultPattern(charms)
120+
? PatternFactory.getPattern(charms, "space-default")
121121
: undefined;
122122
const activeCharm = this.activeCharm;
123123

packages/shell/src/views/RootView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class XRootView extends BaseView {
4242
// Non-private for typing in `updated()` callback
4343
_app = {
4444
apiUrl: API_URL,
45-
view: { spaceName: "common-knowledge" },
45+
view: { builtin: "home" },
4646
} as AppState;
4747

4848
@property()

0 commit comments

Comments
 (0)