Skip to content
Myr

context.ts

This file

Path

apps/web/src/features/shared/shell/context.ts

66 lines · 2.4 KiB · TypeScript

Last changed by

Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e

History of this file

Take it

Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e

lines 1–66why, one step back
1/**
2 * What the shell lends the parts inside it: a way to open its overlays (the3 * rail as a sheet, the phone's More and Projects sheets, the keys dialog),4 * the project the address is in, and what the root route read for the rail5 * (the list of projects, the count on Today). The shell's parts are views:6 * the root route wires the reads (`src/routes/__root.tsx`).7 */8import type { ProjectListing } from "@repo/protocol/domain/front-page";9import { ProjectName } from "@repo/protocol/ids";10import { useMatch } from "@tanstack/react-router";11import { Schema } from "effect";12import { createContext, useContext } from "react";13import type { ReactNode } from "react";1415/** One of the shell's overlays. */16export type Overlay = "keys" | "menu" | "more" | "projects";1718export interface Shell {19 readonly open: (overlay: Overlay) => void;20}2122export const ShellContext = createContext<Shell>({23 open: () => {24 // Outside the shell (a view's own test) there is nothing to open.25 },26});2728/** The shell's overlays, from inside it. */29export const useShell = (): Shell => useContext(ShellContext);3031const isProjectName = Schema.is(ProjectName);3233/**34 * The project the address is in: the `$project` route's name, when it is35 * one a project could have. None on the site's own pages, and none for a36 * name no project may take (the page says so itself).37 */38export const useCurrentProject = (): ProjectName | undefined =>39 useMatch({40 from: "/$project",41 select: ({ params }) =>42 isProjectName(params.project) ? params.project : undefined,43 shouldThrow: false,44 });4546/** What the root route read for the rail. */47export interface ShellReads {48 /** Every listed project, once the list has arrived in the browser; nothing before. */49 readonly projects?: readonly ProjectListing[] | undefined;50 /** Today's count, drawn at the end of its row (`RailCount`); nothing for a reader who decides nothing. */51 readonly todayCount?: ReactNode;52}5354export const ShellReadsContext = createContext<ShellReads>({});5556/**57 * Every listed project, once the list has arrived: the server renders the58 * rail without them (the current project's name comes from the address),59 * and nothing on a page waits on the list.60 */61export const useProjectList = (): readonly ProjectListing[] | undefined =>62 useContext(ShellReadsContext).projects;6364/** Today's count for the rail, when there is one. */65export const useTodayCount = (): ReactNode =>66 useContext(ShellReadsContext).todayCount;