web: parse search type prefixes into kind filters

This commit is contained in:
2026-10-20 17:31:42 +01:00
parent 35040fe13f
commit a670610497
5 changed files with 195 additions and 14 deletions
+21
View File
@@ -42,6 +42,27 @@ export const SEARCH_KINDS = [
/** The default, no-narrowing kind. */
export const DEFAULT_SEARCH_KIND = "";
/** Unquoted, whitespace-delimited t:/type: tokens; last supported token wins.
* Unknown tokens and quoted text stay literal, including their whitespace.
* The selector is a fallback; explicitly changing it removes tokens in the view.
*/
export function parseSearchQuery(value, selectedKind = DEFAULT_SEARCH_KIND) {
const source = String(value ?? "");
let kind = selectedKind;
let hasType = false;
const query = source.replace(/(?:[^\s"'\\]|\\[\s\S]|"(?:[^"\\]|\\[\s\S])*(?:"|$)|'(?:[^'\\]|\\[\s\S])*(?:'|$))+/g, (token) => {
const match = /^(?:t|type):([a-z]+)$/i.exec(token);
const candidate = match?.[1].toLowerCase();
if (!candidate || !SEARCH_KINDS.some((option) => option.value === candidate)) return token;
kind = candidate;
hasType = true;
return "";
});
return { query, kind, hasType };
}
export const SEARCH_TYPE_HELP = "Add search text after the type filter (for example, t:pipeline plan). This server does not list results by type alone.";
/**
* The orders the search sort control offers. "Relevance" keeps the server's own
* ranking (score, then recency, then a stable kind/target tie-break); "Newest
+34 -14
View File
@@ -12,6 +12,8 @@ import {
SEARCH_DEBOUNCE_MS,
SEARCH_KINDS,
SEARCH_SORTS,
SEARCH_TYPE_HELP,
parseSearchQuery,
SearchSequencer,
filterSearchResults,
resolveRunTarget,
@@ -28,6 +30,7 @@ import {
export function createSearchView(rt) {
const { state, repo, searchHost } = rt;
const parsedSearch = () => parseSearchQuery(state.searchQuery, state.searchKind);
let searchTimer = null;
let searchEdit = null;
// The last search is resumed on the next open (the field, kind, sort and the
@@ -181,9 +184,11 @@ function sendSearchNow() {
* request is sent with the new kind. A sort change never sends a request.
*/
function setSearchKind(kind) {
state.searchQuery = parsedSearch().query;
const input = document.getElementById("searchInput");
if (input) input.value = state.searchQuery;
state.searchKind = kind;
refreshSearch();
if (searchQuerySendable(state.searchQuery)) sendSearchNow();
sendSearchNow();
}
function setSearchSort(sort) {
@@ -198,8 +203,16 @@ function setSearchSort(sort) {
* failure carries the server's own text. A stale reply is dropped entirely.
*/
async function runSearch(sent) {
const parsed = parseSearchQuery(sent.query, state.searchKind);
if (!parsed.query.trim()) {
state.searchResults = [];
state.searchLoading = false;
state.searchLoaded = false;
refreshSearch();
return;
}
try {
const reply = await repo.search(sent.query, state.searchKind);
const reply = await repo.search(parsed.query, parsed.kind);
if (!state.searchSequencer.accepts(sent.token)) return;
if (reply == null) {
state.searchLoading = false;
@@ -230,11 +243,11 @@ async function runSearch(sent) {
*/
function refreshSearch() {
const kindSelect = document.getElementById("searchKind");
if (kindSelect) kindSelect.value = state.searchKind;
if (kindSelect) kindSelect.value = parsedSearch().kind;
const sortSelect = document.getElementById("searchSort");
if (sortSelect) sortSelect.value = state.searchSort;
const summary = document.getElementById("searchSummary");
if (summary) summary.textContent = searchControlsSummary(state.searchKind, state.searchSort);
if (summary) summary.textContent = searchControlsSummary(parsedSearch().kind, state.searchSort);
const area = document.getElementById("searchResults");
if (area) fillSearchResults(area);
}
@@ -260,7 +273,7 @@ function renderSearch() {
},
...SEARCH_KINDS.map((option) => h("option", { value: option.value, text: option.label })),
);
kindSelect.value = state.searchKind;
kindSelect.value = parsedSearch().kind;
const sortSelect = h(
"select",
{
@@ -288,7 +301,7 @@ function renderSearch() {
oninput: (event) => onSearchInput(event.target.value),
onkeydown: (event) => {
const shown = sortSearchResults(
filterSearchResults(state.searchResults, state.searchKind),
filterSearchResults(state.searchResults, parsedSearch().kind),
state.searchSort,
);
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
@@ -302,8 +315,9 @@ function renderSearch() {
}
if (event.key === "Enter") {
event.preventDefault();
if (shown.length) routeSearchResult(shown[state.searchActiveIndex] ?? shown[0]);
else sendSearchNow();
if (shown.length && !state.searchLoading && !state.searchError && parsedSearch().query.trim()) {
routeSearchResult(shown[state.searchActiveIndex] ?? shown[0]);
} else sendSearchNow();
}
},
}),
@@ -324,7 +338,7 @@ function renderSearch() {
h("span", {
id: "searchSummary",
class: "search-summary muted",
text: searchControlsSummary(state.searchKind, state.searchSort),
text: searchControlsSummary(parsedSearch().kind, state.searchSort),
}),
h("button", {
id: "searchClearFilter",
@@ -340,7 +354,8 @@ function renderSearch() {
const panel = h(
"div",
{ id: "searchPanel", class: "search-panel", role: "dialog", "aria-modal": "true", "aria-label": "Search" },
h("div", { class: "search-modal", role: "search" }, fieldRow, controls, area),
h("div", { class: "search-modal", role: "search" }, fieldRow, controls,
h("p", { class: "muted", text: "Filter with t:pipeline or type:artifact (pipeline, run, step, field, artifact, version). Last type wins; choosing Kind replaces it. Quote tokens to search literally." }), area),
);
// A spotlight: clicking the dimmed backdrop closes it (the modal itself stops).
panel.addEventListener("click", (event) => {
@@ -361,15 +376,20 @@ function renderSearch() {
* the filtered and sorted rows, or the empty-state line.
*/
function fillSearchResults(area) {
const parsed = parsedSearch();
if (parsed.hasType && !parsed.query.trim()) {
area.replaceChildren(h("div", { class: "search-empty muted", text: SEARCH_TYPE_HELP }));
return;
}
const shown = sortSearchResults(
filterSearchResults(state.searchResults, state.searchKind),
filterSearchResults(state.searchResults, parsed.kind),
state.searchSort,
);
const nodes = [];
if (searchPanelProgress(state)) {
nodes.push(h("div", { class: "search-progress", role: "progressbar", "aria-label": "Searching" }));
}
const body = searchPanelBody(state, shown, state.searchKind);
const body = searchPanelBody(state, shown, parsed.kind);
if (body.kind === "error") {
nodes.push(h("div", { class: "search-error", text: body.message }));
} else if (body.kind === "results") {
@@ -504,7 +524,7 @@ async function openRunFromSearch(runId) {
* read-only and is never clickable. Every server string is set as text.
*/
function renderSearchResultRow(hit) {
const row = searchRow(hit, state.searchQuery);
const row = searchRow(hit, parsedSearch().query);
const target = searchTarget(hit);
const artifactHit = hit?.kind === "artifact" && hit.artifact ? hit.artifact : null;
const element = h("div", {