129 lines
4.3 KiB
TypeScript
129 lines
4.3 KiB
TypeScript
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<SettingsPages, string>;
|
||
|
|
/** 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<HTMLInputElement>(null);
|
||
|
|
const [query, setQuery] = useState('');
|
||
|
|
const [entries, setEntries] = useState<SettingsEntry[]>();
|
||
|
|
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 (
|
||
|
|
<Box direction="Column" gap="200" style={{ padding: `0 ${config.space.S200}` }}>
|
||
|
|
<Input
|
||
|
|
ref={inputRef}
|
||
|
|
size="400"
|
||
|
|
variant="Background"
|
||
|
|
radii="400"
|
||
|
|
placeholder="Search settings"
|
||
|
|
aria-label="Search settings"
|
||
|
|
value={query}
|
||
|
|
onFocus={ensureIndex}
|
||
|
|
onChange={(e) => {
|
||
|
|
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={<Icon size="100" src={Icons.Search} />}
|
||
|
|
after={
|
||
|
|
query ? (
|
||
|
|
<IconButton
|
||
|
|
size="300"
|
||
|
|
radii="300"
|
||
|
|
variant="Background"
|
||
|
|
aria-label="Clear search"
|
||
|
|
onClick={() => setQuery('')}
|
||
|
|
>
|
||
|
|
<Icon size="50" src={Icons.Cross} />
|
||
|
|
</IconButton>
|
||
|
|
) : undefined
|
||
|
|
}
|
||
|
|
/>
|
||
|
|
{active && (
|
||
|
|
<Box direction="Column" role="list" aria-label="Matching settings">
|
||
|
|
{entries === undefined && (
|
||
|
|
<Text size="T200" priority="300" style={{ padding: config.space.S200 }}>
|
||
|
|
Searching…
|
||
|
|
</Text>
|
||
|
|
)}
|
||
|
|
{entries !== undefined && results.length === 0 && (
|
||
|
|
<Text size="T200" priority="300" style={{ padding: config.space.S200 }}>
|
||
|
|
No settings match “{query.trim()}”.
|
||
|
|
</Text>
|
||
|
|
)}
|
||
|
|
{results.map((r) => (
|
||
|
|
<MenuItem
|
||
|
|
key={`${r.page}-${r.title}`}
|
||
|
|
role="listitem"
|
||
|
|
variant="Background"
|
||
|
|
radii="400"
|
||
|
|
onClick={() => {
|
||
|
|
onPick(r);
|
||
|
|
setQuery('');
|
||
|
|
}}
|
||
|
|
>
|
||
|
|
<Box direction="Column" style={{ padding: `${config.space.S100} 0`, minWidth: 0 }}>
|
||
|
|
<Text size="T300" truncate>
|
||
|
|
{r.title}
|
||
|
|
</Text>
|
||
|
|
<Text size="T200" priority="300" truncate>
|
||
|
|
{pageNames.get(r.page)}
|
||
|
|
{r.section && r.section !== r.title ? ` · ${r.section}` : ''}
|
||
|
|
</Text>
|
||
|
|
</Box>
|
||
|
|
</MenuItem>
|
||
|
|
))}
|
||
|
|
</Box>
|
||
|
|
)}
|
||
|
|
</Box>
|
||
|
|
);
|
||
|
|
}
|