|
| 1 | +import { Tags } from 'lucide-react'; |
| 2 | +import { useEffect, useState } from 'react'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | + |
| 5 | +import { Switch } from '@/components/ui/switch'; |
| 6 | +import { http } from '@/lib/request'; |
| 7 | + |
| 8 | +export default function AutoTag() { |
| 9 | + const { t } = useTranslation(); |
| 10 | + const [isEnabled, setIsEnabled] = useState(true); |
| 11 | + const [loading, setLoading] = useState(true); |
| 12 | + |
| 13 | + useEffect(() => { |
| 14 | + const loadPreference = async () => { |
| 15 | + try { |
| 16 | + const response = await http.get( |
| 17 | + '/user/option/enable_ai_tag_extraction' |
| 18 | + ); |
| 19 | + if (response.value !== undefined) { |
| 20 | + setIsEnabled(response.value === 'true'); |
| 21 | + } |
| 22 | + } catch { |
| 23 | + // If option doesn't exist, default is enabled |
| 24 | + setIsEnabled(true); |
| 25 | + } finally { |
| 26 | + setLoading(false); |
| 27 | + } |
| 28 | + }; |
| 29 | + loadPreference().then(); |
| 30 | + }, []); |
| 31 | + |
| 32 | + const handleToggle = async (checked: boolean) => { |
| 33 | + setIsEnabled(checked); |
| 34 | + try { |
| 35 | + await http.post('/user/option', { |
| 36 | + name: 'enable_ai_tag_extraction', |
| 37 | + value: checked ? 'true' : 'false', |
| 38 | + }); |
| 39 | + } catch { |
| 40 | + setIsEnabled(!checked); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + return ( |
| 45 | + <div className="flex items-center justify-between"> |
| 46 | + <div className="flex flex-col gap-1"> |
| 47 | + <div className="flex items-center gap-2"> |
| 48 | + <Tags className="size-4" /> |
| 49 | + <span>{t('manage.auto_tag_setting')}</span> |
| 50 | + </div> |
| 51 | + <span className="text-xs text-muted-foreground ml-6"> |
| 52 | + {t('manage.auto_tag_description')} |
| 53 | + </span> |
| 54 | + </div> |
| 55 | + <Switch |
| 56 | + checked={isEnabled} |
| 57 | + onCheckedChange={handleToggle} |
| 58 | + disabled={loading} |
| 59 | + /> |
| 60 | + </div> |
| 61 | + ); |
| 62 | +} |
0 commit comments