// Combo plans: an owner who opened Sales from the Service portal can jump back without signing in. // SalesServer issues a 60s one-time code (POST /api/service-sso, only for branch tokens with via: 'service'); // the Service app's /service-sso page swaps it for the Service session. Rendered in the desktop and mobile headers. function SwitchToServiceButton({ salesUrl, token, compact }) { const [busy, setBusy] = React.useState(false); const serviceUrl = String((window.ENV_CONFIG && window.ENV_CONFIG.SERVICE_APP_URL) || '').replace(/\/+$/, ''); if (!serviceUrl) return null; const go = async () => { if (busy) return; setBusy(true); try { const res = await fetch(salesUrl + '/api/service-sso', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + (token || localStorage.getItem('branch_token') || '') } }); const data = await res.json().catch(() => ({})); if (!res.ok || !data.code) throw new Error(data.message || 'Could not open Service'); window.location.href = serviceUrl + '/service-sso?code=' + encodeURIComponent(data.code); } catch (e) { setBusy(false); alert(e.message || 'Could not open Service. Please try again.'); } }; return ( ); } window.SwitchToServiceButton = SwitchToServiceButton;