justintsus-experiment Public
// Changes the list pages make to a parsed query for their links: the state tabs (Open, Closed,// Merged, All) replace the query's is:open/closed/merged terms, and a label chip adds label:x.import { Some, None, type Option } from "@tsus/runtime";import type { ListState, SearchQuery, StateValue, Term } from "./ast.tsus";import { serializeTerm } from "./serialize.tsus";import { labelValue } from "./values.tsus";const listStateOf = (s: StateValue): Option<ListState> => match (s) { "open" => Some<ListState>("open"), "closed" => Some<ListState>("closed"), "merged" => Some<ListState>("merged"), "draft" => None, };// The is:open/closed/merged term t is, with whether it is negated.type StateTerm = { readonly state: ListState; readonly negated: boolean };const stateTerm = (t: Term): Option<StateTerm> => match (t) { Is(x) => listStateOf(x.state).map((state) => ({ state, negated: x.negated })), _ => None, };// The tab a query is on: its one is:open, is:closed or is:merged, when it has exactly one and no// negated one. None: All, or a mix no tab stands for.export const tabOf = (q: SearchQuery): Option<ListState> => { const states: StateTerm[] = []; for (const t of q.terms) { const s = stateTerm(t); if (s.isSome()) states.push(s.value); } const only = states.length === 1 ? states.slice(0, 1) : []; for (const s of only) if (!s.negated) return Some(s.state); return None;};// The query with its state terms replaced by is:<state> in front (or removed, for None).export const withState = (q: SearchQuery, state: Option<ListState>): SearchQuery => { const rest = q.terms.filter((t) => stateTerm(t).isNone()); const lead: readonly Term[] = state.isSome() ? [{ tag: "Is", negated: false, state: state.value }] : []; return { target: q.target, terms: [...lead, ...rest] };};// The query with label:<label> added at the end, unless it has it already. None for a name that is// no label (or on pulls, which have none).export const withLabel = (q: SearchQuery, label: string): Option<SearchQuery> => { if (q.target !== "issues") return None; return labelValue(label).map((l): SearchQuery => { const t: Term = { tag: "Label", negated: false, label: l }; const have = q.terms.some((x) => serializeTerm(x) === serializeTerm(t)); return have ? q : { target: q.target, terms: [...q.terms, t] }; });};