mirror of
https://github.com/perfect-panel/ppanel-web.git
synced 2026-04-19 10:09:55 -04:00
♻️ refactor(sbscribe): Rename and reorganize components for better structure and clarity
This commit is contained in:
parent
d6fbc38795
commit
5e5e4edd2e
189
apps/admin/app/dashboard/subscribe/app/config.tsx
Normal file
189
apps/admin/app/dashboard/subscribe/app/config.tsx
Normal file
@ -0,0 +1,189 @@
|
||||
'use client';
|
||||
|
||||
import { getAppConfig, updateAppConfig } from '@/services/admin/app';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { Icon } from '@iconify/react';
|
||||
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 {
|
||||
Sheet,
|
||||
SheetContent,
|
||||
SheetFooter,
|
||||
SheetHeader,
|
||||
SheetTitle,
|
||||
SheetTrigger,
|
||||
} from '@workspace/ui/components/sheet';
|
||||
import { Textarea } from '@workspace/ui/components/textarea';
|
||||
import { EnhancedInput } from '@workspace/ui/custom-components/enhanced-input';
|
||||
import { UploadImage } from '@workspace/ui/custom-components/upload-image';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { toast } from 'sonner';
|
||||
import { z } from 'zod';
|
||||
|
||||
const formSchema = z.object({
|
||||
startup_picture: z.string(),
|
||||
startup_picture_skip_time: z.number(),
|
||||
domains: z.array(z.string()),
|
||||
});
|
||||
|
||||
type FormSchema = z.infer<typeof formSchema>;
|
||||
|
||||
export default function ConfigForm() {
|
||||
const t = useTranslations('subscribe.app');
|
||||
const [open, setOpen] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const form = useForm<FormSchema>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: {
|
||||
startup_picture: '',
|
||||
startup_picture_skip_time: 0,
|
||||
domains: [],
|
||||
},
|
||||
});
|
||||
|
||||
const { data, refetch } = useQuery({
|
||||
queryKey: ['getAppConfig'],
|
||||
queryFn: async () => {
|
||||
const { data } = await getAppConfig();
|
||||
return data.data;
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
form.reset({
|
||||
...data,
|
||||
domains: data.domains || [],
|
||||
});
|
||||
}
|
||||
}, [data, form]);
|
||||
|
||||
async function onSubmit(values: FormSchema) {
|
||||
setLoading(true);
|
||||
try {
|
||||
await updateAppConfig(values as API.AppConfig);
|
||||
toast.success(t('updateSuccess'));
|
||||
refetch();
|
||||
setOpen(false);
|
||||
} catch (error) {
|
||||
/* empty */
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Sheet open={open} onOpenChange={setOpen}>
|
||||
<SheetTrigger asChild>
|
||||
<Button variant='outline'>
|
||||
<Icon icon='mdi:cog' className='mr-2' />
|
||||
{t('config')}
|
||||
</Button>
|
||||
</SheetTrigger>
|
||||
<SheetContent className='w-[520px] max-w-full md:max-w-screen-md'>
|
||||
<SheetHeader>
|
||||
<SheetTitle>{t('configApp')}</SheetTitle>
|
||||
</SheetHeader>
|
||||
<ScrollArea className='h-[calc(100dvh-48px-36px-36px)]'>
|
||||
<Form {...form}>
|
||||
<form className='space-y-4 py-4'>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name='startup_picture'
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t('startupPicture')}</FormLabel>
|
||||
<FormDescription>{t('startupPictureDescription')}</FormDescription>
|
||||
<FormControl>
|
||||
<EnhancedInput
|
||||
{...field}
|
||||
suffix={
|
||||
<UploadImage
|
||||
className='bg-muted h-9 rounded-none border-none px-2'
|
||||
onChange={(value) => form.setValue('startup_picture', value as string)}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name='startup_picture_skip_time'
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t('startupPictureSkip')}</FormLabel>
|
||||
<FormDescription>{t('startupPictureSkipDescription')}</FormDescription>
|
||||
<FormControl>
|
||||
<EnhancedInput
|
||||
{...field}
|
||||
type='number'
|
||||
min={0}
|
||||
suffix='S'
|
||||
value={field.value}
|
||||
onValueChange={(value) =>
|
||||
form.setValue('startup_picture_skip_time', Number(value))
|
||||
}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name='domains'
|
||||
render={({ field }) => (
|
||||
<FormItem className='px-1'>
|
||||
<FormLabel>{t('backupDomains')}</FormLabel>
|
||||
<FormDescription>{t('backupDomainsDescription')}</FormDescription>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
className='h-52'
|
||||
placeholder='example.com'
|
||||
value={field.value.join('\n')}
|
||||
onChange={(e) =>
|
||||
form.setValue(
|
||||
'domains',
|
||||
e.target.value.split('\n').filter((line) => line.trim()),
|
||||
)
|
||||
}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</form>
|
||||
</Form>
|
||||
</ScrollArea>
|
||||
<SheetFooter className='flex-row justify-end gap-2 pt-3'>
|
||||
<Button variant='outline' onClick={() => setOpen(false)}>
|
||||
{t('cancel')}
|
||||
</Button>
|
||||
<Button onClick={form.handleSubmit(onSubmit)} disabled={loading}>
|
||||
{loading && <Icon icon='mdi:loading' className='mr-2 animate-spin' />}
|
||||
{t('confirm')}
|
||||
</Button>
|
||||
</SheetFooter>
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
);
|
||||
}
|
||||
277
apps/admin/app/dashboard/subscribe/app/form.tsx
Normal file
277
apps/admin/app/dashboard/subscribe/app/form.tsx
Normal file
@ -0,0 +1,277 @@
|
||||
'use client';
|
||||
|
||||
import { getSubscribeType } from '@/services/admin/system';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { Icon } from '@iconify/react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { Button } from '@workspace/ui/components/button';
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
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 { Tabs, TabsContent, TabsList, TabsTrigger } from '@workspace/ui/components/tabs';
|
||||
import { ArrayInput } from '@workspace/ui/custom-components/dynamic-Inputs';
|
||||
import { EnhancedInput } from '@workspace/ui/custom-components/enhanced-input';
|
||||
import { UploadImage } from '@workspace/ui/custom-components/upload-image';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { assign, shake } from 'radash';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { z } from 'zod';
|
||||
|
||||
const platforms = ['windows', 'macos', 'linux', 'android', 'ios', 'harmony'];
|
||||
|
||||
const defaultValues = {
|
||||
subscription_protocol: 'Clash',
|
||||
name: '',
|
||||
icon: '',
|
||||
url: '',
|
||||
};
|
||||
|
||||
const versionSchema = z.object({
|
||||
url: z.string(),
|
||||
version: z.string(),
|
||||
description: z.string().optional(),
|
||||
is_default: z.boolean().optional(),
|
||||
});
|
||||
|
||||
const formSchema = z.object({
|
||||
icon: z.string(),
|
||||
name: z.string(),
|
||||
subscription_protocol: z.string(),
|
||||
platform: z.object({
|
||||
windows: z.array(versionSchema).optional(),
|
||||
macos: z.array(versionSchema).optional(),
|
||||
linux: z.array(versionSchema).optional(),
|
||||
android: z.array(versionSchema).optional(),
|
||||
ios: z.array(versionSchema).optional(),
|
||||
harmony: z.array(versionSchema).optional(),
|
||||
}),
|
||||
});
|
||||
|
||||
interface FormProps<T> {
|
||||
trigger: React.ReactNode | string;
|
||||
title: string;
|
||||
initialValues?: Partial<T>;
|
||||
onSubmit: (values: T) => Promise<boolean>;
|
||||
loading?: boolean;
|
||||
}
|
||||
|
||||
export default function SubscribeAppForm<
|
||||
T extends API.CreateApplicationRequest | API.UpdateApplicationRequest,
|
||||
>({ trigger, title, loading, initialValues, onSubmit }: FormProps<T>) {
|
||||
const t = useTranslations('subscribe.app');
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
type FormSchema = z.infer<typeof formSchema>;
|
||||
|
||||
const form = useForm<FormSchema>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: assign(
|
||||
defaultValues,
|
||||
shake(initialValues, (value) => value === null),
|
||||
),
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
form.reset(
|
||||
assign(
|
||||
defaultValues,
|
||||
shake(initialValues, (value) => value === null),
|
||||
),
|
||||
);
|
||||
}, [form, initialValues]);
|
||||
|
||||
const { data: subscribe_types } = useQuery<string[]>({
|
||||
queryKey: ['getSubscribeType'],
|
||||
queryFn: async () => {
|
||||
const { data } = await getSubscribeType();
|
||||
return data.data?.subscribe_types || [];
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<Sheet open={open} onOpenChange={setOpen}>
|
||||
<SheetTrigger asChild>
|
||||
{typeof trigger === 'string' ? <Button>{trigger}</Button> : trigger}
|
||||
</SheetTrigger>
|
||||
<SheetContent className='w-[520px] max-w-full md:max-w-screen-md'>
|
||||
<SheetHeader>
|
||||
<SheetTitle>{title}</SheetTitle>
|
||||
</SheetHeader>
|
||||
<ScrollArea className='h-[calc(100dvh-48px-36px-36px)]'>
|
||||
<Form {...form}>
|
||||
<form className='space-y-4 py-4'>
|
||||
<div className='grid grid-cols-2 gap-4'>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name='icon'
|
||||
render={({ field }) => (
|
||||
<FormItem className='col-span-2'>
|
||||
<FormLabel>{t('appIcon')}</FormLabel>
|
||||
<FormControl>
|
||||
<EnhancedInput
|
||||
required
|
||||
suffix={
|
||||
<UploadImage
|
||||
className='bg-muted h-9 rounded-none border-none px-2'
|
||||
onChange={(value) => {
|
||||
form.setValue(field.name, value as string);
|
||||
}}
|
||||
/>
|
||||
}
|
||||
value={field.value}
|
||||
onValueChange={(value) => {
|
||||
form.setValue(field.name, value as string);
|
||||
}}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name='name'
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t('appName')}</FormLabel>
|
||||
<FormControl>
|
||||
<EnhancedInput
|
||||
required
|
||||
type='text'
|
||||
value={field.value}
|
||||
onValueChange={(value) => {
|
||||
form.setValue(field.name, value as string);
|
||||
}}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name='subscription_protocol'
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t('subscriptionProtocol')}</FormLabel>
|
||||
<FormControl>
|
||||
<Select value={field.value} onValueChange={field.onChange}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder={t('subscriptionProtocol')} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{subscribe_types?.map((type) => (
|
||||
<SelectItem key={type} value={type}>
|
||||
{type}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
<FormItem>
|
||||
<FormLabel>{t('platform')}</FormLabel>
|
||||
<Tabs defaultValue={platforms[0]}>
|
||||
<TabsList>
|
||||
{platforms.map((platform) => (
|
||||
<TabsTrigger key={platform} value={platform} className='uppercase'>
|
||||
{platform}
|
||||
</TabsTrigger>
|
||||
))}
|
||||
</TabsList>
|
||||
{platforms.map((platform) => (
|
||||
<TabsContent key={platform} value={platform}>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name={`platform.${platform as keyof FormSchema['platform']}`}
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormControl>
|
||||
<ArrayInput
|
||||
isReverse
|
||||
className='grid grid-cols-3 gap-4'
|
||||
fields={[
|
||||
{
|
||||
name: 'version',
|
||||
type: 'text',
|
||||
placeholder: t('version'),
|
||||
},
|
||||
{
|
||||
name: 'description',
|
||||
type: 'text',
|
||||
placeholder: t('description'),
|
||||
},
|
||||
{
|
||||
name: 'is_default',
|
||||
type: 'boolean',
|
||||
placeholder: t('defaultVersion'),
|
||||
},
|
||||
{
|
||||
name: 'url',
|
||||
type: 'text',
|
||||
placeholder: t('downloadLink'),
|
||||
className: 'col-span-3',
|
||||
},
|
||||
]}
|
||||
value={field.value}
|
||||
onChange={(value) => {
|
||||
form.setValue(field.name, value);
|
||||
}}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</TabsContent>
|
||||
))}
|
||||
</Tabs>
|
||||
</FormItem>
|
||||
</form>
|
||||
</Form>
|
||||
</ScrollArea>
|
||||
<SheetFooter className='flex-row justify-end gap-2 pt-3'>
|
||||
<Button variant='outline' onClick={() => setOpen(false)}>
|
||||
{t('cancel')}
|
||||
</Button>
|
||||
<Button
|
||||
onClick={form.handleSubmit(async (values) => {
|
||||
const success = await onSubmit(values as unknown as T);
|
||||
if (success) setOpen(false);
|
||||
})}
|
||||
disabled={loading}
|
||||
>
|
||||
{loading && <Icon icon='mdi:loading' className='mr-2 animate-spin' />}
|
||||
{t('confirm')}
|
||||
</Button>
|
||||
</SheetFooter>
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
);
|
||||
}
|
||||
143
apps/admin/app/dashboard/subscribe/app/table.tsx
Normal file
143
apps/admin/app/dashboard/subscribe/app/table.tsx
Normal file
@ -0,0 +1,143 @@
|
||||
'use client';
|
||||
|
||||
import { ProTable, ProTableActions } from '@/components/pro-table';
|
||||
import {
|
||||
createApplication,
|
||||
deleteApplication,
|
||||
getApplication,
|
||||
updateApplication,
|
||||
} from '@/services/admin/system';
|
||||
import { Button } from '@workspace/ui/components/button';
|
||||
import { ConfirmButton } from '@workspace/ui/custom-components/confirm-button';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import Image from 'next/legacy/image';
|
||||
import { useRef, useState } from 'react';
|
||||
import { toast } from 'sonner';
|
||||
import ConfigForm from './config';
|
||||
import SubscribeAppForm from './form';
|
||||
|
||||
export default function SubscribeApp() {
|
||||
const t = useTranslations('subscribe.app');
|
||||
const [loading, setLoading] = useState(false);
|
||||
const ref = useRef<ProTableActions>(null);
|
||||
|
||||
return (
|
||||
<ProTable<API.ApplicationResponseInfo, Record<string, unknown>>
|
||||
action={ref}
|
||||
header={{
|
||||
title: t('appList'),
|
||||
toolbar: (
|
||||
<div className='flex items-center gap-2'>
|
||||
<ConfigForm />
|
||||
<SubscribeAppForm<API.CreateApplicationRequest>
|
||||
trigger={t('create')}
|
||||
title={t('createApp')}
|
||||
loading={loading}
|
||||
onSubmit={async (values) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
await createApplication(values);
|
||||
toast.success(t('createSuccess'));
|
||||
ref.current?.refresh();
|
||||
setLoading(false);
|
||||
return true;
|
||||
} catch (error) {
|
||||
setLoading(false);
|
||||
return false;
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
),
|
||||
}}
|
||||
request={async (_pagination, filters) => {
|
||||
const { data } = await getApplication();
|
||||
return {
|
||||
list: data.data?.applications || [],
|
||||
total: 0,
|
||||
};
|
||||
}}
|
||||
columns={[
|
||||
{
|
||||
accessorKey: 'icon',
|
||||
header: t('appIcon'),
|
||||
cell: ({ row }) => (
|
||||
<Image
|
||||
src={row.getValue('icon')}
|
||||
alt={row.getValue('name')}
|
||||
className='h-8 w-8 rounded-md'
|
||||
width={32}
|
||||
height={32}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{
|
||||
accessorKey: 'name',
|
||||
header: t('appName'),
|
||||
},
|
||||
{
|
||||
accessorKey: 'subscription_protocol',
|
||||
header: t('subscriptionProtocol'),
|
||||
cell: ({ row }) => row.getValue('subscription_protocol'),
|
||||
},
|
||||
]}
|
||||
actions={{
|
||||
render: (row) => [
|
||||
<SubscribeAppForm<API.UpdateApplicationRequest>
|
||||
key='edit'
|
||||
trigger={<Button>{t('edit')}</Button>}
|
||||
title={t('editApp')}
|
||||
loading={loading}
|
||||
initialValues={{
|
||||
...row,
|
||||
}}
|
||||
onSubmit={async (values) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
await updateApplication({
|
||||
...values,
|
||||
id: row.id,
|
||||
});
|
||||
toast.success(t('updateSuccess'));
|
||||
ref.current?.refresh();
|
||||
setLoading(false);
|
||||
return true;
|
||||
} catch (error) {
|
||||
setLoading(false);
|
||||
return false;
|
||||
}
|
||||
}}
|
||||
/>,
|
||||
<ConfirmButton
|
||||
key='delete'
|
||||
trigger={<Button variant='destructive'>{t('delete')}</Button>}
|
||||
title={t('confirmDelete')}
|
||||
description={t('deleteWarning')}
|
||||
onConfirm={async () => {
|
||||
await deleteApplication({ id: row.id! });
|
||||
toast.success(t('deleteSuccess'));
|
||||
ref.current?.refresh();
|
||||
}}
|
||||
cancelText={t('cancel')}
|
||||
confirmText={t('confirm')}
|
||||
/>,
|
||||
],
|
||||
batchRender: (rows) => [
|
||||
<ConfirmButton
|
||||
key='delete'
|
||||
trigger={<Button variant='destructive'>{t('batchDelete')}</Button>}
|
||||
title={t('confirmDelete')}
|
||||
description={t('deleteWarning')}
|
||||
onConfirm={async () => {
|
||||
await Promise.all(rows.map((row) => deleteApplication({ id: row.id! })));
|
||||
toast.success(t('deleteSuccess'));
|
||||
ref.current?.reset();
|
||||
}}
|
||||
cancelText={t('cancel')}
|
||||
confirmText={t('confirm')}
|
||||
/>,
|
||||
],
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@ -1,5 +1,4 @@
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { Icon } from '@iconify/react';
|
||||
import { Button } from '@workspace/ui/components/button';
|
||||
import {
|
||||
Form,
|
||||
@ -19,6 +18,7 @@ import {
|
||||
SheetTrigger,
|
||||
} from '@workspace/ui/components/sheet';
|
||||
import { EnhancedInput } from '@workspace/ui/custom-components/enhanced-input';
|
||||
import { Icon } from '@workspace/ui/custom-components/icon';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
@ -14,7 +14,7 @@ import { formatDate } from '@workspace/ui/utils';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { useRef, useState } from 'react';
|
||||
import { toast } from 'sonner';
|
||||
import GroupForm from './group-form';
|
||||
import GroupForm from './form';
|
||||
|
||||
const GroupTable = () => {
|
||||
const t = useTranslations('subscribe');
|
||||
@ -2,8 +2,8 @@ import { getTranslations } from 'next-intl/server';
|
||||
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@workspace/ui/components/tabs';
|
||||
|
||||
import GroupTable from './group-table';
|
||||
import SubscribeApp from './subscribe-app';
|
||||
import SubscribeApp from './app/table';
|
||||
import GroupTable from './group/table';
|
||||
import SubscribeConfig from './subscribe-config';
|
||||
import SubscribeTable from './subscribe-table';
|
||||
|
||||
|
||||
@ -1,386 +0,0 @@
|
||||
'use client';
|
||||
|
||||
import { ProTable, ProTableActions } from '@/components/pro-table';
|
||||
import {
|
||||
createApplication,
|
||||
deleteApplication,
|
||||
getApplication,
|
||||
getSubscribeType,
|
||||
updateApplication,
|
||||
} from '@/services/admin/system';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { Icon } from '@iconify/react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { Button } from '@workspace/ui/components/button';
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
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 { ConfirmButton } from '@workspace/ui/custom-components/confirm-button';
|
||||
import { EnhancedInput } from '@workspace/ui/custom-components/enhanced-input';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import Image from 'next/legacy/image';
|
||||
import { assign, shake } from 'radash';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { toast } from 'sonner';
|
||||
import { z } from 'zod';
|
||||
|
||||
const defaultValues = {
|
||||
platform: 'windows',
|
||||
subscribe_type: 'Clash',
|
||||
name: '',
|
||||
icon: '',
|
||||
url: '',
|
||||
};
|
||||
|
||||
interface FormProps<T> {
|
||||
trigger: React.ReactNode | string;
|
||||
title: string;
|
||||
initialValues?: Partial<T>;
|
||||
onSubmit: (values: T) => Promise<boolean>;
|
||||
loading?: boolean;
|
||||
}
|
||||
|
||||
function SubscribeAppForm<T extends API.CreateApplicationRequest | API.UpdateApplicationRequest>({
|
||||
trigger,
|
||||
title,
|
||||
loading,
|
||||
initialValues,
|
||||
onSubmit,
|
||||
}: FormProps<T>) {
|
||||
const t = useTranslations('subscribe.app');
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
const formSchema = z.object({
|
||||
platform: z.enum(['windows', 'macos', 'linux', 'android', 'ios']),
|
||||
name: z.string(),
|
||||
subscribe_type: z.string(),
|
||||
icon: z.string(),
|
||||
url: z.string(),
|
||||
});
|
||||
|
||||
type FormSchema = z.infer<typeof formSchema>;
|
||||
|
||||
const form = useForm<FormSchema>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: assign(
|
||||
defaultValues,
|
||||
shake(initialValues, (value) => value === null),
|
||||
),
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
form.reset(
|
||||
assign(
|
||||
defaultValues,
|
||||
shake(initialValues, (value) => value === null),
|
||||
),
|
||||
);
|
||||
}, [form, initialValues]);
|
||||
|
||||
const { data: subscribe_types } = useQuery<string[]>({
|
||||
queryKey: ['getSubscribeType'],
|
||||
queryFn: async () => {
|
||||
const { data } = await getSubscribeType();
|
||||
return data.data?.subscribe_types || [];
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<Sheet open={open} onOpenChange={setOpen}>
|
||||
<SheetTrigger asChild>
|
||||
{typeof trigger === 'string' ? <Button>{trigger}</Button> : trigger}
|
||||
</SheetTrigger>
|
||||
<SheetContent className='w-[600px] max-w-full'>
|
||||
<SheetHeader>
|
||||
<SheetTitle>{title}</SheetTitle>
|
||||
</SheetHeader>
|
||||
<ScrollArea className='h-[calc(100dvh-48px-36px-36px)]'>
|
||||
<Form {...form}>
|
||||
<form className='space-y-4 py-4'>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name='platform'
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t('platform')}</FormLabel>
|
||||
<FormControl>
|
||||
<Select value={field.value} onValueChange={field.onChange}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder={t('platform')} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{['windows', 'macos', 'linux', 'android', 'ios'].map((platform) => (
|
||||
<SelectItem key={platform} value={platform}>
|
||||
{platform.toUpperCase()}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name='subscribe_type'
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t('subscriptionProtocol')}</FormLabel>
|
||||
<FormControl>
|
||||
<Select value={field.value} onValueChange={field.onChange}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder={t('subscriptionProtocol')} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{subscribe_types?.map((type) => (
|
||||
<SelectItem key={type} value={type}>
|
||||
{type}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name='name'
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t('appName')}</FormLabel>
|
||||
<FormControl>
|
||||
<EnhancedInput {...field} required />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name='icon'
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t('appIcon')}</FormLabel>
|
||||
<FormControl>
|
||||
<EnhancedInput {...field} required />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name='url'
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t('appDownloadURL')}</FormLabel>
|
||||
<FormControl>
|
||||
<EnhancedInput {...field} required />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</form>
|
||||
</Form>
|
||||
</ScrollArea>
|
||||
<SheetFooter className='flex-row justify-end gap-2 pt-3'>
|
||||
<Button variant='outline' onClick={() => setOpen(false)}>
|
||||
{t('cancel')}
|
||||
</Button>
|
||||
<Button
|
||||
onClick={form.handleSubmit(async (values) => {
|
||||
const success = await onSubmit(values as T);
|
||||
if (success) setOpen(false);
|
||||
})}
|
||||
disabled={loading}
|
||||
>
|
||||
{loading && <Icon icon='mdi:loading' className='mr-2 animate-spin' />}
|
||||
{t('confirm')}
|
||||
</Button>
|
||||
</SheetFooter>
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
);
|
||||
}
|
||||
|
||||
export default function SubscribeApp() {
|
||||
const t = useTranslations('subscribe.app');
|
||||
const [loading, setLoading] = useState(false);
|
||||
const ref = useRef<ProTableActions>(null);
|
||||
|
||||
return (
|
||||
<ProTable<API.Application, { platform: string }>
|
||||
action={ref}
|
||||
header={{
|
||||
toolbar: (
|
||||
<SubscribeAppForm<API.CreateApplicationRequest>
|
||||
trigger={t('add')}
|
||||
title={t('createApp')}
|
||||
loading={loading}
|
||||
onSubmit={async (values) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
await createApplication(values);
|
||||
toast.success(t('createSuccess'));
|
||||
ref.current?.refresh();
|
||||
setLoading(false);
|
||||
return true;
|
||||
} catch (error) {
|
||||
setLoading(false);
|
||||
return false;
|
||||
}
|
||||
}}
|
||||
/>
|
||||
),
|
||||
}}
|
||||
params={[
|
||||
{
|
||||
key: 'platform',
|
||||
placeholder: t('platform'),
|
||||
options: [
|
||||
{ label: 'Windows', value: 'windows' },
|
||||
{ label: 'MacOS', value: 'mac' },
|
||||
{ label: 'Linux', value: 'linux' },
|
||||
{ label: 'Android', value: 'android' },
|
||||
{ label: 'iOS', value: 'ios' },
|
||||
],
|
||||
},
|
||||
]}
|
||||
request={async (_pagination, filters) => {
|
||||
const { data } = await getApplication();
|
||||
const flatApps = Object.entries(data.data || {}).flatMap(([platform, apps]) =>
|
||||
(apps as API.Application[]).map((app) => ({
|
||||
...app,
|
||||
platform,
|
||||
})),
|
||||
);
|
||||
return {
|
||||
list: filters.platform
|
||||
? flatApps.filter((app) => app.platform === filters.platform)
|
||||
: flatApps,
|
||||
total: 0,
|
||||
};
|
||||
}}
|
||||
columns={[
|
||||
{
|
||||
accessorKey: 'platform',
|
||||
header: t('platform'),
|
||||
cell: ({ row }) => row.getValue('platform'),
|
||||
},
|
||||
{
|
||||
accessorKey: 'subscribe_type',
|
||||
header: t('subscriptionProtocol'),
|
||||
cell: ({ row }) => row.getValue('subscribe_type'),
|
||||
},
|
||||
{
|
||||
accessorKey: 'name',
|
||||
header: t('appName'),
|
||||
},
|
||||
{
|
||||
accessorKey: 'icon',
|
||||
header: t('appIcon'),
|
||||
cell: ({ row }) => (
|
||||
<Image
|
||||
src={row.getValue('icon')}
|
||||
alt={row.getValue('name')}
|
||||
className='h-8 w-8 rounded-md'
|
||||
width={32}
|
||||
height={32}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{
|
||||
accessorKey: 'url',
|
||||
header: t('appDownloadURL'),
|
||||
},
|
||||
]}
|
||||
actions={{
|
||||
render: (row) => [
|
||||
<SubscribeAppForm<API.UpdateApplicationRequest>
|
||||
key='edit'
|
||||
trigger={<Button>{t('edit')}</Button>}
|
||||
title={t('editApp')}
|
||||
loading={loading}
|
||||
initialValues={{
|
||||
...row,
|
||||
}}
|
||||
onSubmit={async (values) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
await updateApplication({
|
||||
...values,
|
||||
id: row.id,
|
||||
});
|
||||
toast.success(t('updateSuccess'));
|
||||
ref.current?.refresh();
|
||||
setLoading(false);
|
||||
return true;
|
||||
} catch (error) {
|
||||
setLoading(false);
|
||||
return false;
|
||||
}
|
||||
}}
|
||||
/>,
|
||||
<ConfirmButton
|
||||
key='delete'
|
||||
trigger={<Button variant='destructive'>{t('delete')}</Button>}
|
||||
title={t('confirmDelete')}
|
||||
description={t('deleteWarning')}
|
||||
onConfirm={async () => {
|
||||
await deleteApplication({ id: row.id! });
|
||||
toast.success(t('deleteSuccess'));
|
||||
ref.current?.refresh();
|
||||
}}
|
||||
cancelText={t('cancel')}
|
||||
confirmText={t('confirm')}
|
||||
/>,
|
||||
],
|
||||
batchRender: (rows) => [
|
||||
<ConfirmButton
|
||||
key='delete'
|
||||
trigger={<Button variant='destructive'>{t('batchDelete')}</Button>}
|
||||
title={t('confirmDelete')}
|
||||
description={t('deleteWarning')}
|
||||
onConfirm={async () => {
|
||||
await Promise.all(rows.map((row) => deleteApplication({ id: row.id! })));
|
||||
toast.success(t('deleteSuccess'));
|
||||
ref.current?.reset();
|
||||
}}
|
||||
cancelText={t('cancel')}
|
||||
confirmText={t('confirm')}
|
||||
/>,
|
||||
],
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@ -3,7 +3,6 @@
|
||||
import { getNodeGroupList, getNodeList } from '@/services/admin/server';
|
||||
import { getSubscribeGroupList } from '@/services/admin/subscribe';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { Icon } from '@iconify/react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import {
|
||||
Accordion,
|
||||
@ -38,6 +37,7 @@ import { Combobox } from '@workspace/ui/custom-components/combobox';
|
||||
import { ArrayInput } from '@workspace/ui/custom-components/dynamic-Inputs';
|
||||
import { JSONEditor } from '@workspace/ui/custom-components/editor';
|
||||
import { EnhancedInput } from '@workspace/ui/custom-components/enhanced-input';
|
||||
import { Icon } from '@workspace/ui/custom-components/icon';
|
||||
import { evaluateWithPrecision, unitConversion } from '@workspace/ui/utils';
|
||||
import { CreditCard, Server, Settings } from 'lucide-react';
|
||||
import { useTranslations } from 'next-intl';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user