|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { Switch } from "../ui/switch"; |
| 5 | +import { Label } from "../ui/label"; |
| 6 | +import { Input } from "../ui/input"; |
| 7 | +import { Button } from "../ui/button"; |
| 8 | +import { toast } from "../ui/use-toast"; |
| 9 | +import { Check, Loader2 } from "lucide-react"; |
| 10 | +import { getUserSettings, updateAutomationSettings } from "@/actions/userSettings.actions"; |
| 11 | +import { useTranslations } from "@/i18n"; |
| 12 | +import type { AutomationSettings as AutomationSettingsType } from "@/models/userSettings.model"; |
| 13 | + |
| 14 | +const defaultAutomation: AutomationSettingsType = { |
| 15 | + performanceWarningEnabled: true, |
| 16 | + performanceWarningThreshold: 10, |
| 17 | +}; |
| 18 | + |
| 19 | +function AutomationSettings() { |
| 20 | + const { t } = useTranslations(); |
| 21 | + const [isLoading, setIsLoading] = useState(true); |
| 22 | + const [settings, setSettings] = useState<AutomationSettingsType>(defaultAutomation); |
| 23 | + const [thresholdInput, setThresholdInput] = useState("10"); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + const fetchSettings = async () => { |
| 27 | + setIsLoading(true); |
| 28 | + try { |
| 29 | + const result = await getUserSettings(); |
| 30 | + if (result.success && (result.data as any)?.settings?.automation) { |
| 31 | + const automation = (result.data as any).settings.automation; |
| 32 | + const merged = { ...defaultAutomation, ...automation }; |
| 33 | + setSettings(merged); |
| 34 | + setThresholdInput(String(merged.performanceWarningThreshold)); |
| 35 | + } |
| 36 | + } catch (error) { |
| 37 | + console.error("Error fetching automation settings:", error); |
| 38 | + } finally { |
| 39 | + setIsLoading(false); |
| 40 | + } |
| 41 | + }; |
| 42 | + fetchSettings(); |
| 43 | + }, []); |
| 44 | + |
| 45 | + const handleToggle = async (update: Partial<AutomationSettingsType>) => { |
| 46 | + const newSettings: AutomationSettingsType = { |
| 47 | + ...settings, |
| 48 | + ...update, |
| 49 | + }; |
| 50 | + setSettings(newSettings); |
| 51 | + |
| 52 | + try { |
| 53 | + const result = await updateAutomationSettings(newSettings); |
| 54 | + if (result.success) { |
| 55 | + toast({ |
| 56 | + variant: "success", |
| 57 | + title: t("settings.saved"), |
| 58 | + }); |
| 59 | + } else { |
| 60 | + toast({ |
| 61 | + variant: "destructive", |
| 62 | + title: t("settings.error"), |
| 63 | + description: t("settings.saveFailed"), |
| 64 | + }); |
| 65 | + setSettings(settings); |
| 66 | + } |
| 67 | + } catch { |
| 68 | + toast({ |
| 69 | + variant: "destructive", |
| 70 | + title: t("settings.error"), |
| 71 | + description: t("settings.saveFailed"), |
| 72 | + }); |
| 73 | + setSettings(settings); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + const handleThresholdSave = () => { |
| 78 | + const value = parseInt(thresholdInput, 10); |
| 79 | + if (isNaN(value) || value < 1) { |
| 80 | + setThresholdInput(String(settings.performanceWarningThreshold)); |
| 81 | + return; |
| 82 | + } |
| 83 | + handleToggle({ performanceWarningThreshold: value }); |
| 84 | + }; |
| 85 | + |
| 86 | + if (isLoading) { |
| 87 | + return ( |
| 88 | + <div className="space-y-4"> |
| 89 | + <div> |
| 90 | + <h3 className="text-lg font-medium">{t("settings.automationSettings")}</h3> |
| 91 | + <p className="text-sm text-muted-foreground"> |
| 92 | + {t("settings.automationSettingsDesc")} |
| 93 | + </p> |
| 94 | + </div> |
| 95 | + <div className="flex items-center gap-2"> |
| 96 | + <Loader2 className="h-4 w-4 animate-spin" /> |
| 97 | + <span>{t("settings.loadingSettings")}</span> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + return ( |
| 104 | + <div className="space-y-4"> |
| 105 | + <div> |
| 106 | + <h3 className="text-lg font-medium">{t("settings.automationSettings")}</h3> |
| 107 | + <p className="text-sm text-muted-foreground"> |
| 108 | + {t("settings.automationSettingsDesc")} |
| 109 | + </p> |
| 110 | + </div> |
| 111 | + |
| 112 | + <div className="space-y-4"> |
| 113 | + {/* Performance warning toggle */} |
| 114 | + <div className="flex items-center justify-between rounded-lg border p-4"> |
| 115 | + <div className="space-y-0.5"> |
| 116 | + <Label htmlFor="performance-warning"> |
| 117 | + {t("settings.automationPerformanceWarning")} |
| 118 | + </Label> |
| 119 | + <p className="text-sm text-muted-foreground"> |
| 120 | + {t("settings.automationPerformanceWarningDesc")} |
| 121 | + </p> |
| 122 | + </div> |
| 123 | + <Switch |
| 124 | + id="performance-warning" |
| 125 | + checked={settings.performanceWarningEnabled} |
| 126 | + onCheckedChange={(checked) => |
| 127 | + handleToggle({ performanceWarningEnabled: checked }) |
| 128 | + } |
| 129 | + aria-label={t("settings.automationPerformanceWarning")} |
| 130 | + /> |
| 131 | + </div> |
| 132 | + |
| 133 | + {/* Performance warning threshold */} |
| 134 | + <div className="rounded-lg border p-4 space-y-2"> |
| 135 | + <div className="space-y-0.5"> |
| 136 | + <Label htmlFor="performance-threshold"> |
| 137 | + {t("settings.automationPerformanceThreshold")} |
| 138 | + </Label> |
| 139 | + <p className="text-sm text-muted-foreground"> |
| 140 | + {t("settings.automationPerformanceThresholdDesc")} |
| 141 | + </p> |
| 142 | + </div> |
| 143 | + <div className="flex gap-2"> |
| 144 | + <Input |
| 145 | + id="performance-threshold" |
| 146 | + type="number" |
| 147 | + min={1} |
| 148 | + className="w-24" |
| 149 | + value={thresholdInput} |
| 150 | + disabled={!settings.performanceWarningEnabled} |
| 151 | + onChange={(e) => setThresholdInput(e.target.value)} |
| 152 | + onKeyDown={(e) => { |
| 153 | + if (e.key === "Enter") { |
| 154 | + e.preventDefault(); |
| 155 | + handleThresholdSave(); |
| 156 | + } |
| 157 | + }} |
| 158 | + /> |
| 159 | + <Button |
| 160 | + variant="outline" |
| 161 | + size="icon" |
| 162 | + className="shrink-0" |
| 163 | + disabled={!settings.performanceWarningEnabled} |
| 164 | + onClick={handleThresholdSave} |
| 165 | + aria-label={t("common.save")} |
| 166 | + > |
| 167 | + <Check className="h-4 w-4" /> |
| 168 | + </Button> |
| 169 | + </div> |
| 170 | + </div> |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + ); |
| 174 | +} |
| 175 | + |
| 176 | +export default AutomationSettings; |
0 commit comments