import { OpenTui, OpenTuiCommand, OpenTuiSubscription } from "@effect/ui-opentui" import { OpenTuiTest } from "@effect/ui-opentui/testing" import { describe, expect, it } from "@effect/vitest" import { Effect, Schema } from "effect" import { Command, Route, Router, RouterStack, UiSchema } from "effect/ui" const AppRoute = Route.union({ Issues: Route.make("/"), Issue: Route.make("/issues/:id", { params: { id: Schema.Number } }) }) const AppNavigation = Router.navigable(AppRoute) // What each page received, by page. const log: Array = [] const Scroll = OpenTui.ref("Scroll", "scrollbox") const Issues = OpenTui.ref("Issues", "select") const ListMessage = UiSchema.TaggedEnum({ Selected: { index: Schema.Number }, ScrolledDown: {}, Pressed: { key: Schema.String }, Pasted: { text: Schema.String }, Resized: { width: Schema.Number } }) const IssueList = OpenTui.component({ name: "IssueList", model: Schema.Struct({ focused: Schema.Boolean }), message: ListMessage }).make({ init: () => ({ focused: true }), update: { Selected: ($, { index }) => $.run( AppNavigation.Issue.push({ params: { id: index + 1 } }).pipe( Command.asNoop ) ), ScrolledDown: ($) => $.run( OpenTuiCommand.scrollTo(Scroll, 2, (found) => found).pipe( Command.asNoop ) ), Pressed: ($, { key }) => { log.push("list key " + key) return $.model }, Pasted: ($, { text }) => { log.push("list paste " + text) return $.model }, // A covered screen can still run Commands, and focusing doesn't take // input from the top screen. A narrow terminal unfocuses the select. Resized: ($, { width }) => { log.push("list resize") return width < 30 ? $.evolve({ focused: false }) : $.run( OpenTuiCommand.focus(Issues, (found) => found).pipe(Command.asNoop) ) } }, subscriptions: () => [ OpenTuiSubscription.shortcuts([ { key: "x", message: ListMessage.Pressed({ key: "x" }) }, { key: "j", message: ListMessage.ScrolledDown() } ]), OpenTuiSubscription.onPaste(({ text }) => ListMessage.Pasted({ text })), OpenTuiSubscription.onResize(({ width }) => ListMessage.Resized({ width })) ], view: (h, model) => h.box.style({ flexDirection: "column" }).children( h.scrollbox.ref(Scroll).style({ height: 3 }).children( ...Array.from({ length: 8 }, (_, index) => h.text(`line ${index + 1}`)) ), h.select.ref(Issues).style({ height: 6, width: 20 }).focused( model.focused ) .options( Array.from({ length: 3 }, (_, index) => ({ name: `Issue ${index + 1}`, description: "" })) ).on("itemSelected", ({ index }) => ListMessage.Selected({ index })) ) }) const PageMessage = UiSchema.TaggedEnum({ Pressed: { key: Schema.String }, Back: {} }) const IssuePage = OpenTui.component({ name: "IssuePage", props: Schema.Struct({ id: Schema.Number }), model: Schema.Null, message: PageMessage }).make({ init: () => null, update: { Pressed: ($, { key }) => { log.push(`issue ${$.props.id} key ${key}`) return $.model }, Back: ($) => $.run(Router.Back.make().pipe(Command.asNoop)) }, subscriptions: () => [ OpenTuiSubscription.shortcuts([ { key: "x", message: PageMessage.Pressed({ key: "x" }) }, { key: "b", message: PageMessage.Back() } ]) ], view: (h) => h.text(`issue ${h.props.id}`) }) const Message = UiSchema.TaggedEnum({ Navigated: { navigation: RouterStack.Navigation(AppRoute) } }) const App = OpenTui.component({ name: "App", props: Schema.Struct({ stack: RouterStack.Stack(AppRoute) }), model: RouterStack.Stack(AppRoute), message: Message }).make({ init: ($) => $.props.stack, update: { Navigated: ($, { navigation }) => $.set(RouterStack.follow(AppRoute, $.model, navigation)) }, subscriptions: ({ model }) => [ RouterStack.subscription( AppRoute, model, (navigation) => Message.Navigated({ navigation }) ) ], view: (h, stack) => h.box.children(RouterStack.Screens(h, { entries: stack, screen: (s, { route }, covered) => s.el("box").prop("visible", !covered).children( AppRoute.match(route, { Issues: () => IssueList(s, { key: "issues" }), Issue: ({ params }) => IssuePage(s, { key: "issue", props: params }) }) ) })) }) describe("RouterStack on OpenTUI", () => { it.effect("shows a covered screen as it was left, and gives its keys to the top screen", () => Effect.gen(function*() { log.length = 0 const app = yield* OpenTuiTest.mount(App, RouterStack.props(AppRoute), { width: 30, height: 10 }) yield* app.pressKey("j") expect(app.captureFrame()).not.toContain("line 1") yield* app.pressArrow("down") yield* app.pressEnter expect(app.captureFrame()).toContain("issue 2") expect(app.captureFrame()).not.toContain("line") yield* app.pressKey("x") yield* Effect.promise(() => app.setup.mockInput.pasteBracketedText("hi")) yield* app.resize(32, 10) expect(log).toEqual(["issue 2 key x", "list resize"]) // Nor does the covered list's focused select. yield* app.pressArrow("down") yield* app.pressEnter expect(app.root.model()).toHaveLength(2) yield* app.pressKey("b") expect(app.root.model()).toEqual([{ id: 0, route: AppRoute.Issues() }]) expect(app.captureFrame()).toContain("line 3") expect(app.captureFrame()).not.toContain("line 1") // The select kept its focus and its row. yield* app.pressEnter expect(app.captureFrame()).toContain("issue 2") // Back doesn't refocus a select the covered list unfocused. yield* app.resize(20, 10) yield* app.pressKey("b") yield* app.pressEnter expect(app.root.model()).toHaveLength(1) }).pipe( // A URL the routes don't parse starts at the root. Effect.provide(RouterStack.layer({ routes: AppRoute, url: "/nope" })) )) })