Rebasely Beta: data may be purged

justintsus-experiment Public

TypeScript · 133 lines · 7.3 KiB · at 8e57616

// The search box of the Issues and Pull requests lists (search/api.ts reads the query language): the// box with the query's canonical form (or the input as typed, when it has errors), a syntax help// disclosure listing the qualifiers of that list with one example each, and the errors of an invalid// query: each one's message under the box, and the input again with each offending span marked by a// wavy underline and its error's number, so the mark does not rely on color. Works without script:// the form is a GET, the help a <details>.import { parseQuery, type QueryError, type SearchQuery, type Target } from "../search/api.ts";import { button } from "./components.ts";import { html, type Html } from "./html.ts";export type SearchBox = {  target: Target;  action: string;  // what the box shows  value: string;  // the errors of value, in input order (none for a valid query)  errors: readonly QueryError[];};type HelpRow = { example: string; text: string };const HELP: Record<Target, HelpRow[]> = {  issues: [    { example: "is:open", text: "Open issues; is:closed for closed ones." },    { example: "label:bug", text: "Issues with that label. Quote a label with spaces: label:\"needs review\"." },    { example: "no:label", text: "Issues without any label." },    { example: "author:@me", text: "Opened by you, by a user id, or by one of this organization's agents (author:docs-bot)." },    { example: "created:>=2026-10-01", text: "Opened on a day, before or after it (<, <=, >, >=), or in a range: 2026-10-01..2026-10-31, with * for an open end." },    { example: "updated:<7d", text: "Updated less than 7 days ago. h, d and w count hours, days and weeks; >7d is older." },    { example: "sort:updated-desc", text: "The order: created, updated or comments, then -desc or -asc. Newest first by default." },    { example: "#12", text: "Issue number 12." },    { example: "\"out of memory\"", text: "Text in the title or description. Each word is matched on its own; quotes keep a phrase together." },    { example: "-label:wontfix", text: "A leading - leaves out what a term matches. Not for no:, sort:, created:, updated: or a number." },  ],  pulls: [    { example: "is:open", text: "Open pull requests, drafts included; is:closed (closed or merged), is:merged, is:draft." },    { example: "author:@me", text: "Opened by you or by an agent you own, by a user id, or by one of this organization's agents (author:docs-bot)." },    { example: "review-requested:@me", text: "Waiting for a review from you, a user id, or an agent of this organization." },    { example: "created:>=2026-10-01", text: "Opened on a day, before or after it (<, <=, >, >=), or in a range: 2026-10-01..2026-10-31, with * for an open end." },    { example: "updated:<7d", text: "Updated less than 7 days ago. h, d and w count hours, days and weeks; >7d is older." },    { example: "sort:updated-desc", text: "The order: created or updated, then -desc or -asc. Newest first by default." },    { example: "#12", text: "Pull request number 12." },    { example: "\"fix login\"", text: "Text in the title. Each word is matched on its own; quotes keep a phrase together." },    { example: "-author:@me", text: "A leading - leaves out what a term matches. Not for sort:, created:, updated: or a number." },  ],};const NOUN: Record<Target, string> = { issues: "issues", pulls: "pull requests" };const ID: Record<Target, string> = { issues: "issue-q", pulls: "pr-q" };function help(target: Target): Html {  return html`<details class="search-help"><summary>Search syntax</summary><div class="search-help-body" id="${ID[target]}-help"><p class="small">Terms are separated by spaces, and a result matches all of them. Values are not case-sensitive.</p><dl>${HELP[target].map((r) => html`<div><dt><code>${r.example}</code></dt><dd>${r.text}</dd></div>`)}</dl></div></details>`;}// The input again, each error's span marked and numbered. Spans do not overlap and come in order.function echo(value: string, errors: readonly QueryError[]): Html {  const parts: Html[] = [];  let at = 0;  errors.forEach((e, i) => {    const start = Math.max(at, Math.min(e.start, value.length));    const end = Math.max(start, Math.min(e.end, value.length));    if (start > at) parts.push(html`${value.slice(at, start)}`);    parts.push(html`<mark class="search-mark">${value.slice(start, end) || " "}<sup>${i + 1}</sup></mark>`);    at = end;  });  if (at < value.length) parts.push(html`${value.slice(at)}`);  return html`<p class="search-echo mono" aria-hidden="true">${parts}</p>`;}function errorList(target: Target, value: string, errors: readonly QueryError[]): Html {  if (!errors.length) return html``;  const one = errors.length === 1;  return html`<div class="search-errors" id="${ID[target]}-errors" role="alert"><p class="search-errors-head">${one ? "This search has a problem, so nothing is listed. Fix it and search again." : `This search has ${errors.length} problems, so nothing is listed. Fix them and search again.`}</p>${echo(value, errors)}<ol class="search-error-list">${errors.map((e) => {    const term = value.slice(e.start, e.end);    const shown = term.length > 60 ? `${term.slice(0, 60)}...` : term;    return html`<li>${shown && e.code !== "too_long" ? html`<code>${shown}</code> ` : ""}${e.message}</li>`;  })}</ol></div>`;}function emptyQuery(t: Target): SearchQuery {  const r = parseQuery("", t);  if (!r.ok) throw new Error("the empty query is valid");  return r.query;}export const EMPTY_ISSUES = emptyQuery("issues");export const EMPTY_PULLS = emptyQuery("pulls");// A list URL for a query in its canonical form, and a page of it.export function searchHref(base: string, q: string, page = 1): string {  const p = new URLSearchParams({ q });  if (page > 1) p.set("page", String(page));  return `${base}?${p}`;}export type ListTab = { label: string; href: string; count: number | null; current: boolean };export function stateTabs(tabs: readonly ListTab[], label: string): Html {  return html`<nav class="subnav pr-states" aria-label="${label}">${tabs.map((t) => html`<a href="${t.href}"${t.current ? html` aria-current="page"` : ""}>${t.label}${t.count !== null ? html` <span class="count num">${t.count.toLocaleString("en-US")}</span>` : ""}</a>`)}</nav>`;}export type Pager = { prev: string | null; next: string | null };export function pager(p: Pager, noun: string): Html {  if (!p.prev && !p.next) return html``;  return html`<nav class="card-foot search-pager" aria-label="Pages of ${noun}">${p.prev ? html`<a href="${p.prev}" rel="prev">Previous page</a>` : html`<span></span>`}${p.next ? html`<a href="${p.next}" rel="next">Next page</a>` : ""}</nav>`;}export function searchBox(d: SearchBox): Html {  const id = ID[d.target];  const invalid = d.errors.length > 0;  const described = [`${id}-help`, ...(invalid ? [`${id}-errors`] : [])].join(" ");  return html`<div class="search-bar"><form method="get" action="${d.action}" class="pr-search search-form" role="search"><label class="visually-hidden" for="${id}">Search ${NOUN[d.target]}</label><input id="${id}" name="q" type="search" value="${d.value}" placeholder="is:open author:@me crash" autocomplete="off" spellcheck="false" aria-describedby="${described}"${invalid ? html` aria-invalid="true"` : ""}>${button("Search", { size: "sm" })}</form>${help(d.target)}</div>${errorList(d.target, d.value, d.errors)}`;}