Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .brain/areas/web.svx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ sources:

## What it does

`apps/web` is the reader site. It is one Foldkit program.
Foldkit is a UI framework on Effect: a Model, Messages, an `update` function, and a pure view.
`apps/web` is the reader site. [Foldkit](/lore/a-browser-send-follows-one-path) renders and hydrates most routes.
The HTML pages at `/news`, `/news/latest`, and `/directory` render and hydrate their content with [Effect UI](https://ui.effect.website/article.md) inside the reader shell.
Foldkit is a UI framework on [Effect](/lore/effect-basics): a Model, Messages, an `update` function, and a pure view.

Server rendering means the Worker builds the page HTML for each request.
Prerendering means the build writes page HTML to files before deployment.
Expand Down
6 changes: 3 additions & 3 deletions .brain/data/git-snapshot.json

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions apps/web/effect-ui.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { Schema } from "effect";
import type { Plugin } from "vite";

const entries = {
"directory-effect-ui": "src/client/directory-effect-ui/entry.ts",
"news-effect-ui": "src/client/news-effect-ui/entry.ts",
};

const manifestFile = ".vite/manifest.json";

const assetModule = "virtual:reader-effect-ui-assets";

const Manifest = Schema.Record(
Schema.String,
Schema.Struct({
css: Schema.optionalKey(Schema.Array(Schema.String)),
file: Schema.String,
imports: Schema.optionalKey(Schema.Array(Schema.String)),
isEntry: Schema.optionalKey(Schema.Boolean),
})
);

const decodeManifest = Schema.decodeUnknownSync(
Schema.fromJsonString(Manifest)
);

export const manifestAssets = (
manifest: typeof Manifest.Type,
entry: string,
base: string
) => {
const input = manifest[entry];

if (input?.isEntry !== true) {
throw new Error(
`Missing Effect UI client entry for ${entry}. Fix: check \`effectUiPlugin\` client inputs.`
);
}

const visited = new Set([entry]);
const modulePreloads = new Set<string>();
const stylesheets = new Set(input.css);

const visit = (imports: readonly string[]) => {
for (const key of imports) {
if (visited.has(key)) {
continue;
}

const chunk = manifest[key];

if (chunk === undefined) {
throw new Error(
`Effect UI entry ${entry} imports missing manifest chunk ${key}. Fix: rebuild \`@rat-stack/web\`.`
);
}

visited.add(key);
modulePreloads.add(chunk.file);

for (const href of chunk.css ?? []) {
stylesheets.add(href);
}

visit(chunk.imports ?? []);
}
};

visit(input.imports ?? []);

const assetUrl = (file: string) => `${base}${file}`;

return {
entryScript: assetUrl(input.file),
modulePreloads: [...modulePreloads].map(assetUrl),
stylesheets: [...stylesheets].map(assetUrl),
};
};

export const effectUiPlugin = (): Plugin => {
let builtAssets:
| Record<string, ReturnType<typeof manifestAssets>>
| undefined;

return {
config: () => ({
environments: {
client: {
build: {
manifest: manifestFile,
rolldownOptions: {
input: Object.fromEntries(
Object.entries(entries).map(([name, path]) => [
name,
new URL(path, import.meta.url).pathname,
])
),
},
},
},
},
}),
generateBundle: {
order: "post",
handler(_options, bundle) {
if (this.environment.name !== "client") {
return;
}

const asset = bundle[manifestFile];

if (asset?.type !== "asset") {
this.error(
"Missing Vite client manifest. Fix: enable `build.manifest` in the client environment."
);
}

const { base } = this.environment.config;
const manifest = decodeManifest(asset.source);

builtAssets = Object.fromEntries(
Object.entries(entries).map(([name, path]) => [
name,
manifestAssets(manifest, path, base),
])
);

delete bundle[".vite/manifest.json"];
},
},
load(id) {
if (id !== `\0${assetModule}`) {
return null;
}

const assets =
this.environment.config.command === "serve"
? Object.fromEntries(
Object.entries(entries).map(([name, path]) => [
name,
{
entryScript: `/${path}`,
modulePreloads: [],
stylesheets: [],
},
])
)
: builtAssets;

if (assets === undefined) {
this.error(
"Effect UI server build has no client manifest. Fix: run the full `@rat-stack/web` build so its client environment finishes before SSR."
);
}

return `export default ${JSON.stringify(assets)}`;
},
name: "reader-effect-ui",
resolveId(id) {
return id === assetModule ? `\0${id}` : undefined;
},
sharedDuringBuild: true,
};
};
96 changes: 0 additions & 96 deletions apps/web/news-effect-ui.ts

This file was deleted.

26 changes: 26 additions & 0 deletions apps/web/src/client/directory-effect-ui/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Component } from "@effect/ui";
import { Html, HtmlCommand } from "@effect/ui/html";

import { view } from "../../features/directory-effect-ui/view.js";
import {
DirectoryProps,
DirectoryMessage,
SelectedLetters,
firstLetterToggle,
toggleLetter,
} from "./model.js";

export const DirectoryPage = Html.component({
name: "DirectoryPage",
props: DirectoryProps,
model: SelectedLetters,
message: DirectoryMessage,
}).make({
init: ({ props }) => props.selected,
update: {
ClearedStackLetters: ($) =>
$.set([]).run(HtmlCommand.focus(firstLetterToggle, () => Component.noop)),
ToggledStackLetter: ({ model }, { letter }) => toggleLetter(model, letter),
},
view: (h, selected) => view(h, h.props.projects, selected),
});
11 changes: 11 additions & 0 deletions apps/web/src/client/directory-effect-ui/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Effect } from "effect";

import { installReaderTools } from "../web-mcp-boot.js";
import { hydrateDirectory } from "./hydration.js";

import "../../../../site/src/rat.css";
import "../reader.css";

installReaderTools();

Effect.runFork(hydrateDirectory(document));
57 changes: 57 additions & 0 deletions apps/web/src/client/directory-effect-ui/hydration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { HtmlHydrate } from "@effect/ui/html";
import { Cause, Effect, Schema } from "effect";

import { DirectoryPage } from "./component.js";
import {
DirectoryHandoff,
directoryHandoffId,
directoryRootId,
} from "./model.js";

const decodeHandoff = Schema.decodeEffect(
Schema.fromJsonString(DirectoryHandoff)
);

export const hydrateDirectory = (page: Document) =>
Effect.suspend(() => {
const container = page.querySelector<HTMLDivElement>(`#${directoryRootId}`);
const payload = page.querySelector(`#${directoryHandoffId}`);

if (container === null || payload === null) {
return Effect.void;
}

return decodeHandoff(payload.textContent ?? "").pipe(
Effect.flatMap(({ props, state }) =>
Effect.gen(function* adoptDirectory() {
const root = yield* HtmlHydrate.hydrateEffect(
DirectoryPage,
props,
container,
state
);

yield* root.adopted;
container.dataset.hydrated = "true";

return yield* Effect.never;
})
)
);
}).pipe(
Effect.scoped,
Effect.tapCause((cause) => {
if (Cause.hasInterruptsOnly(cause)) {
return Effect.void;
}

if (Cause.hasFails(cause)) {
return Effect.logError("Directory hydration failed: invalid handoff");
}

return Effect.logError("Directory hydration failed", cause);
}),
Effect.catchCause((cause) =>
Cause.hasInterruptsOnly(cause) ? Effect.interrupt : Effect.void
)
);
Loading
Loading