"use client"; import { useEffect, useMemo, useState } from "react"; import { useAuth } from "@/components/AuthProvider"; import { listBusinesses } from "@/services/marketplace"; import { getMeshkeeBusiness, } from "@/services/meshkee-businesses"; import { getFirstName } from "@/lib/userName"; import type { MarketplaceBusiness } from "@/types/business"; import styles from "./HomeDashboard.module.css"; type RecentRow = { id: string; name: string; category: string; status: string; statusKey: MarketplaceBusiness["status"]; }; const STATUS_LABEL: Record = { draft: "Draft", pending_review: "Review", published: "Published", hidden: "Hidden", rejected: "Rejected", }; export default function HomeDashboard() { const { user } = useAuth(); const firstName = getFirstName(user); const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); useEffect(() => { let cancelled = false; void (async () => { setLoading(true); setError(""); try { const data = await listBusinesses({ status: "all", limit: 100 }); if (cancelled) return; const sorted = [...data.items].sort((a, b) => { const aTime = Date.parse(a.updatedAt || a.createdAt || "") || 0; const bTime = Date.parse(b.updatedAt || b.createdAt || "") || 0; return bTime - aTime; }); const pending = data.items.filter( (item) => item.status === "pending_review", ); const enrichIds = new Set( [ ...sorted.slice(0, 4).map((item) => item.id), ...pending.slice(0, 4).map((item) => item.id), ], ); const enrichedEntries = await Promise.all( data.items .filter((item) => enrichIds.has(item.id)) .map(async (item) => { try { const meshkee = await getMeshkeeBusiness( String(item.meshkeeBusinessId), ); return [ item.id, { ...item, name: meshkee.name, categoryName: meshkee.categories[0]?.name, }, ] as const; } catch { return [item.id, { ...item, name: item.slug }] as const; } }), ); const byId = new Map(enrichedEntries); setItems(data.items.map((item) => byId.get(item.id) ?? item)); } catch (err) { if (cancelled) return; setError( err instanceof Error ? err.message : "Could not load marketplace data.", ); setItems([]); } finally { if (!cancelled) setLoading(false); } })(); return () => { cancelled = true; }; }, []); const stats = useMemo(() => { const published = items.filter((item) => item.status === "published").length; const pending = items.filter( (item) => item.status === "pending_review", ).length; const draft = items.filter((item) => item.status === "draft").length; return [ { id: "businesses", label: "Live", value: loading ? "…" : String(published), hint: "Published on marketplace", href: "/businesses?status=published", }, { id: "pending", label: "Pending", value: loading ? "…" : String(pending), hint: "Awaiting review", href: "/businesses?status=pending_review", }, { id: "draft", label: "Draft", value: loading ? "…" : String(draft), hint: "Not submitted yet", href: "/businesses?status=draft", }, { id: "total", label: "Total", value: loading ? "…" : String(items.length), hint: "All linked listings", href: "/businesses", }, ]; }, [items, loading]); const recent: RecentRow[] = useMemo( () => [...items] .sort((a, b) => { const aTime = Date.parse(a.updatedAt || a.createdAt || "") || 0; const bTime = Date.parse(b.updatedAt || b.createdAt || "") || 0; return bTime - aTime; }) .slice(0, 4) .map((item) => ({ id: item.id, name: item.name || item.slug, category: item.categoryName || "Uncategorized", status: STATUS_LABEL[item.status], statusKey: item.status, })), [items], ); const pendingItems = useMemo( () => items.filter((item) => item.status === "pending_review").slice(0, 4), [items], ); return (

Overview · {firstName}

Marketplace command

Track listings, review submissions, and keep the public marketplace gate in sync.

IranBay Admin
{stats.map((stat) => (

{stat.label}

{stat.value}

{stat.hint}

))}
{error ? (

{error}

) : null}

Recent businesses

Latest marketplace listings

View all
    {loading && recent.length === 0 ? (
  • Loading…

  • ) : null} {!loading && recent.length === 0 ? (
  • No listings yet

  • ) : null} {recent.map((item) => (
  • {item.name}

    {item.category}

    {item.status}
  • ))}

Pending review

Approve new shop submissions

Review
    {loading && pendingItems.length === 0 ? (
  • Loading…

  • ) : null} {!loading && pendingItems.length === 0 ? (
  • Queue clear

    No submissions waiting

  • ) : null} {pendingItems.map((item) => (
  • {item.name || item.slug}

    /{item.slug} · awaiting approval

    Open
  • ))}

Quick actions

Jump into common admin work

Review pending

Approve new marketplace listings

Manage businesses

List, edit, publish, or hide

Open website

Preview the public gate

); }