|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useState, useRef } from 'react'; |
| 4 | +import { useTranslations } from 'next-intl'; |
| 5 | +import { Zap, Hash, Timer, TimerOff, HelpCircle } from 'lucide-react'; |
| 6 | +import { Input } from '@/components/ui/input'; |
| 7 | +import { useSettingList, useSetSetting, SettingKey } from '@/api/endpoints/setting'; |
| 8 | +import { toast } from '@/components/common/Toast'; |
| 9 | +import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/animate-ui/components/animate/tooltip'; |
| 10 | + |
| 11 | +export function SettingCircuitBreaker() { |
| 12 | + const t = useTranslations('setting'); |
| 13 | + const { data: settings } = useSettingList(); |
| 14 | + const setSetting = useSetSetting(); |
| 15 | + |
| 16 | + const [threshold, setThreshold] = useState(''); |
| 17 | + const [cooldown, setCooldown] = useState(''); |
| 18 | + const [maxCooldown, setMaxCooldown] = useState(''); |
| 19 | + |
| 20 | + const initialThreshold = useRef(''); |
| 21 | + const initialCooldown = useRef(''); |
| 22 | + const initialMaxCooldown = useRef(''); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + if (settings) { |
| 26 | + const th = settings.find(s => s.key === SettingKey.CircuitBreakerThreshold); |
| 27 | + const cd = settings.find(s => s.key === SettingKey.CircuitBreakerCooldown); |
| 28 | + const mcd = settings.find(s => s.key === SettingKey.CircuitBreakerMaxCooldown); |
| 29 | + if (th) { |
| 30 | + queueMicrotask(() => setThreshold(th.value)); |
| 31 | + initialThreshold.current = th.value; |
| 32 | + } |
| 33 | + if (cd) { |
| 34 | + queueMicrotask(() => setCooldown(cd.value)); |
| 35 | + initialCooldown.current = cd.value; |
| 36 | + } |
| 37 | + if (mcd) { |
| 38 | + queueMicrotask(() => setMaxCooldown(mcd.value)); |
| 39 | + initialMaxCooldown.current = mcd.value; |
| 40 | + } |
| 41 | + } |
| 42 | + }, [settings]); |
| 43 | + |
| 44 | + const handleSave = (key: string, value: string, initialValue: string) => { |
| 45 | + if (value === initialValue) return; |
| 46 | + |
| 47 | + setSetting.mutate({ key, value }, { |
| 48 | + onSuccess: () => { |
| 49 | + toast.success(t('saved')); |
| 50 | + if (key === SettingKey.CircuitBreakerThreshold) { |
| 51 | + initialThreshold.current = value; |
| 52 | + } else if (key === SettingKey.CircuitBreakerCooldown) { |
| 53 | + initialCooldown.current = value; |
| 54 | + } else if (key === SettingKey.CircuitBreakerMaxCooldown) { |
| 55 | + initialMaxCooldown.current = value; |
| 56 | + } |
| 57 | + } |
| 58 | + }); |
| 59 | + }; |
| 60 | + |
| 61 | + return ( |
| 62 | + <div className="rounded-3xl border border-border bg-card p-6 custom-shadow space-y-5"> |
| 63 | + <h2 className="text-lg font-bold text-card-foreground flex items-center gap-2"> |
| 64 | + <Zap className="h-5 w-5" /> |
| 65 | + {t('circuitBreaker.title')} |
| 66 | + <TooltipProvider> |
| 67 | + <Tooltip> |
| 68 | + <TooltipTrigger asChild> |
| 69 | + <HelpCircle className="size-4 text-muted-foreground cursor-help" /> |
| 70 | + </TooltipTrigger> |
| 71 | + <TooltipContent> |
| 72 | + {t('circuitBreaker.hint')} |
| 73 | + </TooltipContent> |
| 74 | + </Tooltip> |
| 75 | + </TooltipProvider> |
| 76 | + </h2> |
| 77 | + |
| 78 | + {/* 熔断触发阈值 */} |
| 79 | + <div className="flex items-center justify-between gap-4"> |
| 80 | + <div className="flex items-center gap-3"> |
| 81 | + <Hash className="h-5 w-5 text-muted-foreground" /> |
| 82 | + <span className="text-sm font-medium">{t('circuitBreaker.threshold.label')}</span> |
| 83 | + </div> |
| 84 | + <Input |
| 85 | + type="number" |
| 86 | + value={threshold} |
| 87 | + onChange={(e) => setThreshold(e.target.value)} |
| 88 | + onBlur={() => handleSave(SettingKey.CircuitBreakerThreshold, threshold, initialThreshold.current)} |
| 89 | + placeholder={t('circuitBreaker.threshold.placeholder')} |
| 90 | + className="w-48 rounded-xl" |
| 91 | + /> |
| 92 | + </div> |
| 93 | + |
| 94 | + {/* 基础冷却时间 */} |
| 95 | + <div className="flex items-center justify-between gap-4"> |
| 96 | + <div className="flex items-center gap-3"> |
| 97 | + <Timer className="h-5 w-5 text-muted-foreground" /> |
| 98 | + <span className="text-sm font-medium">{t('circuitBreaker.cooldown.label')}</span> |
| 99 | + </div> |
| 100 | + <Input |
| 101 | + type="number" |
| 102 | + value={cooldown} |
| 103 | + onChange={(e) => setCooldown(e.target.value)} |
| 104 | + onBlur={() => handleSave(SettingKey.CircuitBreakerCooldown, cooldown, initialCooldown.current)} |
| 105 | + placeholder={t('circuitBreaker.cooldown.placeholder')} |
| 106 | + className="w-48 rounded-xl" |
| 107 | + /> |
| 108 | + </div> |
| 109 | + |
| 110 | + {/* 最大冷却时间 */} |
| 111 | + <div className="flex items-center justify-between gap-4"> |
| 112 | + <div className="flex items-center gap-3"> |
| 113 | + <TimerOff className="h-5 w-5 text-muted-foreground" /> |
| 114 | + <span className="text-sm font-medium">{t('circuitBreaker.maxCooldown.label')}</span> |
| 115 | + </div> |
| 116 | + <Input |
| 117 | + type="number" |
| 118 | + value={maxCooldown} |
| 119 | + onChange={(e) => setMaxCooldown(e.target.value)} |
| 120 | + onBlur={() => handleSave(SettingKey.CircuitBreakerMaxCooldown, maxCooldown, initialMaxCooldown.current)} |
| 121 | + placeholder={t('circuitBreaker.maxCooldown.placeholder')} |
| 122 | + className="w-48 rounded-xl" |
| 123 | + /> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + ); |
| 127 | +} |
0 commit comments