import { useState, useEffect } from 'react'; import { api } from '../utils/api.js'; import { useToast } from '../contexts/ToastContext.jsx'; // ── Helpers ─────────────────────────────────────────────────────────────────── const APP_TYPES = { 'JAMA-Chat': { label: 'JAMA-Chat', desc: 'Chat only. No Branding, Group Manager or Schedule Manager.' }, 'JAMA-Brand': { label: 'JAMA-Brand', desc: 'Chat and Branding.' }, 'JAMA-Team': { label: 'JAMA-Team', desc: 'Chat, Branding, Group Manager and Schedule Manager.' }, }; // ── Team Management Tab ─────────────────────────────────────────────────────── function TeamManagementTab() { const toast = useToast(); const [userGroups, setUserGroups] = useState([]); const [toolManagers, setToolManagers] = useState([]); const [saving, setSaving] = useState(false); useEffect(() => { api.getUserGroups().then(({ groups }) => setUserGroups(groups || [])).catch(() => {}); api.getSettings().then(({ settings }) => { // Read from unified key, fall back to legacy key setToolManagers(JSON.parse(settings.team_tool_managers || settings.team_group_managers || '[]')); }).catch(() => {}); }, []); const toggle = (id) => { setToolManagers(prev => prev.includes(id) ? prev.filter(x => x !== id) : [...prev, id]); }; const handleSave = async () => { setSaving(true); try { await api.updateTeamSettings({ toolManagers }); toast('Team settings saved', 'success'); window.dispatchEvent(new Event('jama:settings-changed')); } catch (e) { toast(e.message, 'error'); } finally { setSaving(false); } }; return (
Tool Managers

Members of selected groups can access Group Manager, Schedule Manager, and User Manager. Admin users always have access to all three tools.

{userGroups.length === 0 ? (

No user groups created yet. Create groups in the Group Manager first.

) : (
{userGroups.map(g => ( ))}
)} {toolManagers.length === 0 && (

No groups selected — tools are admin-only.

)}
); } // ── Registration Tab ────────────────────────────────────────────────────────── function RegistrationTab({ onFeaturesChanged }) { const toast = useToast(); const [settings, setSettings] = useState({}); const [regCode, setRegCode] = useState(''); const [regLoading, setRegLoading] = useState(false); const [copied, setCopied] = useState(false); useEffect(() => { api.getSettings().then(({ settings }) => setSettings(settings)).catch(() => {}); }, []); const appType = settings.app_type || 'JAMA-Chat'; const activeCode = settings.registration_code || ''; const adminEmail = settings.admin_email || '—'; // Placeholder serial number derived from hostname const serialNumber = btoa(window.location.hostname).replace(/[^A-Z0-9]/gi, '').toUpperCase().slice(0, 16).padEnd(16, '0'); const handleCopySerial = async () => { await navigator.clipboard.writeText(serialNumber).catch(() => {}); setCopied(true); setTimeout(() => setCopied(false), 2000); }; const handleRegister = async () => { if (!regCode.trim()) return toast('Enter a registration code', 'error'); setRegLoading(true); try { const { features: f } = await api.registerCode(regCode.trim()); setRegCode(''); const fresh = await api.getSettings(); setSettings(fresh.settings); toast('Registration applied successfully.', 'success'); window.dispatchEvent(new Event('jama:settings-changed')); onFeaturesChanged && onFeaturesChanged(f); } catch (e) { toast(e.message || 'Invalid registration code', 'error'); } finally { setRegLoading(false); } }; const handleClear = async () => { try { const { features: f } = await api.registerCode(''); const fresh = await api.getSettings(); setSettings(fresh.settings); toast('Registration cleared.', 'success'); window.dispatchEvent(new Event('jama:settings-changed')); onFeaturesChanged && onFeaturesChanged(f); } catch (e) { toast(e.message, 'error'); } }; const typeInfo = APP_TYPES[appType] || APP_TYPES['JAMA-Chat']; const siteUrl = window.location.origin; return (
{/* Info box */}

Registration {activeCode ? 'is' : 'required:'}

JAMA {activeCode ? 'is' : 'will be'} registered to:
Type: {typeInfo.label}
URL: {siteUrl}

{/* Type */}
Application Type
{typeInfo.label}
{typeInfo.desc}
{/* Serial Number */}
Serial Number
{/* Registration Code */}
Registration Code
setRegCode(e.target.value)} onKeyDown={e => e.key === 'Enter' && handleRegister()} />
{activeCode && (
Registered — {typeInfo.label}
)}

Registration codes unlock application features. Contact your JAMA provider for a code.
JAMA-Brand — unlocks Branding.  JAMA-Team — unlocks Branding, Group Manager and Schedule Manager.

); } // ── Web Push Tab ────────────────────────────────────────────────────────────── function WebPushTab() { const toast = useToast(); const [vapidPublic, setVapidPublic] = useState(''); const [loading, setLoading] = useState(true); const [generating, setGenerating] = useState(false); const [showRegenWarning, setShowRegenWarning] = useState(false); useEffect(() => { api.getSettings().then(({ settings }) => { setVapidPublic(settings.vapid_public || ''); setLoading(false); }).catch(() => setLoading(false)); }, []); const doGenerate = async () => { setGenerating(true); setShowRegenWarning(false); try { const { publicKey } = await api.generateVapidKeys(); setVapidPublic(publicKey); toast('VAPID keys generated. Push notifications are now active.', 'success'); } catch (e) { toast(e.message || 'Failed to generate keys', 'error'); } finally { setGenerating(false); } }; if (loading) return

Loading…

; return (
Web Push Notifications (VAPID)
{vapidPublic ? (
Public Key
{vapidPublic}
Push notifications active
) : (

No VAPID keys found. Generate keys to enable Web Push notifications.

)} {showRegenWarning && (

⚠️ Regenerate VAPID keys?

Generating new keys will invalidate all existing push subscriptions. Users will need to re-enable notifications.

)} {!showRegenWarning && ( )}

Requires HTTPS. On iOS, the app must be installed to the home screen first.

); } // ── Main modal ──────────────────────────────────────────────────────────────── export default function SettingsModal({ onClose, onFeaturesChanged }) { const [tab, setTab] = useState('registration'); const [appType, setAppType] = useState('JAMA-Chat'); useEffect(() => { api.getSettings().then(({ settings }) => { setAppType(settings.app_type || 'JAMA-Chat'); }).catch(() => {}); const handler = () => api.getSettings().then(({ settings }) => setAppType(settings.app_type || 'JAMA-Chat')).catch(() => {}); window.addEventListener('jama:settings-changed', handler); return () => window.removeEventListener('jama:settings-changed', handler); }, []); const isTeam = appType === 'JAMA-Team'; const tabs = [ isTeam && { id: 'team', label: 'Team Management' }, { id: 'registration', label: 'Registration' }, { id: 'webpush', label: 'Web Push' }, ].filter(Boolean); return (
e.target === e.currentTarget && onClose()}>

Settings

{/* Tab buttons */}
{tabs.map(t => ( ))}
{tab === 'team' && } {tab === 'registration' && } {tab === 'webpush' && }
); }