Compare commits
2 Commits
11bc782e39
...
95b2d7a58a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
95b2d7a58a | ||
|
|
838443fcb2 |
@@ -28,7 +28,7 @@ export default function CityFilter({ cities, value, onChange }) {
|
||||
return () => document.removeEventListener("mousedown", onDocMouseDown);
|
||||
}, [open]);
|
||||
|
||||
const { options, canAddCustom } = useCityOptions(cities, value, query);
|
||||
const { options, typedCity } = useCityOptions(cities, value, query);
|
||||
|
||||
const toggle = (city) => {
|
||||
if (value.includes(city)) {
|
||||
@@ -40,20 +40,19 @@ export default function CityFilter({ cities, value, onChange }) {
|
||||
inputRef.current?.focus();
|
||||
};
|
||||
|
||||
const addCustom = () => {
|
||||
const city = query.trim();
|
||||
if (city && !value.includes(city)) {
|
||||
onChange([...value, city]);
|
||||
}
|
||||
// Города нет в списке — объявлений там нет, но фильтр всё равно применяем:
|
||||
// лента покажет «В этом городе нет объявлений»
|
||||
const applyTyped = () => {
|
||||
onChange([...value, typedCity]);
|
||||
setQuery("");
|
||||
inputRef.current?.focus();
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
const handleKeyDown = (e) => {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
if (canAddCustom) {
|
||||
addCustom();
|
||||
if (typedCity) {
|
||||
applyTyped();
|
||||
} else if (options.length) {
|
||||
toggle(options[0]);
|
||||
}
|
||||
@@ -138,23 +137,10 @@ export default function CityFilter({ cities, value, onChange }) {
|
||||
|
||||
{open && (
|
||||
<div className="f-city-ms-dropdown">
|
||||
{options.length === 0 && !canAddCustom && (
|
||||
{options.length === 0 && (
|
||||
<div className="f-city-ms-empty">{t('feed.filterCitiesEmpty')}</div>
|
||||
)}
|
||||
|
||||
{canAddCustom && (
|
||||
<button type="button" className="f-city-ms-option f-city-ms-add"
|
||||
onClick={() => {
|
||||
addCustom();
|
||||
setOpen(false);
|
||||
}}>
|
||||
<span className="f-city-ms-check">+</span>
|
||||
<span className="f-city-ms-label">
|
||||
{t('feed.filterCitiesAdd', { city: query.trim() })}
|
||||
</span>
|
||||
</button>
|
||||
)}
|
||||
|
||||
{options.map(city => {
|
||||
const selected = value.includes(city);
|
||||
return (
|
||||
|
||||
@@ -42,7 +42,7 @@ export default function CitySheet({ open, onClose, cities, value, onChange }) {
|
||||
return () => document.removeEventListener("keydown", onKeyDown);
|
||||
}, [open, onClose]);
|
||||
|
||||
const { options, canAddCustom } = useCityOptions(cities, draft, query);
|
||||
const { options, typedCity } = useCityOptions(cities, draft, query);
|
||||
|
||||
// Города, добавленные свободным вводом, в общем списке отсутствуют — показываем их сверху
|
||||
const q = query.trim().toLowerCase();
|
||||
@@ -54,14 +54,10 @@ export default function CitySheet({ open, onClose, cities, value, onChange }) {
|
||||
setDraft(d => d.includes(city) ? d.filter(c => c !== city) : [...d, city]);
|
||||
};
|
||||
|
||||
const addCustom = () => {
|
||||
const city = query.trim();
|
||||
if (city && !draft.includes(city)) setDraft(d => [...d, city]);
|
||||
setQuery("");
|
||||
};
|
||||
|
||||
// Введённый город, которого нет в списке, тоже применяется: лента покажет
|
||||
// «В этом городе нет объявлений»
|
||||
const apply = () => {
|
||||
onChange(draft);
|
||||
onChange(typedCity ? [...draft, typedCity] : draft);
|
||||
onClose();
|
||||
};
|
||||
|
||||
@@ -122,27 +118,18 @@ export default function CitySheet({ open, onClose, cities, value, onChange }) {
|
||||
placeholder={t("feed.filterCitiesSearch")}
|
||||
onChange={e => setQuery(e.target.value)}
|
||||
onKeyDown={e => {
|
||||
if (e.key === "Enter" && canAddCustom) {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
addCustom();
|
||||
apply();
|
||||
}
|
||||
}}
|
||||
autoComplete="off"
|
||||
enterKeyHint="done"
|
||||
enterKeyHint="search"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="f-sheet-list">
|
||||
{canAddCustom && (
|
||||
<button type="button" className="f-sheet-row f-sheet-row-add" onClick={addCustom}>
|
||||
<span className="f-sheet-check">+</span>
|
||||
<span className="f-sheet-row-label">
|
||||
{t("feed.filterCitiesAdd", { city: query.trim() })}
|
||||
</span>
|
||||
</button>
|
||||
)}
|
||||
|
||||
{rows.length === 0 && !canAddCustom && (
|
||||
{rows.length === 0 && (
|
||||
<div className="f-sheet-empty">{t("feed.filterCitiesEmpty")}</div>
|
||||
)}
|
||||
|
||||
|
||||
@@ -6,9 +6,11 @@ import { useMemo } from "react";
|
||||
* @param {string[]} cities — доступные города (из /posts/cities)
|
||||
* @param {string[]} value — выбранные города
|
||||
* @param {string} query — введённый текст поиска
|
||||
* @returns {{ options: string[], canAddCustom: boolean }}
|
||||
* @returns {{ options: string[], typedCity: string }}
|
||||
* options — города, совпадающие с текстом (выбранные помечаются в UI галочкой);
|
||||
* canAddCustom — введён текст, которого нет ни в списке, ни среди выбранных
|
||||
* typedCity — введённый город, которого нет в списке (объявлений там нет).
|
||||
* Пусто, если есть совпадения или город уже выбран. Применяется как фильтр
|
||||
* по «Показать»/Enter — лента покажет «В этом городе нет объявлений».
|
||||
*/
|
||||
export function useCityOptions(cities, value, query) {
|
||||
const normalizedQuery = query.trim().toLowerCase();
|
||||
@@ -18,9 +20,9 @@ export function useCityOptions(cities, value, query) {
|
||||
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);
|
||||
const typedCity = normalizedQuery.length > 0 && options.length === 0 &&
|
||||
!value.some(c => c.toLowerCase() === normalizedQuery)
|
||||
? query.trim() : "";
|
||||
|
||||
return { options, canAddCustom };
|
||||
return { options, typedCity };
|
||||
}
|
||||
|
||||
@@ -7,6 +7,9 @@ const TOP_ZONE_PX = 60;
|
||||
// Пока шапка сворачивается/разворачивается, высота скролл-контейнера меняется
|
||||
// и браузер может сам поправить scrollTop — эти события игнорируем, иначе дрожание
|
||||
const LOCK_MS = 350;
|
||||
// Короткую ленту не сворачиваем: после скрытия шапки контейнер вырастет на её
|
||||
// высоту, и прокручивать станет нечего — шапку уже не вернуть жестом
|
||||
const MIN_SCROLLABLE_PX = 400;
|
||||
|
||||
/**
|
||||
* Хук «прятать шапку при скролле вниз, показывать при скролле вверх».
|
||||
@@ -30,6 +33,7 @@ export function useHideOnScroll(scrollRef, enabled) {
|
||||
let lastY = el.scrollTop;
|
||||
let acc = 0;
|
||||
let lockUntil = 0;
|
||||
let recheckTimer = 0;
|
||||
|
||||
const set = (next) => {
|
||||
if (hiddenRef.current === next) return;
|
||||
@@ -37,6 +41,14 @@ export function useHideOnScroll(scrollRef, enabled) {
|
||||
setHidden(next);
|
||||
acc = 0;
|
||||
lockUntil = Date.now() + LOCK_MS;
|
||||
// События во время блокировки игнорируются; если за это время браузер
|
||||
// прижал список к верху (контейнер вырос), новых scroll уже не будет —
|
||||
// поэтому по окончании блокировки проверяем позицию сами
|
||||
clearTimeout(recheckTimer);
|
||||
recheckTimer = setTimeout(() => {
|
||||
lastY = el.scrollTop;
|
||||
if (hiddenRef.current && el.scrollTop < TOP_ZONE_PX) set(false);
|
||||
}, LOCK_MS);
|
||||
};
|
||||
|
||||
const onScroll = () => {
|
||||
@@ -51,12 +63,15 @@ export function useHideOnScroll(scrollRef, enabled) {
|
||||
}
|
||||
// Копим путь только в одном направлении: смена направления сбрасывает счётчик
|
||||
acc = Math.sign(delta) === Math.sign(acc) ? acc + delta : delta;
|
||||
if (acc > THRESHOLD_PX) set(true);
|
||||
if (acc > THRESHOLD_PX && el.scrollHeight - el.clientHeight > MIN_SCROLLABLE_PX) set(true);
|
||||
else if (acc < -THRESHOLD_PX) set(false);
|
||||
};
|
||||
|
||||
el.addEventListener("scroll", onScroll, { passive: true });
|
||||
return () => el.removeEventListener("scroll", onScroll);
|
||||
return () => {
|
||||
el.removeEventListener("scroll", onScroll);
|
||||
clearTimeout(recheckTimer);
|
||||
};
|
||||
}, [scrollRef, enabled]);
|
||||
|
||||
return hidden;
|
||||
|
||||
@@ -163,8 +163,7 @@
|
||||
"openFilter": "Filter by ad type",
|
||||
"openMenu": "Open menu",
|
||||
"filterCitiesPlaceholder": "Select city…",
|
||||
"filterCitiesEmpty": "No cities found",
|
||||
"filterCitiesAdd": "Add \"{{city}}\"",
|
||||
"filterCitiesEmpty": "No ads in this city",
|
||||
"filterCitiesRemove": "Remove city",
|
||||
"filterCitiesClear": "Reset cities",
|
||||
"filterCitiesTitle": "Cities",
|
||||
|
||||
@@ -163,8 +163,7 @@
|
||||
"openFilter": "Фильтр по типу объявления",
|
||||
"openMenu": "Открыть меню",
|
||||
"filterCitiesPlaceholder": "Выберите город…",
|
||||
"filterCitiesEmpty": "Города не найдены",
|
||||
"filterCitiesAdd": "Добавить «{{city}}»",
|
||||
"filterCitiesEmpty": "В этом городе нет объявлений",
|
||||
"filterCitiesRemove": "Убрать город",
|
||||
"filterCitiesClear": "Сбросить города",
|
||||
"filterCitiesTitle": "Города",
|
||||
@@ -172,7 +171,7 @@
|
||||
"filterCitiesApply": "Показать",
|
||||
"filterCitiesReset": "Сбросить",
|
||||
"filterCitiesOpen": "Выбрать города",
|
||||
"emptyCityTitle": "В этом городе объявлений нет",
|
||||
"emptyCityTitle": "В этом городе нет объявлений",
|
||||
"emptyCitySub": "Попробуйте выбрать другой город",
|
||||
"confirmDelete": "Вы уверены, что хотите удалить это объявление?",
|
||||
"confirmMarkFound": "Отметить это объявление как \"Вещь найдена\"?"
|
||||
|
||||
@@ -460,7 +460,6 @@
|
||||
}
|
||||
.f-city-ms-option:hover { background: var(--hover-bg); }
|
||||
.f-city-ms-option.selected { color: var(--teal); font-weight: 600; }
|
||||
.f-city-ms-option.f-city-ms-add { color: var(--teal); font-weight: 600; }
|
||||
|
||||
.f-city-ms-check {
|
||||
width: 18px; height: 18px; flex-shrink: 0;
|
||||
@@ -472,9 +471,6 @@
|
||||
border-color: var(--teal);
|
||||
background: rgba(var(--teal-rgb), .12);
|
||||
}
|
||||
.f-city-ms-option.f-city-ms-add .f-city-ms-check {
|
||||
border-color: rgba(var(--teal-rgb), .5);
|
||||
}
|
||||
|
||||
.f-city-ms-empty {
|
||||
padding: 10px; text-align: center; color: var(--muted); font-size: 0.85rem;
|
||||
@@ -717,8 +713,6 @@
|
||||
transition: background .15s, border-color .15s;
|
||||
}
|
||||
.f-sheet-row.selected .f-sheet-check { background: var(--teal); border-color: var(--teal); }
|
||||
.f-sheet-row-add { color: var(--teal); }
|
||||
.f-sheet-row-add .f-sheet-check { border-color: var(--teal); color: var(--teal); }
|
||||
.f-sheet-row-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.f-sheet-empty { padding: 24px 0; text-align: center; color: var(--muted); font-size: 0.9rem; }
|
||||
.f-sheet-actions {
|
||||
|
||||
Reference in New Issue
Block a user