fix(search): debounce 350мс в TextsEditor и WordsEditor (22.09.2026)
This commit is contained in:
@@ -20,6 +20,10 @@ const TextsEditor: React.FC<TextsEditorProps> = ({ isOpen, onClose }) => {
|
||||
const [texts, setTexts] = useState<ContentText[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [search, setSearch] = useState('');
|
||||
// 🔧 ФИКС (22.09.2026): debounce для поиска. Раньше каждое нажатие
|
||||
// отправляло запрос — при быстром вводе десятки параллельных запросов,
|
||||
// ответы приходили вразнобой, список мигал.
|
||||
const [debouncedSearch, setDebouncedSearch] = useState('');
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const [editingText, setEditingText] = useState<ContentText | null>(null);
|
||||
@@ -40,18 +44,26 @@ const TextsEditor: React.FC<TextsEditorProps> = ({ isOpen, onClose }) => {
|
||||
});
|
||||
}, [isOpen]);
|
||||
|
||||
// 🔧 ФИКС (22.09.2026): debounce 350мс — ждём, пока юзер перестал печатать
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => {
|
||||
setDebouncedSearch(search);
|
||||
}, 350);
|
||||
return () => clearTimeout(timer);
|
||||
}, [search]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!selectedLevel) return;
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
getLevelTexts(selectedLevel, search || undefined)
|
||||
getLevelTexts(selectedLevel, debouncedSearch || undefined)
|
||||
.then((data) => setTexts(data.texts))
|
||||
.catch((e) => {
|
||||
console.error('[TextsEditor] texts load failed:', e);
|
||||
setError('Не удалось загрузить тексты');
|
||||
})
|
||||
.finally(() => setLoading(false));
|
||||
}, [selectedLevel, search]);
|
||||
}, [selectedLevel, debouncedSearch]);
|
||||
|
||||
if (!isOpen) return null;
|
||||
|
||||
@@ -273,7 +285,7 @@ const TextsEditor: React.FC<TextsEditorProps> = ({ isOpen, onClose }) => {
|
||||
onSaved={() => {
|
||||
setEditingText(null);
|
||||
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)}
|
||||
onSaved={() => {
|
||||
setIsCreating(false);
|
||||
getLevelTexts(selectedLevel, search || undefined).then((data) => setTexts(data.texts));
|
||||
getLevelTexts(selectedLevel, debouncedSearch || undefined).then((data) => setTexts(data.texts));
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -20,6 +20,8 @@ const WordsEditor: React.FC<WordsEditorProps> = ({ isOpen, onClose }) => {
|
||||
const [words, setWords] = useState<ContentWord[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [search, setSearch] = useState('');
|
||||
// 🔧 ФИКС (22.09.2026): debounce для поиска.
|
||||
const [debouncedSearch, setDebouncedSearch] = useState('');
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const [editingWord, setEditingWord] = useState<ContentWord | null>(null);
|
||||
@@ -41,19 +43,27 @@ const WordsEditor: React.FC<WordsEditorProps> = ({ isOpen, onClose }) => {
|
||||
});
|
||||
}, [isOpen]);
|
||||
|
||||
// 🔧 ФИКС (22.09.2026): debounce 350мс — ждём, пока юзер перестал печатать
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => {
|
||||
setDebouncedSearch(search);
|
||||
}, 350);
|
||||
return () => clearTimeout(timer);
|
||||
}, [search]);
|
||||
|
||||
// Загрузка слов при выборе уровня / изменении поиска
|
||||
useEffect(() => {
|
||||
if (!selectedLevel) return;
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
getLevelWords(selectedLevel, search || undefined)
|
||||
getLevelWords(selectedLevel, debouncedSearch || undefined)
|
||||
.then((data) => setWords(data.words))
|
||||
.catch((e) => {
|
||||
console.error('[WordsEditor] words load failed:', e);
|
||||
setError('Не удалось загрузить слова');
|
||||
})
|
||||
.finally(() => setLoading(false));
|
||||
}, [selectedLevel, search]);
|
||||
}, [selectedLevel, debouncedSearch]);
|
||||
|
||||
if (!isOpen) return null;
|
||||
|
||||
@@ -282,7 +292,7 @@ const WordsEditor: React.FC<WordsEditorProps> = ({ isOpen, onClose }) => {
|
||||
onSaved={() => {
|
||||
setEditingWord(null);
|
||||
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)}
|
||||
onSaved={() => {
|
||||
setIsCreating(false);
|
||||
getLevelWords(selectedLevel, search || undefined).then((data) => setWords(data.words));
|
||||
getLevelWords(selectedLevel, debouncedSearch || undefined).then((data) => setWords(data.words));
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user