import Link from "next/link"; import type { MarketListing, MarketNegotiation } from "@/payload-types"; import { Item, ItemContent, ItemHeader, ItemTitle } from "@/components/ui/item"; import { ChevronRightIcon, HandshakeIcon } from "lucide-react"; import { formatAmount, type CurrencyConfig } from "@/lib/banking/format"; interface OpenNegotiationsProps { readonly negotiations: MarketNegotiation[]; readonly currentUserId: number; readonly currencyConfig: CurrencyConfig; } function listingOf(negotiation: MarketNegotiation): MarketListing | null { return typeof negotiation.listing === "object" && negotiation.listing ? (negotiation.listing as MarketListing) : null; } function assetNameOf(listing: MarketListing | null): string { const asset = listing?.asset; return typeof asset === "object" && asset ? asset.name : "Unknown item"; } function buyerIdOf(negotiation: MarketNegotiation): number | null { return typeof negotiation.buyer === "object" && negotiation.buyer ? negotiation.buyer.id : negotiation.buyer; } export function OpenNegotiations({ negotiations, currentUserId, currencyConfig, }: OpenNegotiationsProps) { // Deliberately hidden when there is nothing on the table (same convention as // NegotiationPanel on the market page). if (negotiations.length === 0) return null; const fmt = (n: number) => formatAmount(n, currencyConfig); return (
Negotiations {negotiations.length} open
Market
{negotiations.map((negotiation) => { const listing = listingOf(negotiation); const role = buyerIdOf(negotiation) === currentUserId ? "buyer" : "seller"; const theirProposal = (role === "buyer" && negotiation.proposedBy === "seller") || (role === "seller" && negotiation.proposedBy === "buyer"); const quantity = negotiation.quantity ?? listing?.quantity ?? 1; return (
{assetNameOf(listing)} {role === "buyer" ? "Buying" : "Selling"}
{fmt(negotiation.amount ?? 0)} each · {quantity}× ·{" "} {theirProposal ? "their offer" : "your offer"}
); })}
); }