Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312// The shell of Pilot: a quiet top bar that leads to every place and is the window's title bar, and the page of the current place.// DESIGN.md records the decisions: the places, history, the keys, focus and motion.import { batch, type JSX, keymap, Scroll, signal, Text, View, type ViewRef, windowSize } from "nsh";import { Button, Toolbar } from "nsh/base";import { currentWindow } from "nsh/desktop";import { belowReserve } from "../packages/protocol";import { Arrive, arrive, Box, Btn, type Entrance, hideNarrow, hideSmall, Icon, IconBtn, MOTION, NARROW_WIDTH, Notice, ReserveWarning, ring, Tip, TitleBar, Toast, TRAVEL, wash,} from "../packages/ui";import { watchInbox } from "./alerts";import { FullScreen } from "./artifacts";import { aboutItem, connectDaemon, daemonOutOfDate, link, missionById, missions, repos, request, usage, waitingInbox } from "./daemon";import { HomePage } from "./home";import { followFront, frontItem, InboxPage, openInbox } from "./inbox";import { MissionPage } from "./mission";import { NewMissionDialog } from "./new-mission";import { column, MissionsPage, RepoPage, ReposPage } from "./pages";import { Palette } from "./palette";import { PLACE_ICON, PLACE_KEY, PLACES, type Place } from "./places";import { SettingsPage } from "./settings";import { toaster } from "./toast";
/** The places in the middle of the top bar. Settings sits at its right. */const MIDDLE = PLACES.filter((place) => place !== "Settings");
/** Where the Owner is: a place, and what is open in it: a Repo by its name, in Repos, or a Mission by its id, in Missions. */type Location = { place: Place; open: string | null };const here = signal<Location>({ place: "Home", open: null });const currentPlace = () => here().place;/** Every location the Owner went to, like a browser's history. Cmd+Left and Cmd+Right move in it. */const trail = signal<Location[]>([here()]);const trailAt = signal(0);const canGoBack = () => trailAt() > 0;const canGoForward = () => trailAt() < trail().length - 1;
/** The motion of the page that mounts next, set just before the page changes. The page takes it once, as it mounts. */let pageArrival: { motion: Entrance; at: number } | null = null;/** How long a page change waits for its page to mount, in milliseconds. A page that mounts later does not play the motion. */const ARRIVAL_WINDOW_MS = 200;const takePageArrival = () => { const pending = pageArrival; pageArrival = null; // A change that mounts no new page leaves its motion behind. A page that mounts later for another reason must not play it. return pending && Date.now() - pending.at < ARRIVAL_WINDOW_MS ? pending.motion : null;};/** How deep a location is: 0 for a place, 1 for a Repo or a Mission that is open in it. */const depth = (at: Location) => (at.open ? 1 : 0);/** A place fades in. A Repo or a Mission that opens rises into place. Back to its list, the list drops into place. * Another place from a Repo's page fades in too: only a move inside one place travels. */function show(to: Location, from: Location) { const step = to.place === from.place ? depth(to) - depth(from) : 0; pageArrival = { motion: arrive(step * TRAVEL, step === 0 ? MOTION.fast : MOTION.base), at: Date.now() }; here.set(to);}/** Goes to a location and adds it to the trail. What was ahead in the trail is dropped, as in a browser. */function visit(to: Location) { const from = here(); if (from.place === to.place && from.open === to.open) return; batch(() => { trail.set([...trail().slice(0, trailAt() + 1), to]); trailAt.set(trailAt() + 1); }); show(to, from);}const go = (to: Place) => visit({ place: to, open: null });/** Goes to the Inbox, on this item, or on its oldest item for null. */const showInbox = (item: string | null) => { openInbox({ item }); go("Inbox");};const openRepo = (name: string) => visit({ place: "Repos", open: name });const openMission = (id: string) => visit({ place: "Missions", open: id });
/** The palette, while it shows. */const paletteOpen = signal(false);/** The New Mission dialog: null while it is closed, else the text and the Repo it starts with. */const newMission = signal<{ text: string; repo: string | null } | null>(null);/** The Repo a New Mission starts on: the Repo of the page the Owner is on, an open Repo, the Repo of an open Mission, * or the Repo of the Inbox item in front. Else the last one used: the Repo of the newest Mission. */function startRepo(): string | null { const at = here(); if (at.place === "Repos" && at.open) return at.open; if (at.place === "Missions" && at.open) return missionById(at.open)?.repo ?? null; const item = at.place === "Inbox" ? frontItem() : undefined; if (item && aboutItem(item).repo) return aboutItem(item).repo; return missions().at(-1)?.repo ?? null;}/** Opens the New Mission dialog with this text, on the Repo of the page the Owner is on. The palette closes. */function openNewMission(text: string) { batch(() => { paletteOpen.set(false); newMission.set({ text, repo: startRepo() }); });}function goBack() { if (!canGoBack()) return; const from = here(); trailAt.set(trailAt() - 1); show(trail()[trailAt()], from);}function goForward() { if (!canGoForward()) return; const from = here(); trailAt.set(trailAt() + 1); show(trail()[trailAt()], from);}
/** A place of the top bar. The current one is a white tile with a hairline. * In a narrow window only the current place keeps its label. The others say their name in a tooltip. */function PlaceTile(props: { place: Place }) { const current = () => currentPlace() === props.place; const narrow = () => windowSize().width < NARROW_WIDTH; return ( <Button testID={"place-" + props.place} accessibilityLabel={props.place} onPress={() => go(props.place)} tooltip={narrow() && !current() ? <Tip below label={props.place} keys={"⌘" + PLACE_KEY[props.place]} /> : undefined} class={ "h-7 flex-row items-center gap-1.5 rounded-(--radius) border px-2.5 " + (current() ? "border-panel-border bg-card" : "border-transparent hover:bg-accent/60") + ring + wash } > <Icon name={PLACE_ICON[props.place]} tone={current() ? "strong" : "quiet"} /> {/* The label takes the width of its bold form, which is hidden under it. So a place keeps its width when it becomes the current one, and its neighbours stay put. */} <View class={current() ? "" : hideNarrow}> <Text accessibilityElementsHidden class="text-sm font-medium opacity-0"> {props.place} </Text> <Text class={"absolute inset-0 text-sm " + (current() ? "font-medium text-foreground" : "text-muted-foreground")}>{props.place}</Text> </View> {/* The Inbox tile shows how many items wait, once that is known. With nothing waiting it shows no count. */} {props.place === "Inbox" && link() === "ready" && waitingInbox().length > 0 ? ( <Text class="text-xs text-muted-foreground">{waitingInbox().length}</Text> ) : null} </Button> );}
/** The top bar is quiet: it has the page's colour and no line under it, so it reads as part of the window. * It is the window's title bar too: it drags the window and steps aside of the traffic lights. */function TopBar() { return ( <TitleBar> {/* The whole bar is one stop of Tab. Left and Right move along it, so Tab reaches the page in one more press. */} <Toolbar accessibilityLabel="Pilot" class="flex-1 flex-row items-center"> <View class="flex-1 flex-row items-center gap-1.5"> <Text class={"px-2 text-sm font-bold text-foreground" + hideSmall}>Pilot</Text> <IconBtn testID="back" icon="chevron-left" label="Back" keys="⌘←" below disabled={!canGoBack()} onPress={goBack} /> <IconBtn testID="forward" icon="chevron-right" label="Forward" keys="⌘→" below disabled={!canGoForward()} onPress={goForward} /> </View> {/* The gap holds a focus ring (2 pt of space and 2 pt of ring), with 2 pt of air before the next place. */} <View class="flex-row gap-1.5"> {MIDDLE.map((place) => ( <PlaceTile place={place} /> ))} </View> <View class="flex-1 flex-row items-center justify-end gap-1.5"> {/* Only once the Usage is known: a subscription below the Usage reserve. A press leads to its Usage, in Settings. */} <Each of={link() === "ready" ? usage().subscriptions.filter((it) => belowReserve(it, usage().reserve, Date.now()) !== null) : []} key={(it) => it.name} > {(it) => <ReserveWarning testID={"reserve-" + it().name} name={it().name} onPress={() => go("Settings")} />} </Each> <IconBtn testID="open-palette" icon="search" label="Go to a place or a Mission, or start a New Mission" keys="⌘K" below onPress={() => paletteOpen.set(true)} /> <IconBtn testID="place-Settings" icon={PLACE_ICON.Settings} label="Settings" keys={"⌘" + PLACE_KEY.Settings} below onPress={() => go("Settings")} /> </View> </Toolbar> </TitleBar> );}
/** The page of each place. */const PAGES: Record<Place, () => JSX.Element> = { Home: () => <HomePage go={go} />, Inbox: () => <InboxPage openMission={openMission} />, Missions: () => { const mission = here().open; return mission ? ( <MissionPage id={mission} back={() => go("Missions")} openItem={showInbox} /> ) : ( <MissionsPage go={go} openMission={openMission} newMission={() => openNewMission("")} /> ); }, Repos: () => { const repo = here().open; return repo ? <RepoPage name={repo} back={() => go("Repos")} /> : <ReposPage openRepo={openRepo} />; }, Settings: () => <SettingsPage />,};
export function Shell() { // Escape takes the keyboard's focus off whatever has it: the ring goes, and the page's own keys work again. // NSH has no call that does it, so a view of one point takes the focus and lets it go. It is the last stop of Tab and shows nothing. let sink: ViewRef | undefined; const leaveFocus = () => { sink?.focus(); sink?.blur(); }; // The key goes on after it, so a dialog or a sheet that is open closes on it too. keymap.bind("escape", leaveFocus, { when: "notEditing", passThrough: true }); // Cmd+1 to Cmd+7 go to a place. Cmd+Left and Cmd+Right go back and forward; in a text field they keep moving the cursor. keymap.bind(Object.fromEntries(Object.entries(PLACE_KEY).map(([place, key]) => ["mod+" + key, () => go(place as Place)])), { when: "always" }); keymap.bind({ "mod+left": goBack, "mod+right": goForward }, { when: "notEditing" }); // Cmd+N opens the New Mission dialog, and Cmd+K the palette, from every page. keymap.bind( { "mod+n": () => openNewMission(""), "mod+k": () => { newMission.set(null); paletteOpen.set(true); }, }, { when: "always" }, ); // O opens the Inbox on its oldest item, from every page. The Inbox binds O again while it shows: there it switches to the list and back. keymap.bind("o", () => showInbox(null), { when: "notEditing" }); // A notification or the menu bar's icon brings the window to the front, on the Inbox. const window = currentWindow(); followFront(); watchInbox((item) => { window.focus(); showInbox(item); }); const arriving = (content: JSX.Element) => ( <Arrive class="flex-1" motion={takePageArrival}> {content} </Arrive> ); return ( <View class="flex-1 bg-page"> <TopBar /> <View class="flex-1"> <If when={daemonOutOfDate()} else={arriving(PAGES[currentPlace()]())}> <Scroll class="flex-1"> <View class={column}> <Box> <Notice big tone="destructive" icon="triangle-alert" title="Pilot's Daemon is out of date." hint="Restart the Daemon, then try again." action={<Btn small label="Try again" onPress={connectDaemon} />} /> </Box> </View> </Scroll> </If> </View> <FullScreen /> {/* As they close, the focus leaves them: on macOS it stayed stuck there, and Tab did nothing (DESIGN.md, Focus). */} <Palette open={paletteOpen()} onClose={() => { paletteOpen.set(false); leaveFocus(); }} go={go} openMission={openMission} newMission={openNewMission} /> <NewMissionDialog open={newMission() !== null} start={newMission()?.text ?? ""} repo={newMission()?.repo ?? null} repos={repos().map((repo) => repo.name)} request={request} onClose={() => { newMission.set(null); leaveFocus(); }} onCreated={(id) => { newMission.set(null); leaveFocus(); openMission(id); }} /> <Toast text={toaster.text()} /> <View ref={sink} focusable class="absolute bottom-0 right-0 size-1" /> </View> );}