Skip to content
Myr

selection.ts

This file

Path

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

86 lines · 1.9 KiB · TypeScript

Last changed by

Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72

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

line 1why, one step back
1/**

Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72

lines 2–3why, one step back
2 * A route whose list selects leaves the param out of `loaderDeps`, so a
3 * selection never reads the list again.

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

lines 4–86why, one step back
4 */
5import {6 defaultStringifySearch,7 useLocation,8 useRouter,9} from "@tanstack/react-router";10import { Effect, Option, Schema } from "effect";11import { useCallback } from "react";1213import { useEscapeClears } from "../keys.tsx";1415export type SelectionParam = "ask" | "decision";1617const Selection = Schema.Struct({18 ask: Schema.optionalKey(Schema.String),19 decision: Schema.optionalKey(Schema.String),20});2122const decodeSelection = Schema.decodeUnknownOption(Selection);2324export const searchWith = (25 searchStr: string,26 param: SelectionParam,27 id?: string28): string => {29 const params = new URLSearchParams(searchStr);3031 params.delete(param);3233 if (id !== undefined) {34 params.set(35 param,36 new URLSearchParams(defaultStringifySearch({ [param]: id })).get(param) ??37 id38 );39 }4041 const next = params.toString();4243 return next === "" ? "" : `?${next}`;44};4546export const useSelection = (param: SelectionParam) => {47 const router = useRouter();48 const { pathname, search, searchStr } = useLocation();4950 const selected = Option.getOrUndefined(51 Option.flatMapNullishOr(decodeSelection(search), (read) => read[param])52 );5354 const move = useCallback(55 (id?: string) => {56 Effect.runFork(57 Effect.tryPromise(() =>58 router.navigate({59 href: `${pathname}${searchWith(searchStr, param, id)}`,60 replace: true,61 resetScroll: false,62 })63 ).pipe(Effect.ignore)64 );65 },66 [param, pathname, router, searchStr]67 );6869 const select = useCallback(70 (id: string) => {71 move(id);72 },73 [move]74 );7576 const clear = useCallback(() => {77 move();78 }, [move]);7980 useEscapeClears(clear);8182 return { clear, select, selected };83};8485export const reloadOnEnter = ({ cause }: { readonly cause: string }) =>86 cause !== "stay";