import { Search, X } from "lucide-react"; import { useMemo, useRef, useState } from "react"; import { Button } from "@/components/ui/button.js"; import { Command, CommandInput, CommandItem, CommandList } from "@/components/ui/command.js"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogTrigger, } from "@/components/ui/dialog.js"; import { useZCodeIntl } from "@/i18n/IntlProvider.js"; import enUS from "@/i18n/locales/en-US.js"; import type { SettingsSearchResult } from "@/settings-search/contract.js"; import { isSettingsSectionAvailable, type createSettingsPageConfig, } from "@/settings/settingsPageConfig.js"; import { SETTINGS_SEARCH_ITEMS, type SettingsSearchAvailability, } from "@/settings-search/settingsSearchIndex.js"; import { matchesSettingsNavigation, settingsNavigationSearchKeys, } from "@/settings-search/settingsNavigationSearch.js"; import { useDynamicWorkflowAvailability } from "@/hooks/useDynamicWorkflowAvailability.js"; import { resolveDynamicWorkflowModeSettingView } from "@/settings/DynamicWorkflowModeSetting.js"; export function SettingsSearch({ sections, availability, onSelect, }: { sections: ReturnType["settingsSections"]; availability: Omit; onSelect: (result: SettingsSearchResult) => void; }) { const { intl } = useZCodeIntl(); const visible: SettingsSearchAvailability = { ...availability, workflow: resolveDynamicWorkflowModeSettingView(useDynamicWorkflowAvailability()) !== null, }; const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); const selectedRef = useRef(null); const inputRef = useRef(null); const title = intl.formatMessage({ id: "settings.search.title" }); const entries = useMemo( () => sections.flatMap((section) => { const sectionLabel = intl.formatMessage({ id: section.titleId }); const items = SETTINGS_SEARCH_ITEMS.filter((item) => item.section === section.id).map( (item) => ({ section: section.id, setting: item.id, label: intl.formatMessage({ id: item.id }), sectionLabel, keys: settingsNavigationSearchKeys([ intl.formatMessage({ id: item.id }), enUS[item.id] ?? "", ]), descriptionKeys: [ intl.formatMessage({ id: item.description }), enUS[item.description] ?? "", ], visibility: item.visibility, }), ); return [ { section: section.id, setting: undefined, label: sectionLabel, sectionLabel, keys: settingsNavigationSearchKeys([sectionLabel, enUS[section.titleId] ?? ""]), descriptionKeys: [], visibility: "all" as const, }, ...items, ]; }), [sections, intl], ); const results = entries .flatMap((entry) => { if ( !isSettingsSectionAvailable(entry.section, availability.desktop) || (entry.visibility !== "all" && !visible[entry.visibility]) ) return []; const nameMatch = matchesSettingsNavigation(query, entry.keys); // 说明较长,只做片段匹配,避免顺序字符在整段说明中产生大量无关结果。 const descriptionMatch = query.trim() && entry.descriptionKeys.some((text) => text .normalize("NFKC") .toLowerCase() .includes(query.normalize("NFKC").trim().toLowerCase()), ); return nameMatch || descriptionMatch ? [{ ...entry, rank: nameMatch ? 0 : 1 }] : []; }) .sort((left, right) => left.rank - right.rank); return ( { setOpen(next); if (next) { setQuery(""); selectedRef.current = null; } }} > { const selected = selectedRef.current; if (selected) { // 先结束 Dialog 的焦点锁,再导航,让目标设置行拥有最后一次聚焦权。 if (selected.setting) event.preventDefault(); selectedRef.current = null; onSelect(selected); } }} onEscapeKeyDown={(event) => { if (event.isComposing || event.keyCode === 229) { event.preventDefault(); return; } if (query) { event.preventDefault(); setQuery(""); } }} > {title} {intl.formatMessage({ id: "settings.search.description" })}
{query ? ( ) : null}
{results.length === 0 ? (

{intl.formatMessage({ id: "settings.search.empty" })}

) : ( results.map((entry) => ( { selectedRef.current = entry; setOpen(false); }} > {entry.label} {entry.setting ? entry.sectionLabel : intl.formatMessage({ id: "settings.search.section" })} )) )}
); }