84 lines
2.8 KiB
JavaScript
84 lines
2.8 KiB
JavaScript
/* eslint-disable react-refresh/only-export-components */
|
|
import { createContext, useContext, useState, useEffect, useCallback } from 'react';
|
|
import apiClient from '../api/client';
|
|
import { getRuntimeCompanyId } from '../utils/runtimeContext';
|
|
|
|
const BusinessContext = createContext(null);
|
|
|
|
const SESSION_KEY = 'sms_active_business';
|
|
|
|
export function BusinessProvider({ children }) {
|
|
const [activeBusiness, setActiveBusinessState] = useState(null);
|
|
const [hasGlobalSms, setHasGlobalSms] = useState(false);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
// On mount: rehydrate from sessionStorage and refresh from API
|
|
useEffect(() => {
|
|
async function rehydrate() {
|
|
const stored = sessionStorage.getItem(SESSION_KEY);
|
|
if (!stored) { setLoading(false); return; }
|
|
try {
|
|
const { businessId, companyId } = JSON.parse(stored);
|
|
const runtimeCompanyId = getRuntimeCompanyId();
|
|
|
|
if (runtimeCompanyId && companyId && runtimeCompanyId !== companyId) {
|
|
throw new Error('Stored business belongs to a different company context');
|
|
}
|
|
|
|
const [bizRes, smsRes] = await Promise.all([
|
|
apiClient.get(`/api/businesses/${businessId}`),
|
|
apiClient.get(`/api/businesses/${businessId}/global-sms/active`).catch(() => ({ data: {} }))
|
|
]);
|
|
setActiveBusinessState(bizRes.data);
|
|
setHasGlobalSms(!!smsRes.data?.activeProfile);
|
|
sessionStorage.setItem(SESSION_KEY, JSON.stringify({
|
|
businessId,
|
|
companyId: runtimeCompanyId || companyId || '',
|
|
}));
|
|
} catch {
|
|
// Business no longer exists — clear stale session
|
|
sessionStorage.removeItem(SESSION_KEY);
|
|
setActiveBusinessState(null);
|
|
setHasGlobalSms(false);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
rehydrate();
|
|
}, []);
|
|
|
|
const setActiveBusiness = useCallback(async (business) => {
|
|
setActiveBusinessState(business);
|
|
sessionStorage.setItem(SESSION_KEY, JSON.stringify({
|
|
businessId: business.businessId,
|
|
companyId: getRuntimeCompanyId(),
|
|
}));
|
|
try {
|
|
const smsRes = await apiClient.get(`/api/businesses/${business.businessId}/global-sms/active`);
|
|
setHasGlobalSms(!!smsRes.data?.activeProfile);
|
|
} catch {
|
|
setHasGlobalSms(false);
|
|
}
|
|
}, []);
|
|
|
|
const clearBusiness = useCallback(() => {
|
|
setActiveBusinessState(null);
|
|
setHasGlobalSms(false);
|
|
sessionStorage.removeItem(SESSION_KEY);
|
|
}, []);
|
|
|
|
const activeBusinessId = activeBusiness?.businessId || null;
|
|
|
|
return (
|
|
<BusinessContext.Provider value={{
|
|
activeBusiness, activeBusinessId, setActiveBusiness, clearBusiness, loading, hasGlobalSms, setHasGlobalSms
|
|
}}>
|
|
{children}
|
|
</BusinessContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useBusiness() {
|
|
return useContext(BusinessContext);
|
|
}
|