import { useState, useEffect } from 'react'; import { api } from '../utils/api.js'; import { useToast } from '../contexts/ToastContext.jsx'; // ── Helpers ─────────────────────────────────────────────────────────────────── const APP_TYPES = { 'RosterChirp-Chat': { label: 'RosterChirp-Chat', desc: 'Chat only. No Branding, Group Manager or Schedule Manager.' }, 'RosterChirp-Brand': { label: 'RosterChirp-Brand', desc: 'Chat and Branding.' }, 'RosterChirp-Team': { label: 'RosterChirp-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||[])].sort((a, b) => a.name.localeCompare(b.name)))).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('rosterchirp: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 || 'RosterChirp-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('rosterchirp: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('rosterchirp:settings-changed')); onFeaturesChanged && onFeaturesChanged(f); } catch (e) { toast(e.message, 'error'); } }; const typeInfo = APP_TYPES[appType] || APP_TYPES['RosterChirp-Chat']; const siteUrl = window.location.origin; return (
{/* Info box */}

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

RosterChirp {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()} autoComplete="new-password" />
{activeCode && (
Registered — {typeInfo.label}
)}

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

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

Settings

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