Skip to content
Myr

selection.ts

This file

Path

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

111 lines · 3.0 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–111why, one step back
1/**
2 * A list's selected row, kept in the address's search (`?ask=format-ordinal`,3 * `?decision=dec-000001`), so the server renders the row selected with its4 * facts in the inspector, and a link to them can be shared. Selecting5 * replaces the history entry and keeps every pane's scroll; `Escape` clears6 * it (`useEscapeClears`).7 *8 * A route whose list selects keeps the param out of its loader: `loaderDeps`9 * leaves it out, so a selection never reads the list again, and10 * `shouldReload: reloadOnEnter` keeps a list from reading again on every11 * selection once it is stale.12 *13 * ```tsx14 * const { selected, select } = useSelection("ask");15 * const { focused } = useListKeys(ids, select);16 * <Workspace facts={selected === undefined ? <ProjectFacts /> : <AskFacts ask={selected} />} factsKey={selected} … />17 * ```18 */19import {20 defaultStringifySearch,21 useLocation,22 useRouter,23} from "@tanstack/react-router";24import { Effect, Option, Schema } from "effect";25import { useCallback } from "react";2627import { useEscapeClears } from "../keys.tsx";2829/** The search params a list selects by. */30export type SelectionParam = "ask" | "decision";3132/** A selection as an address's search carries it. */33const Selection = Schema.Struct({34 ask: Schema.optionalKey(Schema.String),35 decision: Schema.optionalKey(Schema.String),36});3738const decodeSelection = Schema.decodeUnknownOption(Selection);3940/**41 * `searchStr` (`?filter=…&ask=…`) with `param` set to `id`, or without it,42 * each value written as the router writes it.43 */44export const searchWith = (45 searchStr: string,46 param: SelectionParam,47 id?: string48): string => {49 const params = new URLSearchParams(searchStr);5051 params.delete(param);5253 if (id !== undefined) {54 params.set(55 param,56 new URLSearchParams(defaultStringifySearch({ [param]: id })).get(param) ??57 id58 );59 }6061 const next = params.toString();6263 return next === "" ? "" : `?${next}`;64};6566export const useSelection = (param: SelectionParam) => {67 const router = useRouter();68 const { pathname, search, searchStr } = useLocation();6970 const selected = Option.getOrUndefined(71 Option.flatMapNullishOr(decodeSelection(search), (read) => read[param])72 );7374 const move = useCallback(75 (id?: string) => {76 Effect.runFork(77 Effect.tryPromise(() =>78 router.navigate({79 href: `${pathname}${searchWith(searchStr, param, id)}`,80 replace: true,81 resetScroll: false,82 })83 ).pipe(Effect.ignore)84 );85 },86 [param, pathname, router, searchStr]87 );8889 const select = useCallback(90 (id: string) => {91 move(id);92 },93 [move]94 );9596 const clear = useCallback(() => {97 move();98 }, [move]);99100 useEscapeClears(clear);101102 return { clear, select, selected };103};104105/**106 * A route's `shouldReload` for a list that selects: read the list when the107 * reader comes to it (or a link to it is preloaded), not again on each108 * selection; its atoms keep it live.109 */110export const reloadOnEnter = ({ cause }: { readonly cause: string }) =>111 cause !== "stay";