"use client"; import { useEffect, useMemo, useState } from "react"; import Link from "next/link"; import { useSearchParams } from "next/navigation"; import { AuthGuard } from "@/components/AuthGuard"; import { Button, SectionTitle } from "@/components/ui"; import { useMe } from "@/hooks/useMe"; import { loadCommercialAssets, loadCommercialAutomationPacks, loadCommercialBundles, loadCommercialExtensions, loadCommercialMetadata, loadCommercialProducts, } from "../adapters"; import type { AdapterResult, CommercialAsset, CommercialAutomationPack, CommercialBundle, CommercialExtension, CommercialMetadataBag, CommercialProduct, } from "../types"; import { BundleCard, CommercialEmptyState, CommercialSkeleton, MetadataBagView, PlatformShell, ProductCard, RegistryBrowser, } from "../components"; import { loadLauncherPrefs, toggleFavorite, togglePinned, type LauncherPrefs, } from "../launcher-prefs"; const GROUP_ORDER = [ "Operations", "Sales", "Marketing", "Finance", "Customers", "Online Presence", "Automation", "AI", "operations", "sales", "marketing", "finance", "customers", "online", "automation", "ai", ]; function groupLabel(raw: string): string { const map: Record = { operations: "عملیات", sales: "فروش", marketing: "بازاریابی", finance: "مالی", customers: "مشتریان", "online presence": "حضور آنلاین", online: "حضور آنلاین", automation: "اتوماسیون", ai: "هوش مصنوعی", }; return map[raw.toLowerCase()] || raw; } function AppsInner() { const { me } = useMe(); const searchParams = useSearchParams(); const q = (searchParams.get("q") || "").trim().toLowerCase(); const [products, setProducts] = useState | null>(null); const [bundles, setBundles] = useState | null>(null); const [assets, setAssets] = useState | null>(null); const [extensions, setExtensions] = useState | null>(null); const [automation, setAutomation] = useState | null>( null ); const [meta, setMeta] = useState | null>(null); const [prefs, setPrefs] = useState({ favorites: [], pinned: [], recent: [] }); const [booted, setBooted] = useState(false); useEffect(() => { setPrefs(loadLauncherPrefs()); void Promise.all([ loadCommercialProducts(), loadCommercialBundles(), loadCommercialAssets(), loadCommercialExtensions(), loadCommercialAutomationPacks(), loadCommercialMetadata({ target_kind: "catalog" }), ]).then(([p, b, a, e, auto, m]) => { setProducts(p); setBundles(b); setAssets(a); setExtensions(e); setAutomation(auto); setMeta(m); setBooted(true); }); }, []); const byCode = useMemo(() => { const m = new Map(); for (const p of products?.data ?? []) m.set(p.product_code, p); return m; }, [products]); const pick = (codes: string[]) => codes.map((c) => byCode.get(c)).filter((p): p is CommercialProduct => Boolean(p)); const favorites = pick(prefs.favorites); const pinned = pick(prefs.pinned); const recent = pick(prefs.recent); const groups = useMemo(() => { let list = (products?.data ?? []).filter((p) => p.visibility !== "hidden"); if (q) { list = list.filter( (p) => p.display_name.toLowerCase().includes(q) || p.product_code.toLowerCase().includes(q) || (p.category || "").toLowerCase().includes(q) ); } const map = new Map(); for (const p of list) { const key = p.category?.trim() || "Operations"; const arr = map.get(key) || []; arr.push(p); map.set(key, arr); } const keys = Array.from(map.keys()).sort((a, b) => { const ia = GROUP_ORDER.findIndex((g) => g.toLowerCase() === a.toLowerCase()); const ib = GROUP_ORDER.findIndex((g) => g.toLowerCase() === b.toLowerCase()); return (ia === -1 ? 99 : ia) - (ib === -1 ? 99 : ib); }); return keys.map((k) => ({ key: k, label: groupLabel(k), items: map.get(k)! })); }, [products, q]); return (
{q ?

نتایج برای: {q}

: }
{!booted ? : null} {booted && pinned.length ? (
{pinned.map((p) => ( setPrefs(toggleFavorite(p.product_code))} onTogglePinned={() => setPrefs(togglePinned(p.product_code))} /> ))}
) : null} {booted && favorites.length ? (
{favorites.map((p) => ( setPrefs(toggleFavorite(p.product_code))} onTogglePinned={() => setPrefs(togglePinned(p.product_code))} /> ))}
) : null} {booted && recent.length ? (
{recent.map((p) => ( setPrefs(toggleFavorite(p.product_code))} onTogglePinned={() => setPrefs(togglePinned(p.product_code))} /> ))}
) : null} {booted && products?.availability === "ready" && groups.length ? (
{groups.map((g) => (
{g.items.map((p) => ( setPrefs(toggleFavorite(p.product_code))} onTogglePinned={() => setPrefs(togglePinned(p.product_code))} /> ))}
))}
) : booted ? ( ) : null}
{bundles?.availability === "ready" && bundles.data.length ? (
{bundles.data.map((b) => ( ))}
) : ( )}
); } export function CommercialAppsPage() { return ( ); }