'use client'; import { getAuthMethodConfig, getSmsPlatform, testSmsSend, updateAuthMethodConfig, } from '@/services/admin/authMethod'; import { zodResolver } from '@hookform/resolvers/zod'; import { useQuery } from '@tanstack/react-query'; import { Button } from '@workspace/ui/components/button'; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@workspace/ui/components/form'; import { ScrollArea } from '@workspace/ui/components/scroll-area'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@workspace/ui/components/select'; import { Sheet, SheetContent, SheetFooter, SheetHeader, SheetTitle, SheetTrigger, } from '@workspace/ui/components/sheet'; import { Switch } from '@workspace/ui/components/switch'; import { Textarea } from '@workspace/ui/components/textarea'; import { AreaCodeSelect } from '@workspace/ui/custom-components/area-code-select'; import { EnhancedInput } from '@workspace/ui/custom-components/enhanced-input'; import { Icon } from '@workspace/ui/custom-components/icon'; import TagInput from '@workspace/ui/custom-components/tag-input'; import { useTranslations } from 'next-intl'; import Link from 'next/link'; import { useEffect, useState } from 'react'; import { useForm } from 'react-hook-form'; import { toast } from 'sonner'; import { z } from 'zod'; const phoneSettingsSchema = z.object({ id: z.number(), method: z.string().default('mobile'), enabled: z.boolean().default(false), config: z .object({ enable_whitelist: z.boolean().optional(), whitelist: z.array(z.string()).optional(), platform: z.string().optional(), platform_config: z .object({ access: z.string().optional(), endpoint: z.string().optional(), secret: z.string().optional(), template_code: z.string().optional(), sign_name: z.string().optional(), phone_number: z.string().optional(), template: z.string().optional(), }) .optional(), }) .optional(), }); type PhoneSettingsFormData = z.infer; export default function PhoneSettingsForm() { const t = useTranslations('auth-control'); const [open, setOpen] = useState(false); const [loading, setLoading] = useState(false); const [testParams, setTestParams] = useState({ telephone: '', area_code: '1', }); const { data, refetch, isFetching } = useQuery({ queryKey: ['getAuthMethodConfig', 'mobile'], queryFn: async () => { const { data } = await getAuthMethodConfig({ method: 'mobile', }); return data.data; }, enabled: open, }); const { data: platforms } = useQuery({ queryKey: ['getSmsPlatform'], queryFn: async () => { const { data } = await getSmsPlatform(); return data.data?.list; }, enabled: open, }); const form = useForm({ resolver: zodResolver(phoneSettingsSchema), defaultValues: { id: 0, method: 'mobile', enabled: false, config: { enable_whitelist: false, whitelist: [], platform: '', platform_config: { access: '', endpoint: '', secret: '', template_code: 'code', sign_name: '', phone_number: '', template: '', }, }, }, }); const selectedPlatform = platforms?.find( (platform) => platform.platform === form.watch('config.platform'), ); const { platform_url, platform_field_description: platformConfig } = selectedPlatform ?? {}; useEffect(() => { if (data) { form.reset(data); } }, [data, form]); async function onSubmit(values: PhoneSettingsFormData) { setLoading(true); try { await updateAuthMethodConfig(values as API.UpdateAuthMethodConfigRequest); toast.success(t('common.saveSuccess')); refetch(); setOpen(false); } catch (error) { toast.error(t('common.saveFailed')); } finally { setLoading(false); } } return (

{t('phone.title')}

{t('phone.description')}

{t('phone.title')}
( {t('phone.enable')} {t('phone.enableTip')} )} /> ( {t('phone.whitelistValidation')} {t('phone.whitelistValidationTip')} )} /> ( {t('phone.whitelistAreaCode')} {t('phone.whitelistAreaCodeTip')} )} /> ( {t('phone.platform')}
{platform_url && ( )}
{t('phone.platformTip')}
)} /> ( {t('phone.accessLabel')} {t('phone.platformConfigTip', { key: platformConfig?.access })} )} /> {platformConfig?.endpoint && ( ( {t('phone.endpointLabel')} {t('phone.platformConfigTip', { key: platformConfig?.endpoint })} )} /> )} ( {t('phone.secretLabel')} {t('phone.platformConfigTip', { key: platformConfig?.secret })} )} /> {platformConfig?.template_code && ( ( {t('phone.templateCodeLabel')} {t('phone.platformConfigTip', { key: platformConfig?.template_code })} )} /> )} {platformConfig?.sign_name && ( ( {t('phone.signNameLabel')} {t('phone.platformConfigTip', { key: platformConfig?.sign_name })} )} /> )} {platformConfig?.phone_number && ( ( {t('phone.phoneNumberLabel')} {t('phone.platformConfigTip', { key: platformConfig?.phone_number })} )} /> )} {platformConfig?.code_variable && ( ( {t('phone.template')}