"use client" import { useEffect, useState } from "react" import { salesConfigApi } from "@/lib/api/sales-config" /** Which optional customer fields Settings -> Sales Configuration has switched on. */ export interface CustomerFieldConfig { phone: boolean email: boolean address: boolean city: boolean country: boolean creditLimit: boolean creditDays: boolean } /** * Everything on — the default before this became configurable, and what a screen shows until the real switches arrive * (or if they can't be read). The server refuses a value for a field that is off, so a wrong guess is caught there. */ export const CUSTOMER_FIELDS_ON: CustomerFieldConfig = { phone: true, email: true, address: true, city: true, country: true, creditLimit: true, creditDays: true, } /** Loads the optional-customer-field switches from the Sales configuration. */ export function useCustomerConfig(): { fields: CustomerFieldConfig; loaded: boolean } { const [fields, setFields] = useState(CUSTOMER_FIELDS_ON) const [loaded, setLoaded] = useState(false) useEffect(() => { let active = true salesConfigApi .get() .then(({ data }) => { if (!active) return setFields({ phone: data.customerPhoneEnabled, email: data.customerEmailEnabled, address: data.customerAddressEnabled, city: data.customerCityEnabled, country: data.customerCountryEnabled, creditLimit: data.customerCreditLimitEnabled, creditDays: data.customerCreditDaysEnabled, }) }) .catch(() => {}) .finally(() => { if (active) setLoaded(true) }) return () => { active = false } }, []) return { fields, loaded } }