justintsus-experiment Public
// What the list pages do with a parsed query (repo/issues-page.ts, repo/pulls-page.ts): which state// tab it is on, the same query on another tab, the query without its state for the tab counts, and// the query with one more term (a label chip). Everything stays a SearchQuery, so every link is// written by serializeQuery and is canonical.import type { SearchQuery, StateValue, Target, Term } from "./ast.ts";import { MAX_TERMS } from "./parse.ts";import { termText } from "./serialize.ts";export type StateTab = "open" | "closed" | "merged" | "all";const isStateTerm = (t: Term) => t.kind === "is" && t.value !== "draft";export function emptyQuery(target: Target): SearchQuery { return { target, terms: [] };}// The tab the query is on: no open/closed/merged term is All, one positive term is its tab, anything// else (two of them, a negated one) is none of the tabs.export function stateTab(q: SearchQuery): StateTab | null { const s = q.terms.filter(isStateTerm); if (!s.length) return "all"; const [t] = s; return s.length === 1 && t.kind === "is" && !t.neg ? (t.value as StateTab) : null;}// The query on another tab: its open/closed/merged terms replaced by the tab's (first), or removed// for All. is:draft and everything else stay where they are.export function withState(q: SearchQuery, tab: StateTab): SearchQuery { const rest = q.terms.filter((t) => !isStateTerm(t)); const terms: Term[] = tab === "all" ? rest : [{ kind: "is", neg: false, value: tab as StateValue }, ...rest]; return { target: q.target, terms };}// The query with one more term at the end (unchanged when it already has it), or null when that would// pass the term limit.export function withTerm(q: SearchQuery, term: Term): SearchQuery | null { const text = termText(term); if (q.terms.some((t) => termText(t) === text)) return q; if (term.kind === "sort") return { target: q.target, terms: [...q.terms.filter((t) => t.kind !== "sort"), term] }; if (q.terms.length >= MAX_TERMS) return null; return { target: q.target, terms: [...q.terms, term] };}// Whether the query narrows the list beyond a state tab and an order (the empty state then offers to// clear it).export function hasFilters(q: SearchQuery): boolean { return q.terms.some((t) => !isStateTerm(t) && t.kind !== "sort");}