added button back from map and fix post on map

This commit is contained in:
SlimusMinus
2026-09-07 01:32:22 +03:00
parent 3299df14c6
commit f0a7327a1a
6 changed files with 140 additions and 11 deletions

View File

@@ -55,6 +55,9 @@ export default function MainFeed() {
const [favoritesLoading, setFavoritesLoading] = useState(false);
const feedRef = useRef(null);
// Состояние ленты перед переходом на карту — чтобы вернуть юзера на то же место
const feedStateRef = useRef({ scrollTop: 0, url: "/feed", activeNav: "home" });
const restoreScrollRef = useRef(false);
const isSearchView = new URLSearchParams(location.search).get("view") === "search";
const isMapView = new URLSearchParams(location.search).get("view") === "map";
@@ -230,18 +233,44 @@ export default function MainFeed() {
}, [isSearchView, isMapView, navigate]);
// Карта с объявлениями
const saveFeedState = useCallback(() => {
feedStateRef.current = {
scrollTop: feedRef.current?.scrollTop ?? 0,
url: `${location.pathname}${location.search}`,
activeNav,
};
restoreScrollRef.current = true;
}, [location.pathname, location.search, activeNav]);
const handleMapClick = useCallback(() => {
saveFeedState();
setActiveNav("map");
if (!isMapView) navigate("/feed?view=map");
feedRef.current?.scrollTo({ top: 0, behavior: "smooth" });
}, [isMapView, navigate]);
}, [isMapView, navigate, saveFeedState]);
const handleMapPostClick = useCallback((postId) => {
modals.open("postDetail", { postId });
}, [modals]);
// Возврат с карты на ленту — на то же место, где юзер был до открытия карты
const handleMapBack = useCallback(() => {
const { url, activeNav: prevNav } = feedStateRef.current;
setActiveNav(prevNav || "home");
navigate(url || "/feed", { replace: true });
}, [navigate]);
// Восстанавливаем прокрутку ленты после возврата с карты
useEffect(() => {
if (isMapView || !restoreScrollRef.current) return;
const el = feedRef.current;
if (el) el.scrollTop = feedStateRef.current.scrollTop;
restoreScrollRef.current = false;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isMapView, feed.ads, cityFeed.cityAds, feed.loading, cityFeed.cityLoading]);
// Клик по локации на карточке — карта с фокусом на этом объявлении
const handleLocationClick = useCallback((ad) => {
saveFeedState();
const params = new URLSearchParams({ view: "map", post: ad.id });
if (ad.latitude != null && ad.longitude != null) {
params.set("lat", ad.latitude);
@@ -249,8 +278,7 @@ export default function MainFeed() {
}
setActiveNav("map");
navigate(`/feed?${params.toString()}`);
feedRef.current?.scrollTo({ top: 0, behavior: "smooth" });
}, [navigate]);
}, [navigate, saveFeedState]);
const handleProfileClick = useCallback(() => {
if (!isAuthenticated) modals.open("auth");
@@ -332,7 +360,13 @@ export default function MainFeed() {
<div className="f-wrap">
{isMapView ? (
<MapView focusPostId={focusPostId} focusLat={focusLat} focusLng={focusLng} onPostClick={handleMapPostClick} />
<MapView
focusPostId={focusPostId}
focusLat={focusLat}
focusLng={focusLng}
onPostClick={handleMapPostClick}
onBack={handleMapBack}
/>
) : (
<>
<div className="f-feed" ref={feedRef}>