"use client"; import { useEffect, useMemo, useState } from "react"; import Link from "next/link"; import { useParams } from "next/navigation"; import { AuthGuard } from "@/components/AuthGuard"; import { Button } from "@/components/ui"; import { useMe } from "@/hooks/useMe"; import { loadCommercialProducts, loadSetupChecklistFromApi } from "../adapters"; import type { AdapterResult, CommercialProduct, SetupChecklistItem } from "../types"; import { resolveProductLaunchUrl } from "../types"; import { CommercialEmptyState, CommercialSkeleton, PlatformShell, SetupChecklist, } from "../components"; import { pushRecent } from "../launcher-prefs"; /** * Generic first-run product setup wizard. * Steps come from commercial checklist API filtered by product — no vertical hardcoding. */ function SetupWizardInner() { const params = useParams(); const productCode = decodeURIComponent(String(params.productCode || "")); const { me } = useMe(); const [product, setProduct] = useState(null); const [productsAvail, setProductsAvail] = useState("loading"); const [checklist, setChecklist] = useState([]); const [pct, setPct] = useState(null); const [step, setStep] = useState(0); const [loading, setLoading] = useState(true); useEffect(() => { void (async () => { setLoading(true); const prods = await loadCommercialProducts(); setProductsAvail(prods.availability); const found = (prods.data || []).find((p) => p.product_code === productCode) || null; setProduct(found); if (found) pushRecent(found.product_code); if (me?.current_tenant_id) { const chk = await loadSetupChecklistFromApi(me.current_tenant_id); const items = (chk.data || []).filter( (i) => !i.product_code || i.product_code === productCode || i.id.includes(productCode) ); // If API returned general checklist only, synthesize product-aware steps from metadata if (!items.length && found) { const metaSteps = Array.isArray(found.metadata?.setup_steps) ? (found.metadata!.setup_steps as Array>) : []; if (metaSteps.length) { setChecklist( metaSteps.map((s, idx) => ({ id: String(s.id || `meta-${idx}`), label: String(s.label || s.title || `گام ${idx + 1}`), href: typeof s.href === "string" ? s.href : resolveProductLaunchUrl(found), product_code: productCode, done: Boolean(s.done), required: Boolean(s.required ?? true), })) ); } else { setChecklist([ { id: "open", label: `باز کردن ${found.display_name}`, href: resolveProductLaunchUrl(found), product_code: productCode, required: true, }, { id: "branding", label: "بررسی برندینگ workspace", href: "/dashboard/settings", required: false, }, { id: "billing", label: "تأیید trial / اشتراک", href: "/billing", required: false, }, ]); } } else { setChecklist(items); setPct( (chk as AdapterResult & { completion_percent?: number }) .completion_percent ?? null ); } } setLoading(false); })(); }, [productCode, me?.current_tenant_id]); const current = checklist[step]; const launch = product ? resolveProductLaunchUrl(product) : null; const progress = useMemo(() => { if (pct != null) return pct; if (!checklist.length) return 0; return Math.round(((step + 1) / checklist.length) * 100); }, [pct, checklist.length, step]); return ( {loading ? : null} {!loading && !product ? ( ) : null} {!loading && product ? (

پیشرفت {progress}%

{current ? (

گام {step + 1} از {checklist.length}

{current.label}

{current.href ? ( ) : null} {step < checklist.length - 1 ? ( ) : ( )} {step > 0 ? ( ) : null}
) : ( )}
) : null} ); } export function ProductSetupWizardPage() { return ( ); }