fixed city search

This commit is contained in:
SlimusMinus
2026-10-19 00:18:49 +03:00
parent 0c9af11063
commit 28fedb4308
9 changed files with 573 additions and 49 deletions

View File

@@ -0,0 +1,26 @@
import { useMemo } from "react";
/**
* Общая логика выбора городов для CityFilter (десктоп) и CitySheet (мобильная шторка).
*
* @param {string[]} cities — доступные города (из /posts/cities)
* @param {string[]} value — выбранные города
* @param {string} query — введённый текст поиска
* @returns {{ options: string[], canAddCustom: boolean }}
* options — города, совпадающие с текстом (выбранные помечаются в UI галочкой);
* canAddCustom — введён текст, которого нет ни в списке, ни среди выбранных
*/
export function useCityOptions(cities, value, query) {
const normalizedQuery = query.trim().toLowerCase();
const options = useMemo(() => {
const q = normalizedQuery;
return cities.filter(c => c && (!q || c.toLowerCase().includes(q)));
}, [cities, normalizedQuery]);
const canAddCustom = normalizedQuery.length > 0 &&
!value.some(c => c.toLowerCase() === normalizedQuery) &&
!cities.some(c => c.toLowerCase() === normalizedQuery);
return { options, canAddCustom };
}