Files
nahodka-front/src/components/RightSidebar.jsx
2026-10-23 00:02:44 +03:00

161 lines
7.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import ProfileAvatar from "./profile/ProfileAvatar";
import SupportModal from "./SupportModal";
import { DesktopIcons } from "./desktop/DesktopIcons";
import { getImageUrl } from "../config/authConstants";
import { POST_STATUS_CLOSED } from "../config/postConstants";
import { formatRelativeTime } from "../utils/relativeTime";
const FRESH_LIMIT = 5;
/**
* RightSidebar — правая колонка десктопной ленты (>= 1280px, ниже скрыта CSS):
* карточка профиля, CTA размещения, переход на карту и свежие объявления.
* На мобильной раскладке не отображается.
*
* @param {{
* ads: any[], profileData: any, isAuthenticated: boolean,
* onProfileClick: () => void, onLoginClick: () => void, onNavClick?: (key: string) => void,
* onCreatePost?: () => void, onMapClick?: () => void, onPostClick?: (id: any) => void,
* onRulesClick?: () => void, onReviewsClick?: () => void,
* }} props
*/
export default function RightSidebar({
ads,
profileData,
isAuthenticated,
onProfileClick,
onLoginClick,
onNavClick,
onCreatePost,
onMapClick,
onPostClick,
onRulesClick,
onReviewsClick,
}) {
const { t, i18n } = useTranslation();
const [supportOpen, setSupportOpen] = useState(false);
// Свежие активные объявления: по дате создания, найденные не показываем.
const fresh = useMemo(() => (
[...(ads || [])]
.filter((ad) => ad.status !== POST_STATUS_CLOSED)
.sort((a, b) => new Date(b.createdAt || 0) - new Date(a.createdAt || 0))
.slice(0, FRESH_LIMIT)
), [ads]);
const profileName = isAuthenticated
? `${profileData?.firstName || ""} ${profileData?.lastName || ""}`.trim() || profileData?.login
: null;
return (
<aside className="f-right fd-right">
{/* Профиль / гость */}
<section className="fd-card fd-profile-card">
{isAuthenticated ? (
<button type="button" className="fd-profile-row" onClick={onProfileClick}>
<ProfileAvatar avatar={profileData?.avatar} name={profileName} />
<span className="fd-profile-info">
<span className="fd-profile-name">{profileName}</span>
<span className="fd-profile-sub">@{profileData?.login}</span>
</span>
<DesktopIcons.ChevronRight size={18} />
</button>
) : (
<div className="fd-guest">
<div className="fd-guest-icon"><DesktopIcons.User size={22} /></div>
<div className="fd-guest-text">
<div className="fd-guest-title">{t("desktop.guestTitle")}</div>
<div className="fd-guest-sub">{t("desktop.guestText")}</div>
</div>
<button type="button" className="fd-btn fd-btn-primary fd-btn-block" onClick={onLoginClick}>
{t("feed.login")}
</button>
</div>
)}
</section>
{/* CTA размещения */}
<section className="fd-card fd-cta-card">
<div className="fd-cta-glow" aria-hidden="true" />
<div className="fd-cta-icon"><DesktopIcons.Sparkles size={20} /></div>
<div className="fd-cta-title">{t("desktop.ctaTitle")}</div>
<div className="fd-cta-text">{t("desktop.ctaText")}</div>
<button type="button" className="fd-btn fd-btn-brand fd-btn-block fd-cta-btn" onClick={onCreatePost}>
<DesktopIcons.Plus size={18} />
<span className="fd-cta-btn-label">{t("desktop.createPost")}</span>
</button>
</section>
{/* Карта */}
<button type="button" className="fd-card fd-map-card" onClick={onMapClick}>
<span className="fd-map-art" aria-hidden="true">
<span className="fd-map-road fd-map-road-1" />
<span className="fd-map-road fd-map-road-2" />
<span className="fd-map-road fd-map-road-3" />
<span className="fd-map-pin fd-map-pin-loss" style={{ left: "22%", top: "38%" }} />
<span className="fd-map-pin fd-map-pin-found" style={{ left: "58%", top: "24%" }} />
<span className="fd-map-pin fd-map-pin-loss" style={{ left: "72%", top: "62%" }} />
<span className="fd-map-pin fd-map-pin-found" style={{ left: "36%", top: "70%" }} />
</span>
<span className="fd-map-body">
<span className="fd-map-title">{t("desktop.mapCardTitle")}</span>
<span className="fd-map-text">{t("desktop.mapCardText")}</span>
<span className="fd-map-link">
{t("desktop.mapCardBtn")}
<DesktopIcons.ArrowRight size={16} />
</span>
</span>
</button>
{/* Свежие объявления */}
<section className="fd-card fd-fresh">
<div className="fd-card-head">
<span className="fd-card-title">{t("desktop.fresh")}</span>
<button type="button" className="fd-link-btn" onClick={() => onNavClick?.("home")}>
{t("desktop.seeAll")}
</button>
</div>
{fresh.length === 0 ? (
<div className="fd-fresh-empty">{t("desktop.freshEmpty")}</div>
) : (
<ul className="fd-fresh-list">
{fresh.map((ad) => {
const img = Array.isArray(ad.images) && ad.images[0] ? getImageUrl(ad.images[0]) : null;
const when = formatRelativeTime(ad.createdAt, i18n.language);
return (
<li key={ad.id}>
<button type="button" className="fd-fresh-item" onClick={() => onPostClick?.(ad.id)}>
<span className="fd-fresh-thumb">
{img ? <img src={img} alt="" loading="lazy" decoding="async" /> : <span>{ad.emoji || "📋"}</span>}
<span className={`fd-fresh-type fd-fresh-type-${ad.type === "found" ? "found" : "loss"}`} />
</span>
<span className="fd-fresh-info">
<span className="fd-fresh-title">{ad.title}</span>
<span className="fd-fresh-sub">
{ad.type === "found" ? t("postCard.found") : t("postCard.loss")}
{ad.city && <> · {ad.city}</>}
{when && <> · {when}</>}
</span>
</span>
</button>
</li>
);
})}
</ul>
)}
</section>
<footer className="fd-right-foot">
<button type="button" onClick={onRulesClick}>{t("rightSidebar.footRules")}</button>
<button type="button" onClick={onReviewsClick}>{t("nav.reviews")}</button>
<button type="button" onClick={() => setSupportOpen(true)}>{t("rightSidebar.footSupport")}</button>
<span>© {new Date().getFullYear()} {t("app.name")}</span>
</footer>
{supportOpen && <SupportModal onClose={() => setSupportOpen(false)} />}
</aside>
);
}