import React, { useEffect, useRef, useState } from 'react'; import { Box, Icon, IconButton, Icons, Input, MenuItem, Text, config } from 'folds'; import { SettingsPages } from '../pages'; import { searchSettings, SettingsEntry } from './settingsIndex'; import { loadSettingsIndex } from './loadSettingsIndex'; import { isTauri } from '../../../hooks/useTauri'; type SettingsSearchProps = { pageNames: Map; /** Whether a query is active (the page list is hidden meanwhile). */ onActiveChange: (active: boolean) => void; onPick: (entry: SettingsEntry) => void; }; /** * [Gitea #463] Search box at the top of the settings sidebar. Ctrl/Cmd+F * focuses it while settings are open; Esc clears it. */ export function SettingsSearch({ pageNames, onActiveChange, onPick }: SettingsSearchProps) { const inputRef = useRef(null); const [query, setQuery] = useState(''); const [entries, setEntries] = useState(); const active = query.trim().length > 0; useEffect(() => onActiveChange(active), [active, onActiveChange]); const ensureIndex = () => { if (!entries) loadSettingsIndex().then(setEntries, () => setEntries([])); }; useEffect(() => { const onKey = (e: KeyboardEvent) => { if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'f') { // Capture phase + stop: settings is on top, so this beats the app's // own Ctrl/Cmd+F (message search) underneath. e.preventDefault(); e.stopPropagation(); inputRef.current?.focus(); inputRef.current?.select(); } }; window.addEventListener('keydown', onKey, true); return () => window.removeEventListener('keydown', onKey, true); }, []); // Only desktop-app options in the desktop app; only pages that exist here. const results = entries ? searchSettings(entries, query, { desktop: isTauri() }).filter((e) => pageNames.has(e.page)) : []; return ( { ensureIndex(); setQuery(e.currentTarget.value); }} onKeyDown={(e) => { if (e.key === 'Escape' && query) { e.stopPropagation(); setQuery(''); } if (e.key === 'Enter' && results[0]) { onPick(results[0]); setQuery(''); } }} before={} after={ query ? ( setQuery('')} > ) : undefined } /> {active && ( {entries === undefined && ( Searching… )} {entries !== undefined && results.length === 0 && ( No settings match “{query.trim()}”. )} {results.map((r) => ( { onPick(r); setQuery(''); }} > {r.title} {pageNames.get(r.page)} {r.section && r.section !== r.title ? ` · ${r.section}` : ''} ))} )} ); }