The best way to control Claude Code, Codex and more from any computer or phone
| 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, 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 | // MESSAGE |
| 19 | |
| 20 | export const Message = defineMessageUnion({ |
| 21 | ClickedIncrement: {}, |
| 22 | ClickedLink: { request: UrlRequest }, |
| 23 | ChangedUrl: { url: Url }, |
| 24 | CompletedNavigateInternal: {}, |
| 25 | CompletedLoadExternal: {}, |
| 26 | }); |
| 27 | |
| 28 | export type Message = typeof Message.Type; |
| 29 | |
| 30 | // INIT |
| 31 | |
| 32 | export const init: Runtime.RoutingApplicationInit<Model, Message> = (url) => ({ |
| 33 | model: { route: urlToAppRoute(url), count: 0 }, |
| 34 | }); |
| 35 | |
| 36 | // COMMAND |
| 37 | |
| 38 | const NavigateInternal = Command.define("NavigateInternal", { |
| 39 | args: { url: Schema.String }, |
| 40 | messages: [Message.CompletedNavigateInternal], |
| 41 | execute: ({ url }) => pushUrl(url).pipe(Effect.as(Message.CompletedNavigateInternal())), |
| 42 | }); |
| 43 | |
| 44 | const LoadExternal = Command.define("LoadExternal", { |
| 45 | args: { href: Schema.String }, |
| 46 | messages: [Message.CompletedLoadExternal], |
| 47 | execute: ({ href }) => load(href).pipe(Effect.as(Message.CompletedLoadExternal())), |
| 48 | }); |
| 49 | |
| 50 | // UPDATE |
| 51 | |
| 52 | type UpdateReturn = Update.Return<Model, Message>; |
| 53 | |
| 54 | export const update = (model: Model, message: Message) => |
| 55 | Message.match<UpdateReturn>(message, { |
| 56 | ClickedIncrement: () => ({ |
| 57 | model: modifyFields(model, { count: (count) => count + 1 }), |
| 58 | }), |
| 59 | ClickedLink: ({ request }) => |
| 60 | UrlRequest.match<UpdateReturn>(request, { |
| 61 | Internal: ({ url }) => ({ |
| 62 | model, |
| 63 | commands: [NavigateInternal({ url: urlToString(url) })], |
| 64 | }), |
| 65 | External: ({ href }) => ({ |
| 66 | model, |
| 67 | commands: [LoadExternal({ href })], |
| 68 | }), |
| 69 | }), |
| 70 | ChangedUrl: ({ url }) => ({ |
| 71 | model: modifyFields(model, { route: () => urlToAppRoute(url) }), |
| 72 | }), |
| 73 | CompletedNavigateInternal: () => ({ model }), |
| 74 | CompletedLoadExternal: () => ({ model }), |
| 75 | }); |
| 76 | |
| 77 | // VIEW |
| 78 | |
| 79 | const APP_NAME = "Foldkit App"; |
| 80 | |
| 81 | const appendAppName = (page: string): string => `${page} | ${APP_NAME}`; |
| 82 | |
| 83 | const routeTitle = (route: AppRoute): string => |
| 84 | pipe( |
| 85 | AppRoute.match(route, { |
| 86 | Home: () => "Home", |
| 87 | About: () => "About", |
| 88 | NotFound: () => "Not Found", |
| 89 | }), |
| 90 | appendAppName, |
| 91 | ); |
| 92 | |
| 93 | const navigationView = (h: HtmlBuilder<Message>): Html => |
| 94 | h.nav( |
| 95 | [h.Class("flex gap-4")], |
| 96 | [ |
| 97 | h.a([h.Href(homeRouter()), h.Class("underline")], ["Home"]), |
| 98 | h.a([h.Href(aboutRouter()), h.Class("underline")], ["About"]), |
| 99 | ], |
| 100 | ); |
| 101 | |
| 102 | const pageView = (model: Model, h: HtmlBuilder<Message>): Html => |
| 103 | AppRoute.match(model.route, { |
| 104 | Home: () => |
| 105 | h.section( |
| 106 | [h.Class("grid gap-4")], |
| 107 | [ |
| 108 | h.h1([h.Id("page-title"), h.Class("text-4xl font-bold")], ["Statically generated home"]), |
| 109 | h.p([], ["This route was rendered during the build and hydrated in place."]), |
| 110 | h.button( |
| 111 | [h.OnClick(Message.ClickedIncrement()), h.Class("w-fit bg-black px-4 py-2 text-white")], |
| 112 | [`Count: ${model.count}`], |
| 113 | ), |
| 114 | ], |
| 115 | ), |
| 116 | About: () => |
| 117 | h.section( |
| 118 | [h.Class("grid gap-4")], |
| 119 | [ |
| 120 | h.h1( |
| 121 | [h.Id("page-title"), h.Class("text-4xl font-bold")], |
| 122 | ["Statically generated about page"], |
| 123 | ), |
| 124 | h.p([], ["The same renderPage function produced this route in the same build."]), |
| 125 | ], |
| 126 | ), |
| 127 | NotFound: ({ path }) => |
| 128 | h.section( |
| 129 | [h.Class("grid gap-4")], |
| 130 | [ |
| 131 | h.h1([h.Id("page-title"), h.Class("text-4xl font-bold")], ["Not found"]), |
| 132 | h.p([], [`No statically generated page exists for ${path}.`]), |
| 133 | ], |
| 134 | ), |
| 135 | }); |
| 136 | |
| 137 | export const view = (model: Model, h: HtmlBuilder<Message>): Document => ({ |
| 138 | title: routeTitle(model.route), |
| 139 | body: h.main( |
| 140 | [h.Class("mx-auto grid min-h-screen max-w-3xl content-center gap-10 p-8")], |
| 141 | [navigationView(h), pageView(model, h)], |
| 142 | ), |
| 143 | }); |