fix(search): debounce 350мс в TextsEditor и WordsEditor (22.09.2026)

This commit is contained in:
2026-09-22 13:24:23 +03:00
parent 45628fdaf8
commit 982e671955
2 changed files with 30 additions and 8 deletions
+16 -4
View File
@@ -20,6 +20,10 @@ const TextsEditor: React.FC<TextsEditorProps> = ({ isOpen, onClose }) => {
const [texts, setTexts] = useState<ContentText[]>([]); const [texts, setTexts] = useState<ContentText[]>([]);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [search, setSearch] = useState(''); const [search, setSearch] = useState('');
// 🔧 ФИКС (22.09.2026): debounce для поиска. Раньше каждое нажатие
// отправляло запрос — при быстром вводе десятки параллельных запросов,
// ответы приходили вразнобой, список мигал.
const [debouncedSearch, setDebouncedSearch] = useState('');
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [editingText, setEditingText] = useState<ContentText | null>(null); const [editingText, setEditingText] = useState<ContentText | null>(null);
@@ -40,18 +44,26 @@ const TextsEditor: React.FC<TextsEditorProps> = ({ isOpen, onClose }) => {
}); });
}, [isOpen]); }, [isOpen]);
// 🔧 ФИКС (22.09.2026): debounce 350мс — ждём, пока юзер перестал печатать
useEffect(() => {
const timer = setTimeout(() => {
setDebouncedSearch(search);
}, 350);
return () => clearTimeout(timer);
}, [search]);
useEffect(() => { useEffect(() => {
if (!selectedLevel) return; if (!selectedLevel) return;
setLoading(true); setLoading(true);
setError(null); setError(null);
getLevelTexts(selectedLevel, search || undefined) getLevelTexts(selectedLevel, debouncedSearch || undefined)
.then((data) => setTexts(data.texts)) .then((data) => setTexts(data.texts))
.catch((e) => { .catch((e) => {
console.error('[TextsEditor] texts load failed:', e); console.error('[TextsEditor] texts load failed:', e);
setError('Не удалось загрузить тексты'); setError('Не удалось загрузить тексты');
}) })
.finally(() => setLoading(false)); .finally(() => setLoading(false));
}, [selectedLevel, search]); }, [selectedLevel, debouncedSearch]);
if (!isOpen) return null; if (!isOpen) return null;
@@ -273,7 +285,7 @@ const TextsEditor: React.FC<TextsEditorProps> = ({ isOpen, onClose }) => {
onSaved={() => { onSaved={() => {
setEditingText(null); setEditingText(null);
if (selectedLevel) { if (selectedLevel) {
getLevelTexts(selectedLevel, search || undefined).then((data) => setTexts(data.texts)); getLevelTexts(selectedLevel, debouncedSearch || undefined).then((data) => setTexts(data.texts));
} }
}} }}
/> />
@@ -286,7 +298,7 @@ const TextsEditor: React.FC<TextsEditorProps> = ({ isOpen, onClose }) => {
onClose={() => setIsCreating(false)} onClose={() => setIsCreating(false)}
onSaved={() => { onSaved={() => {
setIsCreating(false); setIsCreating(false);
getLevelTexts(selectedLevel, search || undefined).then((data) => setTexts(data.texts)); getLevelTexts(selectedLevel, debouncedSearch || undefined).then((data) => setTexts(data.texts));
}} }}
/> />
)} )}
+14 -4
View File
@@ -20,6 +20,8 @@ const WordsEditor: React.FC<WordsEditorProps> = ({ isOpen, onClose }) => {
const [words, setWords] = useState<ContentWord[]>([]); const [words, setWords] = useState<ContentWord[]>([]);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [search, setSearch] = useState(''); const [search, setSearch] = useState('');
// 🔧 ФИКС (22.09.2026): debounce для поиска.
const [debouncedSearch, setDebouncedSearch] = useState('');
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [editingWord, setEditingWord] = useState<ContentWord | null>(null); const [editingWord, setEditingWord] = useState<ContentWord | null>(null);
@@ -41,19 +43,27 @@ const WordsEditor: React.FC<WordsEditorProps> = ({ isOpen, onClose }) => {
}); });
}, [isOpen]); }, [isOpen]);
// 🔧 ФИКС (22.09.2026): debounce 350мс — ждём, пока юзер перестал печатать
useEffect(() => {
const timer = setTimeout(() => {
setDebouncedSearch(search);
}, 350);
return () => clearTimeout(timer);
}, [search]);
// Загрузка слов при выборе уровня / изменении поиска // Загрузка слов при выборе уровня / изменении поиска
useEffect(() => { useEffect(() => {
if (!selectedLevel) return; if (!selectedLevel) return;
setLoading(true); setLoading(true);
setError(null); setError(null);
getLevelWords(selectedLevel, search || undefined) getLevelWords(selectedLevel, debouncedSearch || undefined)
.then((data) => setWords(data.words)) .then((data) => setWords(data.words))
.catch((e) => { .catch((e) => {
console.error('[WordsEditor] words load failed:', e); console.error('[WordsEditor] words load failed:', e);
setError('Не удалось загрузить слова'); setError('Не удалось загрузить слова');
}) })
.finally(() => setLoading(false)); .finally(() => setLoading(false));
}, [selectedLevel, search]); }, [selectedLevel, debouncedSearch]);
if (!isOpen) return null; if (!isOpen) return null;
@@ -282,7 +292,7 @@ const WordsEditor: React.FC<WordsEditorProps> = ({ isOpen, onClose }) => {
onSaved={() => { onSaved={() => {
setEditingWord(null); setEditingWord(null);
if (selectedLevel) { if (selectedLevel) {
getLevelWords(selectedLevel, search || undefined).then((data) => setWords(data.words)); getLevelWords(selectedLevel, debouncedSearch || undefined).then((data) => setWords(data.words));
} }
}} }}
/> />
@@ -295,7 +305,7 @@ const WordsEditor: React.FC<WordsEditorProps> = ({ isOpen, onClose }) => {
onClose={() => setIsCreating(false)} onClose={() => setIsCreating(false)}
onSaved={() => { onSaved={() => {
setIsCreating(false); setIsCreating(false);
getLevelWords(selectedLevel, search || undefined).then((data) => setWords(data.words)); getLevelWords(selectedLevel, debouncedSearch || undefined).then((data) => setWords(data.words));
}} }}
/> />
)} )}