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
13 changes: 12 additions & 1 deletion dprint.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
{
"$schema": "https://dprint.dev/schemas/v0.json",
"incremental": false,
"incremental": true,
"includes": ["**/*.{ts,tsx,js,jsx,json,md}"],
"indentWidth": 2,
"lineWidth": 120,
"newLineKind": "lf",
"exec": {
"cwd": "${configDir}",
"associations": ["**/examples/ui/**/*.{ts,tsx}", "!**/*.test.{ts,tsx}"],
"commands": [{
"command": "node scripts/stack-chains.mjs {{ext}}",
"associations": ["**/examples/ui/**/*.{ts,tsx}"],
"cacheKeyFiles": ["scripts/stack-chains.mjs"]
}]
},
"typescript": {
"associations": ["**/*.{ts,tsx,mts,cts,js,jsx,mjs,cjs}"],
"semiColons": "asi",
"quoteStyle": "alwaysDouble",
"trailingCommas": "never",
Expand Down Expand Up @@ -38,6 +48,7 @@
"!.vscode/**/*"
],
"plugins": [
"https://plugins.dprint.dev/exec-0.9.0.json@939c28cce4022e2676ac9596585448c16abee12b8ccb0320a9eb9596ade1c219",
"https://plugins.dprint.dev/typescript-0.93.4.wasm",
"https://plugins.dprint.dev/markdown-0.20.0.wasm",
"https://plugins.dprint.dev/json-0.21.1.wasm"
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ pnpm vitest run --project examples/ui examples/ui/routing/

## Formatting

The examples follow the repository's dprint configuration. `launch` has its own `dprint.json` with a 70-column line width, so the code it shows in the article fits its code windows.
The examples follow the repository's dprint configuration. dprint also runs `scripts/stack-chains.mjs` over their TypeScript, except `*.test.ts(x)` files: when two or more method calls follow the start of a chain (such as `h.div`), each goes on its own line, so a view reads as a tree. `launch` has its own `dprint.json` with a 70-column line width, so the code it shows in the article fits its code windows.
42 changes: 24 additions & 18 deletions examples/ui/auth/src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,17 @@ export const AppNoDeps = Html.component({
$.set(AppModel.LoggedIn({ route: LoggedInRoute.Dashboard(), session }))
.run(
SaveSession.make({ session }),
AppNavigation.Dashboard.replace().pipe(Command.asNoop)
AppNavigation.Dashboard
.replace()
.pipe(Command.asNoop)
),

SignedOut: ($) =>
$.set(AppModel.LoggedOut({ route: LoggedOutRoute.Home() })).run(
ClearSession.make(),
AppNavigation.Home.replace().pipe(Command.asNoop)
AppNavigation.Home
.replace()
.pipe(Command.asNoop)
),

FailedSaveSession: ($, { error }) => $.run(LogError.make({ entries: ["Failed to save session:", error] })),
Expand All @@ -161,22 +165,24 @@ export const AppNoDeps = Html.component({
],

view: (h, model) =>
h.div.class("min-h-screen bg-gray-100").children(
AppModel.match(model, {
LoggedOut: ({ route }) =>
LoggedOut(h, {
props: { route },
onEmit: {
SignedIn: ({ session }) => AppMessage.SignedIn({ session })
}
}),
LoggedIn: ({ route, session }) =>
LoggedIn(h, {
props: { route, session },
onEmit: { SignedOut: () => AppMessage.SignedOut() }
})
})
)
h.div
.class("min-h-screen bg-gray-100")
.children(
AppModel.match(model, {
LoggedOut: ({ route }) =>
LoggedOut(h, {
props: { route },
onEmit: {
SignedIn: ({ session }) => AppMessage.SignedIn({ session })
}
}),
LoggedIn: ({ route, session }) =>
LoggedIn(h, {
props: { route, session },
onEmit: { SignedOut: () => AppMessage.SignedOut() }
})
})
)
})

export const App = AppNoDeps.pipe(
Expand Down
66 changes: 36 additions & 30 deletions examples/ui/auth/src/LoggedIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ const NavLink = LoggedInDef.fragment(
(h, tag: "Dashboard" | "Settings") => {
const active = tag === h.props.route._tag
return h.li.children(
h.a.attr("href", LoggedInRoute[tag].href())
h.a
.attr("href", LoggedInRoute[tag].href())
.attr("aria-current", active ? "page" : undefined)
.class(
"hover:bg-blue-600 font-medium px-3 py-1 rounded transition",
active && "bg-blue-700/50"
)
.class("hover:bg-blue-600 font-medium px-3 py-1 rounded transition", active && "bg-blue-700/50")
.children(tag)
)
}
Expand All @@ -41,33 +39,41 @@ export const LoggedIn = LoggedInDef.make({

view: (h) => {
const { route, session } = h.props
return h.div.class("min-h-screen").children(
h.nav.class("bg-blue-500 text-white p-4").children(
h.div.class("max-w-4xl mx-auto flex justify-between items-center")
return h.div
.class("min-h-screen")
.children(
h.nav
.class("bg-blue-500 text-white p-4")
.children(
h.ul.class("flex gap-6 list-none").children(
NavLink(h, "Dashboard"),
NavLink(h, "Settings")
),
h.div.class("text-sm").children(`Signed in as ${session.email}`)
)
),
h.main.class("py-8").children(
LoggedInRoute.match(route, {
Dashboard: () => DashboardPage(h, session),
Settings: () =>
SettingsPage(h, {
session,
onSignOut: LoggedInMessage.ClickedSignOut()
}),
NotFound: (route) =>
NotFound(h, {
path: route.params["*"],
backHref: LoggedInRoute.Dashboard.href(),
backText: "Go to Dashboard"
h.div
.class("max-w-4xl mx-auto flex justify-between items-center")
.children(
h.ul
.class("flex gap-6 list-none")
.children(NavLink(h, "Dashboard"), NavLink(h, "Settings")),
h.div
.class("text-sm")
.children(`Signed in as ${session.email}`)
)
),
h.main
.class("py-8")
.children(
LoggedInRoute.match(route, {
Dashboard: () => DashboardPage(h, session),
Settings: () =>
SettingsPage(h, {
session,
onSignOut: LoggedInMessage.ClickedSignOut()
}),
NotFound: (route) =>
NotFound(h, {
path: route.params["*"],
backHref: LoggedInRoute.Dashboard.href(),
backText: "Go to Dashboard"
})
})
})
)
)
)
}
})
36 changes: 19 additions & 17 deletions examples/ui/auth/src/LoggedOut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,23 @@ export const LoggedOut = Html.component({
update: ($, { session }) => $.emit(LoggedOutEvents.SignedIn({ session })),

view: (h) =>
h.div.class("py-8").children(
LoggedOutRoute.match(h.props.route, {
Home: () => HomePage(h),
Login: () =>
LoginPage(h, {
onEmit: {
SignedIn: ({ session }) => LoggedOutMessage.SignedInLogin({ session })
}
}),
NotFound: (route) =>
NotFound(h, {
path: route.params["*"],
backHref: LoggedOutRoute.Home.href(),
backText: "Go Home"
})
})
)
h.div
.class("py-8")
.children(
LoggedOutRoute.match(h.props.route, {
Home: () => HomePage(h),
Login: () =>
LoginPage(h, {
onEmit: {
SignedIn: ({ session }) => LoggedOutMessage.SignedInLogin({ session })
}
}),
NotFound: (route) =>
NotFound(h, {
path: route.params["*"],
backHref: LoggedOutRoute.Home.href(),
backText: "Go Home"
})
})
)
})
79 changes: 45 additions & 34 deletions examples/ui/auth/src/pages/HomePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,52 @@ import type { Html } from "effect/ui/html"
import { LoggedOutRoute } from "../routes.ts"

const FeatureCard = <M>(h: Html.H<M>, title: string, description: string) =>
h.div.class("bg-white rounded-lg shadow-md p-6").children(
h.h2.class("text-xl font-semibold text-gray-800 mb-3").children(title),
h.p.class("text-gray-600").children(description)
)
h.div
.class("bg-white rounded-lg shadow-md p-6")
.children(
h.h2
.class("text-xl font-semibold text-gray-800 mb-3")
.children(title),
h.p
.class("text-gray-600")
.children(description)
)

export const HomePage = <M>(h: Html.H<M>) =>
h.div.class("max-w-4xl mx-auto px-4").children(
h.div.class("text-center py-16").children(
h.h1.class("text-5xl font-bold text-gray-800 mb-6").children(
"Welcome to Auth Example"
),
h.p.class("text-xl text-gray-600 mb-8").children(
"A demonstration of authentication with a Model union and route guards in Effect UI."
),
h.a.attr("href", LoggedOutRoute.Login.href())
.class(
"inline-block px-8 py-3 bg-blue-500 text-white font-medium rounded-lg hover:bg-blue-600 transition"
h.div
.class("max-w-4xl mx-auto px-4")
.children(
h.div
.class("text-center py-16")
.children(
h.h1
.class("text-5xl font-bold text-gray-800 mb-6")
.children("Welcome to Auth Example"),
h.p
.class("text-xl text-gray-600 mb-8")
.children("A demonstration of authentication with a Model union and route guards in Effect UI."),
h.a
.attr("href", LoggedOutRoute.Login.href())
.class("inline-block px-8 py-3 bg-blue-500 text-white font-medium rounded-lg hover:bg-blue-600 transition")
.children("Sign In")
),
h.div
.class("grid grid-cols-1 md:grid-cols-3 gap-8 mt-16")
.children(
FeatureCard(
h,
"Model as Union",
"App state is fundamentally LoggedOut | LoggedIn, not a flat struct with optional session."
),
FeatureCard(
h,
"Route Guards",
"Protected routes redirect to login. Auth routes redirect to dashboard when logged in."
),
FeatureCard(
h,
"Session Persistence",
"Session survives page refresh via localStorage, read into the root's props before it mounts."
)
)
.children("Sign In")
),
h.div.class("grid grid-cols-1 md:grid-cols-3 gap-8 mt-16").children(
FeatureCard(
h,
"Model as Union",
"App state is fundamentally LoggedOut | LoggedIn, not a flat struct with optional session."
),
FeatureCard(
h,
"Route Guards",
"Protected routes redirect to login. Auth routes redirect to dashboard when logged in."
),
FeatureCard(
h,
"Session Persistence",
"Session survives page refresh via localStorage, read into the root's props before it mounts."
)
)
)
24 changes: 14 additions & 10 deletions examples/ui/auth/src/pages/NotFound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ export const NotFound = <M>(h: Html.H<M>, options: {
readonly backHref: string
readonly backText: string
}) =>
h.div.class("max-w-4xl mx-auto px-4 text-center").children(
h.h1.class("text-4xl font-bold text-red-600 mb-6").children(
"404 - Page Not Found"
),
h.p.class("text-lg text-gray-600 mb-4").children(
`The path "${options.path}" was not found.`
),
h.a.attr("href", options.backHref).class("text-blue-500 hover:underline")
.children(options.backText)
)
h.div
.class("max-w-4xl mx-auto px-4 text-center")
.children(
h.h1
.class("text-4xl font-bold text-red-600 mb-6")
.children("404 - Page Not Found"),
h.p
.class("text-lg text-gray-600 mb-4")
.children(`The path "${options.path}" was not found.`),
h.a
.attr("href", options.backHref)
.class("text-blue-500 hover:underline")
.children(options.backText)
)
42 changes: 26 additions & 16 deletions examples/ui/auth/src/pages/dashboard/DashboardPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@ import type { Session } from "#domain/Session"
import type { Html } from "effect/ui/html"

const StatCard = <M>(h: Html.H<M>, title: string, value: string) =>
h.div.class("bg-white rounded-lg shadow-md p-6").children(
h.h2.class("text-sm font-medium text-gray-500 uppercase mb-1").children(
title
),
h.p.class("text-3xl font-bold text-gray-800").children(value)
)
h.div
.class("bg-white rounded-lg shadow-md p-6")
.children(
h.h2
.class("text-sm font-medium text-gray-500 uppercase mb-1")
.children(title),
h.p
.class("text-3xl font-bold text-gray-800")
.children(value)
)

export const DashboardPage = <M>(h: Html.H<M>, session: Session) =>
h.div.class("max-w-4xl mx-auto px-4").children(
h.h1.class("text-4xl font-bold text-gray-800 mb-6").children(
`Welcome back, ${session.name}!`
),
h.p.class("text-lg text-gray-600 mb-8").children("Here is your dashboard."),
h.div.class("grid grid-cols-1 md:grid-cols-3 gap-6").children(
StatCard(h, "Total Sessions", "42"),
StatCard(h, "Active Projects", "7"),
StatCard(h, "Tasks Completed", "128")
h.div
.class("max-w-4xl mx-auto px-4")
.children(
h.h1
.class("text-4xl font-bold text-gray-800 mb-6")
.children(`Welcome back, ${session.name}!`),
h.p
.class("text-lg text-gray-600 mb-8")
.children("Here is your dashboard."),
h.div
.class("grid grid-cols-1 md:grid-cols-3 gap-6")
.children(
StatCard(h, "Total Sessions", "42"),
StatCard(h, "Active Projects", "7"),
StatCard(h, "Tasks Completed", "128")
)
)
)
Loading
Loading