@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 172 lines 5.16 KB
Raw
1 import { Effect, Schema, pipe } from "effect";
2 import { Command, Runtime, type Update } from "foldkit";
3 import { type Document, type Html, type HtmlBuilder } from "foldkit/html";
4 import { defineMessageUnion } from "foldkit/message";
5 import { UrlRequest, load, pushUrl } from "foldkit/navigation";
6 import { modifyFields } from "foldkit/struct";
7 import { Url, toString as urlToString } from "foldkit/url";
8 import { AppRoute, aboutRouter, counterRouter, homeRouter, urlToAppRoute } from "./route";
9
10 // MODEL
11
12 export const Model = Schema.Struct({
13 route: AppRoute,
14 count: Schema.Number,
15 });
16 export type Model = typeof Model.Type;
17
18 // FLAGS
19
20 export const Flags = Schema.Struct({ initialCount: Schema.Number });
21 export type Flags = typeof Flags.Type;
22
23 // MESSAGE
24
25 export const Message = defineMessageUnion({
26 ClickedIncrement: {},
27 ClickedLink: { request: UrlRequest },
28 ChangedUrl: { url: Url },
29 CompletedNavigateInternal: {},
30 CompletedLoadExternal: {},
31 });
32
33 export type Message = typeof Message.Type;
34
35 // INIT
36
37 export const init: Runtime.RoutingApplicationInit<Model, Message, Flags> = (flags, url) => ({
38 model: { route: urlToAppRoute(url), count: flags.initialCount },
39 });
40
41 // COMMAND
42
43 const NavigateInternal = Command.define("NavigateInternal", {
44 args: { url: Schema.String },
45 messages: [Message.CompletedNavigateInternal],
46 execute: ({ url }) => pushUrl(url).pipe(Effect.as(Message.CompletedNavigateInternal())),
47 });
48
49 const LoadExternal = Command.define("LoadExternal", {
50 args: { href: Schema.String },
51 messages: [Message.CompletedLoadExternal],
52 execute: ({ href }) => load(href).pipe(Effect.as(Message.CompletedLoadExternal())),
53 });
54
55 // UPDATE
56
57 type UpdateReturn = Update.Return<Model, Message>;
58
59 export const update = (model: Model, message: Message) =>
60 Message.match<UpdateReturn>(message, {
61 ClickedIncrement: () => ({
62 model: modifyFields(model, { count: (count) => count + 1 }),
63 }),
64 ClickedLink: ({ request }) =>
65 UrlRequest.match<UpdateReturn>(request, {
66 Internal: ({ url }) => ({
67 model,
68 commands: [NavigateInternal({ url: urlToString(url) })],
69 }),
70 External: ({ href }) => ({
71 model,
72 commands: [LoadExternal({ href })],
73 }),
74 }),
75 ChangedUrl: ({ url }) => ({
76 model: modifyFields(model, { route: () => urlToAppRoute(url) }),
77 }),
78 CompletedNavigateInternal: () => ({ model }),
79 CompletedLoadExternal: () => ({ model }),
80 });
81
82 // VIEW
83
84 const APP_NAME = "Foldkit App";
85
86 const appendAppName = (page: string): string => `${page} | ${APP_NAME}`;
87
88 const routeTitle = (route: AppRoute): string =>
89 pipe(
90 AppRoute.match(route, {
91 Home: () => "Home",
92 About: () => "About",
93 Counter: () => "Counter",
94 NotFound: () => "Not Found",
95 }),
96 appendAppName,
97 );
98
99 const navigationView = (h: HtmlBuilder<Message>): Html =>
100 h.nav(
101 [h.Class("flex gap-4")],
102 [
103 h.a([h.Href(homeRouter()), h.Class("underline")], ["Home"]),
104 h.a([h.Href(aboutRouter()), h.Class("underline")], ["About"]),
105 h.a([h.Href(counterRouter()), h.Class("underline")], ["Counter"]),
106 ],
107 );
108
109 const pageView = (model: Model, h: HtmlBuilder<Message>): Html =>
110 AppRoute.match(model.route, {
111 Home: () =>
112 h.section(
113 [h.Class("grid gap-4")],
114 [
115 h.h1([h.Id("page-title"), h.Class("text-4xl font-bold")], ["Statically generated home"]),
116 h.p([], ["This route was rendered during the build and hydrated in place."]),
117 h.button(
118 [h.OnClick(Message.ClickedIncrement()), h.Class("w-fit bg-black px-4 py-2 text-white")],
119 [`Count: ${model.count}`],
120 ),
121 ],
122 ),
123 About: () =>
124 h.section(
125 [h.Class("grid gap-4")],
126 [
127 h.h1(
128 [h.Id("page-title"), h.Class("text-4xl font-bold")],
129 ["Statically generated about page"],
130 ),
131 h.p([], ["The same renderPage function produced this route in the same build."]),
132 ],
133 ),
134 Counter: () =>
135 h.section(
136 [h.Class("grid gap-4")],
137 [
138 h.h1([h.Id("page-title"), h.Class("text-4xl font-bold")], ["Request-rendered counter"]),
139 h.p(
140 [],
141 [
142 "Open /counter?count=7 directly or reload it: the count arrives in the HTML, then hydrates in the browser.",
143 ],
144 ),
145 h.p([h.Id("count"), h.Class("text-6xl font-bold")], [model.count.toString()]),
146 h.button(
147 [
148 h.Id("increment"),
149 h.OnClick(Message.ClickedIncrement()),
150 h.Class("w-fit bg-black px-4 py-2 text-white"),
151 ],
152 ["Increment"],
153 ),
154 ],
155 ),
156 NotFound: ({ path }) =>
157 h.section(
158 [h.Class("grid gap-4")],
159 [
160 h.h1([h.Id("page-title"), h.Class("text-4xl font-bold")], ["Not found"]),
161 h.p([], [`No statically generated page exists for ${path}.`]),
162 ],
163 ),
164 });
165
166 export const view = (model: Model, h: HtmlBuilder<Message>): Document => ({
167 title: routeTitle(model.route),
168 body: h.main(
169 [h.Class("mx-auto grid min-h-screen max-w-3xl content-center gap-10 p-8")],
170 [navigationView(h), pageView(model, h)],
171 ),
172 });