import { useState } from "react"; import { AdminButton } from "@/components/admin/design-system"; import { type WholesaleSettings, type NotificationRecipient, saveWholesaleSettings, } from "@/actions/wholesale"; import AddRecipientForm from "./AddRecipientForm"; import WebhookSettingsSection from "./WebhookSettingsSection"; import type { MsgFn } from "./types"; interface SettingsTabProps { settings: WholesaleSettings | null; brandId: string; onMsg: MsgFn; onRefresh: () => void; canManageSettings: boolean; } // Wholesale portal settings: approval flow, payments, pickup location, invoice // details, Square sync toggle, team notification recipients, and outbound webhook. export default function SettingsTab({ settings, brandId, onMsg, onRefresh, canManageSettings }: SettingsTabProps) { // Hooks must be called unconditionally - always declare them first const [form, setForm] = useState({ requireApproval: settings?.require_approval ?? true, minOrderAmount: settings?.min_order_amount ?? "", onlinePaymentEnabled: settings?.online_payment_enabled ?? false, wholesaleEnabled: settings?.wholesale_enabled ?? true, squareSyncEnabled: settings?.square_sync_enabled ?? false, pickupLocation: settings?.pickup_location ?? "", fobLocation: settings?.fob_location ?? "", fromEmail: settings?.from_email ?? "", invoiceBusinessName: settings?.invoice_business_name ?? "", invoiceBusinessAddress: settings?.invoice_business_address ?? "", invoiceBusinessPhone: settings?.invoice_business_phone ?? "", invoiceBusinessEmail: settings?.invoice_business_email ?? "", invoiceBusinessWebsite: settings?.invoice_business_website ?? "", notificationRecipients: settings?.notification_recipients ?? [], }); const [saving, setSaving] = useState(false); // Permission check after hooks if (!canManageSettings) { return (
You do not have permission to manage settings.
Require Approval
New registrations must be manually approved.
Wholesale Portal
Show the Wholesale Portal card on this brand's storefront page.
Square Sync for Wholesale
When enabled, wholesale product changes are automatically pushed to Square. {form.squareSyncEnabled ? " Auto-sync is active." : " Auto-sync is disabled — changes will not be pushed to Square."}
These team members receive all wholesale notifications for this brand — new orders, deposits, fulfillments, price sheets, and pickup reminders. If no recipients are active, the system falls back to any configured notification email on file.
{/* Empty state */} {form.notificationRecipients.length === 0 && (No recipients added yet.
Add an email address below to get started.
{r.email}
{r.name &&{r.name}
} {r.notification_types && r.notification_types.length > 0 && (Types: {r.notification_types.join(", ")}
)}Send order events to external systems (Harvest Point, ERPs, etc.). Payload is signed with HMAC-SHA256. Enable and configure the URL and secret below.