"use client"; import { useEffect, useState } from "react"; import Link from "next/link"; import { useI18n } from "../../components/I18nProvider"; type MyListing = { id: string; status: string; translations: { title: string; slug: string; locale: string }[]; }; export default function MyListingsPage() { const { t } = useI18n(); const [listings, setListings] = useState([]); const [error, setError] = useState(null); const [loading, setLoading] = useState(true); const [message, setMessage] = useState(null); const [actionId, setActionId] = useState(null); useEffect(() => { fetch("/api/listings/mine", { cache: "no-store" }) .then((res) => res.json()) .then((data) => { if (data.error) { setError(data.error); } else { setListings(data.listings ?? []); } }) .catch(() => setError("Failed to load")) .finally(() => setLoading(false)); }, []); async function removeListing(listingId: string) { if (!window.confirm(t("removeConfirm"))) return; setActionId(listingId); setError(null); setMessage(null); try { const res = await fetch("/api/listings/remove", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ listingId }), }); const data = await res.json(); if (!res.ok) { setError(data.error || "Failed to remove listing"); } else { setMessage(t("removed")); setListings((prev) => prev.map((l) => l.id === listingId ? { ...l, status: "REMOVED" } : l, ), ); } } catch (e) { setError("Failed to remove listing"); } finally { setActionId(null); } } if (loading) { return (

{t("myListingsTitle")}

{t("loading")}

); } if (error) { return (

{t("myListingsTitle")}

{error}

); } return (

{t("myListingsTitle")}

{message ?

{message}

: null}
{t("createNewListing")}
{listings.length === 0 ? (

{t("noListings")} {t("createOne")}.

) : (
    {listings.map((l) => (
  • {l.translations[0]?.title ?? "Listing"} —{" "} {t("statusLabel")}: {l.status}
    {t("slugsLabel")}:{" "} {l.translations .map((t) => `${t.slug} (${t.locale})`) .join(", ")}
    {t("edit")} {l.status !== "DRAFT" ? ( {t("view")} ) : null}
  • ))}
)}
); }