Files
NetMesh/domain/settingsSearch.ts
zhaolei 3c72efcb7f
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
[Init] Initial commit - NetMesh terminal manager
2026-09-13 18:24:01 +08:00

111 lines
3.3 KiB
TypeScript

import { matchesSearchQuery } from "../lib/searchMatcher";
import {
SETTINGS_SEARCH_CATALOG,
type SettingsSearchEntry,
type SettingsTabId,
} from "./settingsSearchCatalog";
export type SettingsSearchTranslator = (key: string) => string;
export type SettingsSearchHit = {
entry: SettingsSearchEntry;
label: string;
description?: string;
section?: string;
tabLabel: string;
};
const TAB_LABEL_KEYS: Record<SettingsTabId, string> = {
application: "settings.tab.application",
appearance: "settings.tab.appearance",
terminal: "settings.tab.terminal",
shortcuts: "settings.tab.shortcuts",
"file-associations": "settings.tab.sftpFileAssociations",
ai: "settings.tab.ai",
sync: "settings.tab.syncCloud",
system: "settings.tab.system",
plugins: "settings.tab.plugins",
};
function resolveField(t: SettingsSearchTranslator, key: string | undefined): string | undefined {
if (!key) return undefined;
const value = t(key);
if (!value || value === key) return undefined;
return value;
}
function resolveLabel(t: SettingsSearchTranslator, entry: SettingsSearchEntry): string {
const value = t(entry.labelKey);
return value && value !== entry.labelKey ? value : entry.labelKey;
}
export function buildSettingsSearchHit(
entry: SettingsSearchEntry,
t: SettingsSearchTranslator,
): SettingsSearchHit {
const tabLabelKey = TAB_LABEL_KEYS[entry.tab];
const tabLabelRaw = t(tabLabelKey);
return {
entry,
label: resolveLabel(t, entry),
description: resolveField(t, entry.descriptionKey),
section: resolveField(t, entry.sectionKey),
tabLabel: tabLabelRaw && tabLabelRaw !== tabLabelKey ? tabLabelRaw : entry.tab,
};
}
export function filterSettingsSearchCatalog(
query: string,
t: SettingsSearchTranslator,
options?: {
catalog?: readonly SettingsSearchEntry[];
includePlugins?: boolean;
limit?: number;
},
): SettingsSearchHit[] {
const catalog = options?.catalog ?? SETTINGS_SEARCH_CATALOG;
const includePlugins = options?.includePlugins ?? true;
const limit = options?.limit ?? 40;
const trimmed = query.trim();
const hits: SettingsSearchHit[] = [];
for (const entry of catalog) {
if (!includePlugins && entry.tab === "plugins") continue;
const hit = buildSettingsSearchHit(entry, t);
if (!trimmed) {
hits.push(hit);
continue;
}
const matched = matchesSearchQuery(
trimmed,
hit.label,
hit.description,
hit.section,
hit.tabLabel,
entry.labelKey,
...(entry.keywords ?? []),
);
if (matched) hits.push(hit);
}
if (!trimmed) {
return hits.slice(0, limit);
}
// Prefer label prefix / starts-with style matches first for autocomplete feel.
const normalized = trimmed.toLowerCase();
hits.sort((left, right) => {
const leftLabel = left.label.toLowerCase();
const rightLabel = right.label.toLowerCase();
const leftStarts = leftLabel.startsWith(normalized) ? 0 : 1;
const rightStarts = rightLabel.startsWith(normalized) ? 0 : 1;
if (leftStarts !== rightStarts) return leftStarts - rightStarts;
const leftIncludes = leftLabel.includes(normalized) ? 0 : 1;
const rightIncludes = rightLabel.includes(normalized) ? 0 : 1;
if (leftIncludes !== rightIncludes) return leftIncludes - rightIncludes;
return leftLabel.localeCompare(rightLabel);
});
return hits.slice(0, limit);
}