"use client"; import { FormEvent, useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { AuthGuard } from "@/components/AuthGuard"; import { Button, Container } from "@/components/ui"; import { api, ApiError, type TenantContext } from "@/lib/api"; import { useMe } from "@/hooks/useMe"; type Step = "business" | "branding" | "domain" | "review"; const STEPS: { id: Step; label: string }[] = [ { id: "business", label: "اطلاعات کسب‌وکار" }, { id: "branding", label: "برندینگ" }, { id: "domain", label: "دامنه" }, { id: "review", label: "بازبینی و تکمیل" }, ]; function slugify(value: string): string { return value .trim() .toLowerCase() .replace(/\s+/g, "-") .replace(/[^a-z0-9-]/g, "") .replace(/-+/g, "-") .replace(/^-|-$/g, ""); } const inputClass = "mt-1 w-full rounded-xl border border-gray-200 px-4 py-2.5 focus:border-primary focus:outline-none focus:ring-1 focus:ring-primary disabled:bg-gray-50"; function StepIndicator({ current }: { current: Step }) { const currentIndex = STEPS.findIndex((s) => s.id === current); return (
    {STEPS.map((step, index) => { const isActive = step.id === current; const isDone = index < currentIndex; return (
  1. {index + 1} {step.label} {index < STEPS.length - 1 && /}
  2. ); })}
); } function OnboardingWizard() { const router = useRouter(); const { me, loading: meLoading } = useMe(); const [step, setStep] = useState("business"); const [ctx, setCtx] = useState(null); const [bootstrapping, setBootstrapping] = useState(true); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); // فرم گام ۱ const [businessName, setBusinessName] = useState(""); const [slug, setSlug] = useState(""); const [slugTouched, setSlugTouched] = useState(false); const [businessType, setBusinessType] = useState(""); // فرم گام ۲ const [primaryColor, setPrimaryColor] = useState("#0284c7"); const [secondaryColor, setSecondaryColor] = useState("#0f172a"); const [logoUrl, setLogoUrl] = useState(""); // فرم گام ۳ const [customDomain, setCustomDomain] = useState(""); // اگر کاربر tenant ناتمام داشته باشد، از همان‌جا ادامه بده؛ اگر onboarding کامل است برو به داشبورد. useEffect(() => { if (meLoading) return; if (!me) { setBootstrapping(false); return; } if (!me.onboarding_required) { router.replace("/dashboard"); return; } if (me.current_tenant_id) { api.tenantContext .current() .then((tenantCtx) => { setCtx(tenantCtx); setStep("branding"); }) .catch(() => { /* اگر دریافت نشد، از ابتدا شروع کن */ }) .finally(() => setBootstrapping(false)); } else { setBootstrapping(false); } }, [me, meLoading, router]); const handleBusinessNameChange = (value: string) => { setBusinessName(value); if (!slugTouched) setSlug(slugify(value)); }; const handleCreateTenant = async (e: FormEvent) => { e.preventDefault(); setError(""); setLoading(true); try { const result = await api.onboarding.createTenant({ business_name: businessName, slug, business_type: businessType.trim() || undefined, }); setCtx(result); setStep("branding"); } catch (err) { setError( err instanceof ApiError ? err.message : "ایجاد workspace ناموفق بود" ); } finally { setLoading(false); } }; const handleBranding = async (e: FormEvent) => { e.preventDefault(); if (!ctx) return; setError(""); setLoading(true); try { const result = await api.onboarding.updateBranding(ctx.tenant.id, { primary_color: primaryColor || undefined, secondary_color: secondaryColor || undefined, logo_url: logoUrl.trim() || undefined, }); setCtx(result); setStep("domain"); } catch (err) { setError(err instanceof ApiError ? err.message : "ذخیره برندینگ ناموفق بود"); } finally { setLoading(false); } }; const handleDomain = async (e: FormEvent) => { e.preventDefault(); if (!ctx) return; setError(""); setLoading(true); try { let result = ctx; if (customDomain.trim()) { result = await api.onboarding.updateDomain(ctx.tenant.id, { custom_domain: customDomain.trim(), }); setCtx(result); } setStep("review"); } catch (err) { setError(err instanceof ApiError ? err.message : "ثبت دامنه ناموفق بود"); } finally { setLoading(false); } }; const handleComplete = async () => { if (!ctx) return; setError(""); setLoading(true); try { await api.onboarding.complete(ctx.tenant.id); router.push("/dashboard"); } catch (err) { setError(err instanceof ApiError ? err.message : "تکمیل onboarding ناموفق بود"); setLoading(false); } }; if (meLoading || bootstrapping) { return (

در حال بارگذاری...

); } return (

راه‌اندازی workspace

چند مرحله ساده تا آماده‌شدن workspace شما باقی مانده است.

{error && (

{error}

)}
{step === "business" && (
)} {step === "branding" && ctx && (
)} {step === "domain" && ctx && (

زیردامنه اختصاصی

{ctx.primary_domain || `${ctx.tenant.slug}.your-domain`}

)} {step === "review" && ctx && (
نام کسب‌وکار
{ctx.tenant.name}
آدرس
{ctx.tenant.slug}
زیردامنه
{ctx.primary_domain || "—"}
پلن
{ctx.plan_name || "—"}
رنگ اصلی
{ctx.tenant.primary_color || "—"}
)}
); } export default function OnboardingPage() { return ( ); }