import React, { forwardRef, useState } from "react"; import { BaseComponentProps } from "../../types"; import { Button } from "../ui/Button"; import { Card, CardContent, CardHeader, CardTitle } from "../ui/Card"; export interface NotificationSetting { id: string; label: string; description?: string; enabled: boolean; type: "email" | "push" | "sms" | "in-app"; } export interface NotificationCategory { id: string; title: string; description?: string; settings: NotificationSetting[]; } export interface NotificationSettingsProps extends BaseComponentProps { categories: NotificationCategory[]; onSave?: (settings: NotificationSetting[]) => void; loading?: boolean; allowGlobalToggle?: boolean; } export const NotificationSettings = forwardRef< HTMLDivElement, NotificationSettingsProps >( ( { className = "", categories, onSave, loading = false, allowGlobalToggle = true, ...props }, ref ) => { const [settings, setSettings] = useState( categories.flatMap((category) => category.settings) ); const [hasChanges, setHasChanges] = useState(false); const handleToggle = (settingId: string) => { setSettings((prev) => { const updated = prev.map((setting) => setting.id === settingId ? { ...setting, enabled: !setting.enabled } : setting ); setHasChanges(true); return updated; }); }; const handleGlobalToggle = ( type: NotificationSetting["type"], enabled: boolean ) => { setSettings((prev) => { const updated = prev.map((setting) => setting.type === type ? { ...setting, enabled } : setting ); setHasChanges(true); return updated; }); }; const handleSave = () => { onSave?.(settings); setHasChanges(false); }; const handleReset = () => { setSettings(categories.flatMap((category) => category.settings)); setHasChanges(false); }; const getTypeIcon = (type: NotificationSetting["type"]) => { switch (type) { case "email": return ( ); case "push": return ( ); case "sms": return ( ); case "in-app": return ( ); } }; const getSettingsByType = (type: NotificationSetting["type"]) => { return settings.filter((setting) => setting.type === type); }; const isTypeEnabled = (type: NotificationSetting["type"]) => { const typeSettings = getSettingsByType(type); return typeSettings.some((setting) => setting.enabled); }; const renderToggleSwitch = ( enabled: boolean, onChange: () => void, disabled = false ) => ( ); const renderGlobalControls = () => { if (!allowGlobalToggle) return null; const types: NotificationSetting["type"][] = [ "email", "push", "sms", "in-app", ]; return ( Quick Controls
{types.map((type) => (
{getTypeIcon(type)}
{type === "in-app" ? "In-App" : type}
{renderToggleSwitch( isTypeEnabled(type), () => handleGlobalToggle(type, !isTypeEnabled(type)), loading )}
))}
); }; return (
{renderGlobalControls()}
{categories.map((category) => ( {category.title} {category.description && (

{category.description}

)}
{category.settings.map((setting) => { const currentSetting = settings.find( (s) => s.id === setting.id ); if (!currentSetting) return null; return (
{getTypeIcon(setting.type)}

{setting.label}

{setting.description && (

{setting.description}

)}
{renderToggleSwitch( currentSetting.enabled, () => handleToggle(setting.id), loading )}
); })}
))}
{hasChanges && (

You have unsaved changes

)}
); } ); NotificationSettings.displayName = "NotificationSettings";