refactor: 优化eslint配置,提升代码质量 (#1341)
* feat: 统一并标准化eslint * lint: napcat.webui * lint: napcat.webui * lint: napcat.core * build: fix * lint: napcat.webui * refactor: 重构eslint * Update README.md
This commit is contained in:
651 files changed
+37978
-37584
No files matched your search
@@ -1,87 +1,87 @@
|
||||
import { Button } from '@heroui/button'
|
||||
import { Input } from '@heroui/input'
|
||||
import { ModalBody, ModalFooter } from '@heroui/modal'
|
||||
import { Select, SelectItem } from '@heroui/select'
|
||||
import { ReactElement, useEffect } from 'react'
|
||||
import { Controller, useForm } from 'react-hook-form'
|
||||
import { Button } from '@heroui/button';
|
||||
import { Input } from '@heroui/input';
|
||||
import { ModalBody, ModalFooter } from '@heroui/modal';
|
||||
import { Select, SelectItem } from '@heroui/select';
|
||||
import { ReactElement, useEffect } from 'react';
|
||||
import { Controller, useForm } from 'react-hook-form';
|
||||
import type {
|
||||
DefaultValues,
|
||||
Path,
|
||||
PathValue,
|
||||
SubmitHandler
|
||||
} from 'react-hook-form'
|
||||
import toast from 'react-hot-toast'
|
||||
SubmitHandler,
|
||||
} from 'react-hook-form';
|
||||
import toast from 'react-hot-toast';
|
||||
|
||||
import SwitchCard from '../switch_card'
|
||||
import SwitchCard from '../switch_card';
|
||||
|
||||
export type FieldTypes = 'input' | 'select' | 'switch'
|
||||
export type FieldTypes = 'input' | 'select' | 'switch';
|
||||
|
||||
type NetworkConfigType = OneBotConfig['network']
|
||||
type NetworkConfigType = OneBotConfig['network'];
|
||||
|
||||
export interface Field<T extends keyof OneBotConfig['network']> {
|
||||
name: keyof NetworkConfigType[T][0]
|
||||
label: string
|
||||
type: FieldTypes
|
||||
options?: Array<{ key: string; value: string }>
|
||||
placeholder?: string
|
||||
isRequired?: boolean
|
||||
isDisabled?: boolean
|
||||
description?: string
|
||||
colSpan?: 1 | 2
|
||||
name: keyof NetworkConfigType[T][0];
|
||||
label: string;
|
||||
type: FieldTypes;
|
||||
options?: Array<{ key: string; value: string; }>;
|
||||
placeholder?: string;
|
||||
isRequired?: boolean;
|
||||
isDisabled?: boolean;
|
||||
description?: string;
|
||||
colSpan?: 1 | 2;
|
||||
}
|
||||
|
||||
export interface GenericFormProps<T extends keyof NetworkConfigType> {
|
||||
data?: NetworkConfigType[T][0]
|
||||
defaultValues: DefaultValues<NetworkConfigType[T][0]>
|
||||
onClose: () => void
|
||||
onSubmit: (data: NetworkConfigType[T][0]) => Promise<void>
|
||||
fields: Array<Field<T>>
|
||||
data?: NetworkConfigType[T][0];
|
||||
defaultValues: DefaultValues<NetworkConfigType[T][0]>;
|
||||
onClose: () => void;
|
||||
onSubmit: (data: NetworkConfigType[T][0]) => Promise<void>;
|
||||
fields: Array<Field<T>>;
|
||||
}
|
||||
|
||||
const GenericForm = <T extends keyof NetworkConfigType>({
|
||||
const GenericForm = <T extends keyof NetworkConfigType> ({
|
||||
data,
|
||||
defaultValues,
|
||||
onClose,
|
||||
onSubmit,
|
||||
fields
|
||||
fields,
|
||||
}: GenericFormProps<T>): ReactElement => {
|
||||
const { control, handleSubmit, formState, setValue, reset } = useForm<
|
||||
NetworkConfigType[T][0]
|
||||
>({
|
||||
defaultValues
|
||||
})
|
||||
defaultValues,
|
||||
});
|
||||
|
||||
const submitAction: SubmitHandler<NetworkConfigType[T][0]> = async (data) => {
|
||||
await onSubmit(data)
|
||||
onClose()
|
||||
}
|
||||
await onSubmit(data);
|
||||
onClose();
|
||||
};
|
||||
|
||||
const _onSubmit = handleSubmit(submitAction, (e) => {
|
||||
for (const error in e) {
|
||||
toast.error(e[error]?.message as string)
|
||||
return
|
||||
const errors = Object.values(e);
|
||||
if (errors.length > 0) {
|
||||
toast.error(errors[0]?.message as string);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
const keys = Object.keys(data) as Path<NetworkConfig[T][0]>[]
|
||||
const keys = Object.keys(data) as Path<NetworkConfig[T][0]>[];
|
||||
for (const key of keys) {
|
||||
const value = data[key] as PathValue<
|
||||
NetworkConfig[T][0],
|
||||
Path<NetworkConfig[T][0]>
|
||||
>
|
||||
setValue(key, value)
|
||||
>;
|
||||
setValue(key, value);
|
||||
}
|
||||
} else {
|
||||
reset()
|
||||
reset();
|
||||
}
|
||||
}, [data, reset, setValue])
|
||||
}, [data, reset, setValue]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<ModalBody>
|
||||
<div className="grid grid-cols-2 gap-y-4 gap-x-2 w-full">
|
||||
<div className='grid grid-cols-2 gap-y-4 gap-x-2 w-full'>
|
||||
{fields.map((field) => (
|
||||
<div
|
||||
key={field.name as string}
|
||||
@@ -93,9 +93,9 @@ const GenericForm = <T extends keyof NetworkConfigType>({
|
||||
rules={
|
||||
field.isRequired
|
||||
? {
|
||||
required: `请填写${field.label}`
|
||||
}
|
||||
: void 0
|
||||
required: `请填写${field.label}`,
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
render={({ field: controllerField }) => {
|
||||
switch (field.type) {
|
||||
@@ -103,15 +103,14 @@ const GenericForm = <T extends keyof NetworkConfigType>({
|
||||
return (
|
||||
<Input
|
||||
value={controllerField.value as string}
|
||||
onChange={controllerField.onChange}
|
||||
onBlur={controllerField.onBlur}
|
||||
onValueChange={(value) => controllerField.onChange(value)}
|
||||
ref={controllerField.ref}
|
||||
isRequired={field.isRequired}
|
||||
isDisabled={field.isDisabled}
|
||||
label={field.label}
|
||||
placeholder={field.placeholder}
|
||||
/>
|
||||
)
|
||||
);
|
||||
case 'select':
|
||||
return (
|
||||
<Select
|
||||
@@ -129,7 +128,7 @@ const GenericForm = <T extends keyof NetworkConfigType>({
|
||||
</SelectItem>
|
||||
)) || <></>}
|
||||
</Select>
|
||||
)
|
||||
);
|
||||
case 'switch':
|
||||
return (
|
||||
<SwitchCard
|
||||
@@ -138,9 +137,9 @@ const GenericForm = <T extends keyof NetworkConfigType>({
|
||||
description={field.description}
|
||||
label={field.label}
|
||||
/>
|
||||
)
|
||||
);
|
||||
default:
|
||||
return <></>
|
||||
return <></>;
|
||||
}
|
||||
}}
|
||||
/>
|
||||
@@ -150,15 +149,15 @@ const GenericForm = <T extends keyof NetworkConfigType>({
|
||||
</ModalBody>
|
||||
<ModalFooter>
|
||||
<Button
|
||||
color="primary"
|
||||
color='primary'
|
||||
isDisabled={formState.isSubmitting}
|
||||
variant="light"
|
||||
variant='light'
|
||||
onPress={onClose}
|
||||
>
|
||||
关闭
|
||||
</Button>
|
||||
<Button
|
||||
color="primary"
|
||||
color='primary'
|
||||
isLoading={formState.isSubmitting}
|
||||
onPress={() => _onSubmit()}
|
||||
>
|
||||
@@ -166,16 +165,16 @@ const GenericForm = <T extends keyof NetworkConfigType>({
|
||||
</Button>
|
||||
</ModalFooter>
|
||||
</>
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default GenericForm
|
||||
export function random_token(length: number) {
|
||||
export default GenericForm;
|
||||
export function random_token (length: number) {
|
||||
const chars =
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@#$%^&*()-_=+[]{}|;:,.<>?'
|
||||
let result = ''
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@#$%^&*()-_=+[]{}|;:,.<>?';
|
||||
let result = '';
|
||||
for (let i = 0; i < length; i++) {
|
||||
result += chars.charAt(Math.floor(Math.random() * chars.length))
|
||||
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
||||
}
|
||||
return result
|
||||
return result;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import GenericForm, { random_token } from './generic_form'
|
||||
import type { Field } from './generic_form'
|
||||
import GenericForm, { random_token } from './generic_form';
|
||||
import type { Field } from './generic_form';
|
||||
|
||||
export interface HTTPClientFormProps {
|
||||
data?: OneBotConfig['network']['httpClients'][0]
|
||||
@@ -7,12 +7,12 @@ export interface HTTPClientFormProps {
|
||||
onSubmit: (data: OneBotConfig['network']['httpClients'][0]) => Promise<void>
|
||||
}
|
||||
|
||||
type HTTPClientFormType = OneBotConfig['network']['httpClients']
|
||||
type HTTPClientFormType = OneBotConfig['network']['httpClients'];
|
||||
|
||||
const HTTPClientForm: React.FC<HTTPClientFormProps> = ({
|
||||
data,
|
||||
onClose,
|
||||
onSubmit
|
||||
onSubmit,
|
||||
}) => {
|
||||
const defaultValues: HTTPClientFormType[0] = {
|
||||
enable: false,
|
||||
@@ -21,8 +21,8 @@ const HTTPClientForm: React.FC<HTTPClientFormProps> = ({
|
||||
reportSelfMessage: false,
|
||||
messagePostFormat: 'array',
|
||||
token: random_token(16),
|
||||
debug: false
|
||||
}
|
||||
debug: false,
|
||||
};
|
||||
|
||||
const fields: Field<'httpClients'>[] = [
|
||||
{
|
||||
@@ -30,14 +30,14 @@ const HTTPClientForm: React.FC<HTTPClientFormProps> = ({
|
||||
label: '启用',
|
||||
type: 'switch',
|
||||
description: '保存后启用此配置',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'debug',
|
||||
label: '开启Debug',
|
||||
type: 'switch',
|
||||
description: '是否开启调试模式',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'name',
|
||||
@@ -45,21 +45,21 @@ const HTTPClientForm: React.FC<HTTPClientFormProps> = ({
|
||||
type: 'input',
|
||||
placeholder: '请输入名称',
|
||||
isRequired: true,
|
||||
isDisabled: !!data
|
||||
isDisabled: !!data,
|
||||
},
|
||||
{
|
||||
name: 'url',
|
||||
label: 'URL',
|
||||
type: 'input',
|
||||
placeholder: '请输入URL',
|
||||
isRequired: true
|
||||
isRequired: true,
|
||||
},
|
||||
{
|
||||
name: 'reportSelfMessage',
|
||||
label: '上报自身消息',
|
||||
type: 'switch',
|
||||
description: '是否上报自身消息',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'messagePostFormat',
|
||||
@@ -69,17 +69,17 @@ const HTTPClientForm: React.FC<HTTPClientFormProps> = ({
|
||||
isRequired: true,
|
||||
options: [
|
||||
{ key: 'array', value: 'Array' },
|
||||
{ key: 'string', value: 'String' }
|
||||
{ key: 'string', value: 'String' },
|
||||
],
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'token',
|
||||
label: 'Token',
|
||||
type: 'input',
|
||||
placeholder: '请输入Token'
|
||||
}
|
||||
]
|
||||
placeholder: '请输入Token',
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<GenericForm
|
||||
@@ -89,7 +89,7 @@ const HTTPClientForm: React.FC<HTTPClientFormProps> = ({
|
||||
onSubmit={onSubmit}
|
||||
fields={fields}
|
||||
/>
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default HTTPClientForm
|
||||
export default HTTPClientForm;
|
||||
@@ -1,5 +1,5 @@
|
||||
import GenericForm, { random_token } from './generic_form'
|
||||
import type { Field } from './generic_form'
|
||||
import GenericForm, { random_token } from './generic_form';
|
||||
import type { Field } from './generic_form';
|
||||
|
||||
export interface HTTPServerFormProps {
|
||||
data?: OneBotConfig['network']['httpServers'][0]
|
||||
@@ -7,12 +7,12 @@ export interface HTTPServerFormProps {
|
||||
onSubmit: (data: OneBotConfig['network']['httpServers'][0]) => Promise<void>
|
||||
}
|
||||
|
||||
type HTTPServerFormType = OneBotConfig['network']['httpServers']
|
||||
type HTTPServerFormType = OneBotConfig['network']['httpServers'];
|
||||
|
||||
const HTTPServerForm: React.FC<HTTPServerFormProps> = ({
|
||||
data,
|
||||
onClose,
|
||||
onSubmit
|
||||
onSubmit,
|
||||
}) => {
|
||||
const defaultValues: HTTPServerFormType[0] = {
|
||||
enable: false,
|
||||
@@ -23,8 +23,8 @@ const HTTPServerForm: React.FC<HTTPServerFormProps> = ({
|
||||
enableWebsocket: true,
|
||||
messagePostFormat: 'array',
|
||||
token: random_token(16),
|
||||
debug: false
|
||||
}
|
||||
debug: false,
|
||||
};
|
||||
|
||||
const fields: Field<'httpServers'>[] = [
|
||||
{
|
||||
@@ -32,14 +32,14 @@ const HTTPServerForm: React.FC<HTTPServerFormProps> = ({
|
||||
label: '启用',
|
||||
type: 'switch',
|
||||
description: '保存后启用此配置',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'debug',
|
||||
label: '开启Debug',
|
||||
type: 'switch',
|
||||
description: '是否开启调试模式',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'name',
|
||||
@@ -47,35 +47,35 @@ const HTTPServerForm: React.FC<HTTPServerFormProps> = ({
|
||||
type: 'input',
|
||||
placeholder: '请输入名称',
|
||||
isRequired: true,
|
||||
isDisabled: !!data
|
||||
isDisabled: !!data,
|
||||
},
|
||||
{
|
||||
name: 'host',
|
||||
label: 'Host',
|
||||
type: 'input',
|
||||
placeholder: '请输入主机地址',
|
||||
isRequired: true
|
||||
isRequired: true,
|
||||
},
|
||||
{
|
||||
name: 'port',
|
||||
label: 'Port',
|
||||
type: 'input',
|
||||
placeholder: '请输入端口',
|
||||
isRequired: true
|
||||
isRequired: true,
|
||||
},
|
||||
{
|
||||
name: 'enableCors',
|
||||
label: '启用CORS',
|
||||
type: 'switch',
|
||||
description: '是否启用CORS跨域',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'enableWebsocket',
|
||||
label: '启用Websocket',
|
||||
type: 'switch',
|
||||
description: '是否启用Websocket',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'messagePostFormat',
|
||||
@@ -85,16 +85,16 @@ const HTTPServerForm: React.FC<HTTPServerFormProps> = ({
|
||||
isRequired: true,
|
||||
options: [
|
||||
{ key: 'array', value: 'Array' },
|
||||
{ key: 'string', value: 'String' }
|
||||
]
|
||||
{ key: 'string', value: 'String' },
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'token',
|
||||
label: 'Token',
|
||||
type: 'input',
|
||||
placeholder: '请输入Token'
|
||||
}
|
||||
]
|
||||
placeholder: '请输入Token',
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<GenericForm
|
||||
@@ -104,7 +104,7 @@ const HTTPServerForm: React.FC<HTTPServerFormProps> = ({
|
||||
onSubmit={onSubmit}
|
||||
fields={fields}
|
||||
/>
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default HTTPServerForm
|
||||
export default HTTPServerForm;
|
||||
@@ -1,5 +1,5 @@
|
||||
import GenericForm, { random_token } from './generic_form'
|
||||
import type { Field } from './generic_form'
|
||||
import GenericForm, { random_token } from './generic_form';
|
||||
import type { Field } from './generic_form';
|
||||
|
||||
export interface HTTPServerSSEFormProps {
|
||||
data?: OneBotConfig['network']['httpSseServers'][0]
|
||||
@@ -9,12 +9,12 @@ export interface HTTPServerSSEFormProps {
|
||||
) => Promise<void>
|
||||
}
|
||||
|
||||
type HTTPServerSSEFormType = OneBotConfig['network']['httpSseServers']
|
||||
type HTTPServerSSEFormType = OneBotConfig['network']['httpSseServers'];
|
||||
|
||||
const HTTPServerSSEForm: React.FC<HTTPServerSSEFormProps> = ({
|
||||
data,
|
||||
onClose,
|
||||
onSubmit
|
||||
onSubmit,
|
||||
}) => {
|
||||
const defaultValues: HTTPServerSSEFormType[0] = {
|
||||
enable: false,
|
||||
@@ -26,8 +26,8 @@ const HTTPServerSSEForm: React.FC<HTTPServerSSEFormProps> = ({
|
||||
messagePostFormat: 'array',
|
||||
token: random_token(16),
|
||||
debug: false,
|
||||
reportSelfMessage: false
|
||||
}
|
||||
reportSelfMessage: false,
|
||||
};
|
||||
|
||||
const fields: Field<'httpSseServers'>[] = [
|
||||
{
|
||||
@@ -35,14 +35,14 @@ const HTTPServerSSEForm: React.FC<HTTPServerSSEFormProps> = ({
|
||||
label: '启用',
|
||||
type: 'switch',
|
||||
description: '保存后启用此配置',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'debug',
|
||||
label: '开启Debug',
|
||||
type: 'switch',
|
||||
description: '是否开启调试模式',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'name',
|
||||
@@ -50,35 +50,35 @@ const HTTPServerSSEForm: React.FC<HTTPServerSSEFormProps> = ({
|
||||
type: 'input',
|
||||
placeholder: '请输入名称',
|
||||
isRequired: true,
|
||||
isDisabled: !!data
|
||||
isDisabled: !!data,
|
||||
},
|
||||
{
|
||||
name: 'host',
|
||||
label: 'Host',
|
||||
type: 'input',
|
||||
placeholder: '请输入主机地址',
|
||||
isRequired: true
|
||||
isRequired: true,
|
||||
},
|
||||
{
|
||||
name: 'port',
|
||||
label: 'Port',
|
||||
type: 'input',
|
||||
placeholder: '请输入端口',
|
||||
isRequired: true
|
||||
isRequired: true,
|
||||
},
|
||||
{
|
||||
name: 'enableCors',
|
||||
label: '启用CORS',
|
||||
type: 'switch',
|
||||
description: '是否启用CORS跨域',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'enableWebsocket',
|
||||
label: '启用Websocket',
|
||||
type: 'switch',
|
||||
description: '是否启用Websocket',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'messagePostFormat',
|
||||
@@ -88,23 +88,23 @@ const HTTPServerSSEForm: React.FC<HTTPServerSSEFormProps> = ({
|
||||
isRequired: true,
|
||||
options: [
|
||||
{ key: 'array', value: 'Array' },
|
||||
{ key: 'string', value: 'String' }
|
||||
]
|
||||
{ key: 'string', value: 'String' },
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'token',
|
||||
label: 'Token',
|
||||
type: 'input',
|
||||
placeholder: '请输入Token'
|
||||
placeholder: '请输入Token',
|
||||
},
|
||||
{
|
||||
name: 'reportSelfMessage',
|
||||
label: '上报自身消息',
|
||||
type: 'switch',
|
||||
description: '是否上报自身消息',
|
||||
colSpan: 1
|
||||
}
|
||||
]
|
||||
colSpan: 1,
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<GenericForm
|
||||
@@ -114,7 +114,7 @@ const HTTPServerSSEForm: React.FC<HTTPServerSSEFormProps> = ({
|
||||
onSubmit={onSubmit}
|
||||
fields={fields}
|
||||
/>
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default HTTPServerSSEForm
|
||||
export default HTTPServerSSEForm;
|
||||
@@ -1,54 +1,54 @@
|
||||
import { Modal, ModalContent, ModalHeader } from '@heroui/modal'
|
||||
import toast from 'react-hot-toast'
|
||||
import { Modal, ModalContent, ModalHeader } from '@heroui/modal';
|
||||
import toast from 'react-hot-toast';
|
||||
|
||||
import useConfig from '@/hooks/use-config'
|
||||
import useConfig from '@/hooks/use-config';
|
||||
|
||||
import HTTPClientForm from './http_client'
|
||||
import HTTPServerForm from './http_server'
|
||||
import HTTPServerSSEForm from './http_sse'
|
||||
import WebsocketClientForm from './ws_client'
|
||||
import WebsocketServerForm from './ws_server'
|
||||
import HTTPClientForm from './http_client';
|
||||
import HTTPServerForm from './http_server';
|
||||
import HTTPServerSSEForm from './http_sse';
|
||||
import WebsocketClientForm from './ws_client';
|
||||
import WebsocketServerForm from './ws_server';
|
||||
|
||||
const modalTitle = {
|
||||
httpServers: 'HTTP Server',
|
||||
httpClients: 'HTTP Client',
|
||||
httpSseServers: 'HTTP SSE Server',
|
||||
websocketServers: 'Websocket Server',
|
||||
websocketClients: 'Websocket Client'
|
||||
}
|
||||
websocketClients: 'Websocket Client',
|
||||
};
|
||||
|
||||
export interface NetworkFormModalProps<
|
||||
T extends keyof OneBotConfig['network']
|
||||
> {
|
||||
isOpen: boolean
|
||||
field: T
|
||||
data?: OneBotConfig['network'][T][0]
|
||||
onOpenChange: (isOpen: boolean) => void
|
||||
isOpen: boolean;
|
||||
field: T;
|
||||
data?: OneBotConfig['network'][T][0];
|
||||
onOpenChange: (isOpen: boolean) => void;
|
||||
}
|
||||
|
||||
const NetworkFormModal = <T extends keyof OneBotConfig['network']>(
|
||||
const NetworkFormModal = <T extends keyof OneBotConfig['network']> (
|
||||
props: NetworkFormModalProps<T>
|
||||
) => {
|
||||
const { isOpen, onOpenChange, field, data } = props
|
||||
const { createNetworkConfig, updateNetworkConfig } = useConfig()
|
||||
const isCreate = !data
|
||||
const { isOpen, onOpenChange, field, data } = props;
|
||||
const { createNetworkConfig, updateNetworkConfig } = useConfig();
|
||||
const isCreate = !data;
|
||||
|
||||
const onSubmit = async (data: OneBotConfig['network'][typeof field][0]) => {
|
||||
try {
|
||||
if (isCreate) {
|
||||
await createNetworkConfig(field, data)
|
||||
await createNetworkConfig(field, data);
|
||||
} else {
|
||||
await updateNetworkConfig(field, data)
|
||||
await updateNetworkConfig(field, data);
|
||||
}
|
||||
toast.success('保存配置成功')
|
||||
toast.success('保存配置成功');
|
||||
} catch (error) {
|
||||
const msg = (error as Error).message
|
||||
const msg = (error as Error).message;
|
||||
|
||||
toast.error(`保存配置失败: ${msg}`)
|
||||
toast.error(`保存配置失败: ${msg}`);
|
||||
|
||||
throw error
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const renderFormComponent = (onClose: () => void) => {
|
||||
switch (field) {
|
||||
@@ -59,7 +59,7 @@ const NetworkFormModal = <T extends keyof OneBotConfig['network']>(
|
||||
onClose={onClose}
|
||||
onSubmit={onSubmit}
|
||||
/>
|
||||
)
|
||||
);
|
||||
case 'httpClients':
|
||||
return (
|
||||
<HTTPClientForm
|
||||
@@ -67,7 +67,7 @@ const NetworkFormModal = <T extends keyof OneBotConfig['network']>(
|
||||
onClose={onClose}
|
||||
onSubmit={onSubmit}
|
||||
/>
|
||||
)
|
||||
);
|
||||
case 'websocketServers':
|
||||
return (
|
||||
<WebsocketServerForm
|
||||
@@ -75,7 +75,7 @@ const NetworkFormModal = <T extends keyof OneBotConfig['network']>(
|
||||
onClose={onClose}
|
||||
onSubmit={onSubmit}
|
||||
/>
|
||||
)
|
||||
);
|
||||
case 'websocketClients':
|
||||
return (
|
||||
<WebsocketClientForm
|
||||
@@ -83,7 +83,7 @@ const NetworkFormModal = <T extends keyof OneBotConfig['network']>(
|
||||
onClose={onClose}
|
||||
onSubmit={onSubmit}
|
||||
/>
|
||||
)
|
||||
);
|
||||
case 'httpSseServers':
|
||||
return (
|
||||
<HTTPServerSSEForm
|
||||
@@ -91,25 +91,25 @@ const NetworkFormModal = <T extends keyof OneBotConfig['network']>(
|
||||
onClose={onClose}
|
||||
onSubmit={onSubmit}
|
||||
/>
|
||||
)
|
||||
);
|
||||
default:
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Modal
|
||||
backdrop="blur"
|
||||
backdrop='blur'
|
||||
isDismissable={false}
|
||||
isOpen={isOpen}
|
||||
size="lg"
|
||||
scrollBehavior="outside"
|
||||
size='lg'
|
||||
scrollBehavior='outside'
|
||||
onOpenChange={onOpenChange}
|
||||
>
|
||||
<ModalContent>
|
||||
{(onClose) => (
|
||||
<>
|
||||
<ModalHeader className="flex flex-col gap-1">
|
||||
<ModalHeader className='flex flex-col gap-1'>
|
||||
{modalTitle[field]}
|
||||
</ModalHeader>
|
||||
{renderFormComponent(onClose)}
|
||||
@@ -117,7 +117,7 @@ const NetworkFormModal = <T extends keyof OneBotConfig['network']>(
|
||||
)}
|
||||
</ModalContent>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default NetworkFormModal
|
||||
export default NetworkFormModal;
|
||||
@@ -1,5 +1,5 @@
|
||||
import GenericForm, { random_token } from './generic_form'
|
||||
import type { Field } from './generic_form'
|
||||
import GenericForm, { random_token } from './generic_form';
|
||||
import type { Field } from './generic_form';
|
||||
|
||||
export interface WebsocketClientFormProps {
|
||||
data?: OneBotConfig['network']['websocketClients'][0]
|
||||
@@ -9,12 +9,12 @@ export interface WebsocketClientFormProps {
|
||||
) => Promise<void>
|
||||
}
|
||||
|
||||
type WebsocketClientFormType = OneBotConfig['network']['websocketClients']
|
||||
type WebsocketClientFormType = OneBotConfig['network']['websocketClients'];
|
||||
|
||||
const WebsocketClientForm: React.FC<WebsocketClientFormProps> = ({
|
||||
data,
|
||||
onClose,
|
||||
onSubmit
|
||||
onSubmit,
|
||||
}) => {
|
||||
const defaultValues: WebsocketClientFormType[0] = {
|
||||
enable: false,
|
||||
@@ -25,8 +25,8 @@ const WebsocketClientForm: React.FC<WebsocketClientFormProps> = ({
|
||||
token: random_token(16),
|
||||
debug: false,
|
||||
heartInterval: 30000,
|
||||
reconnectInterval: 30000
|
||||
}
|
||||
reconnectInterval: 30000,
|
||||
};
|
||||
|
||||
const fields: Field<'websocketClients'>[] = [
|
||||
{
|
||||
@@ -34,14 +34,14 @@ const WebsocketClientForm: React.FC<WebsocketClientFormProps> = ({
|
||||
label: '启用',
|
||||
type: 'switch',
|
||||
description: '保存后启用此配置',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'debug',
|
||||
label: '开启Debug',
|
||||
type: 'switch',
|
||||
description: '是否开启调试模式',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'name',
|
||||
@@ -49,21 +49,21 @@ const WebsocketClientForm: React.FC<WebsocketClientFormProps> = ({
|
||||
type: 'input',
|
||||
placeholder: '请输入名称',
|
||||
isRequired: true,
|
||||
isDisabled: !!data
|
||||
isDisabled: !!data,
|
||||
},
|
||||
{
|
||||
name: 'url',
|
||||
label: 'URL',
|
||||
type: 'input',
|
||||
placeholder: '请输入URL',
|
||||
isRequired: true
|
||||
isRequired: true,
|
||||
},
|
||||
{
|
||||
name: 'reportSelfMessage',
|
||||
label: '上报自身消息',
|
||||
type: 'switch',
|
||||
description: '是否上报自身消息',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'messagePostFormat',
|
||||
@@ -73,15 +73,15 @@ const WebsocketClientForm: React.FC<WebsocketClientFormProps> = ({
|
||||
isRequired: true,
|
||||
options: [
|
||||
{ key: 'array', value: 'Array' },
|
||||
{ key: 'string', value: 'String' }
|
||||
{ key: 'string', value: 'String' },
|
||||
],
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'token',
|
||||
label: 'Token',
|
||||
type: 'input',
|
||||
placeholder: '请输入Token'
|
||||
placeholder: '请输入Token',
|
||||
},
|
||||
{
|
||||
name: 'heartInterval',
|
||||
@@ -89,7 +89,7 @@ const WebsocketClientForm: React.FC<WebsocketClientFormProps> = ({
|
||||
type: 'input',
|
||||
placeholder: '请输入心跳间隔',
|
||||
isRequired: true,
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'reconnectInterval',
|
||||
@@ -97,9 +97,9 @@ const WebsocketClientForm: React.FC<WebsocketClientFormProps> = ({
|
||||
type: 'input',
|
||||
placeholder: '请输入重连间隔',
|
||||
isRequired: true,
|
||||
colSpan: 1
|
||||
}
|
||||
]
|
||||
colSpan: 1,
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<GenericForm
|
||||
@@ -109,7 +109,7 @@ const WebsocketClientForm: React.FC<WebsocketClientFormProps> = ({
|
||||
onSubmit={onSubmit}
|
||||
fields={fields}
|
||||
/>
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default WebsocketClientForm
|
||||
export default WebsocketClientForm;
|
||||
@@ -1,5 +1,5 @@
|
||||
import GenericForm, { random_token } from './generic_form'
|
||||
import type { Field } from './generic_form'
|
||||
import GenericForm, { random_token } from './generic_form';
|
||||
import type { Field } from './generic_form';
|
||||
|
||||
export interface WebsocketServerFormProps {
|
||||
data?: OneBotConfig['network']['websocketServers'][0]
|
||||
@@ -9,12 +9,12 @@ export interface WebsocketServerFormProps {
|
||||
) => Promise<void>
|
||||
}
|
||||
|
||||
type WebsocketServerFormType = OneBotConfig['network']['websocketServers']
|
||||
type WebsocketServerFormType = OneBotConfig['network']['websocketServers'];
|
||||
|
||||
const WebsocketServerForm: React.FC<WebsocketServerFormProps> = ({
|
||||
data,
|
||||
onClose,
|
||||
onSubmit
|
||||
onSubmit,
|
||||
}) => {
|
||||
const defaultValues: WebsocketServerFormType[0] = {
|
||||
enable: false,
|
||||
@@ -26,8 +26,8 @@ const WebsocketServerForm: React.FC<WebsocketServerFormProps> = ({
|
||||
messagePostFormat: 'array',
|
||||
token: random_token(16),
|
||||
debug: false,
|
||||
heartInterval: 30000
|
||||
}
|
||||
heartInterval: 30000,
|
||||
};
|
||||
|
||||
const fields: Field<'websocketServers'>[] = [
|
||||
{
|
||||
@@ -35,14 +35,14 @@ const WebsocketServerForm: React.FC<WebsocketServerFormProps> = ({
|
||||
label: '启用',
|
||||
type: 'switch',
|
||||
description: '保存后启用此配置',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'debug',
|
||||
label: '开启Debug',
|
||||
type: 'switch',
|
||||
description: '是否开启调试模式',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'name',
|
||||
@@ -50,14 +50,14 @@ const WebsocketServerForm: React.FC<WebsocketServerFormProps> = ({
|
||||
type: 'input',
|
||||
placeholder: '请输入名称',
|
||||
isRequired: true,
|
||||
isDisabled: !!data
|
||||
isDisabled: !!data,
|
||||
},
|
||||
{
|
||||
name: 'host',
|
||||
label: 'Host',
|
||||
type: 'input',
|
||||
placeholder: '请输入主机地址',
|
||||
isRequired: true
|
||||
isRequired: true,
|
||||
},
|
||||
{
|
||||
name: 'port',
|
||||
@@ -65,7 +65,7 @@ const WebsocketServerForm: React.FC<WebsocketServerFormProps> = ({
|
||||
type: 'input',
|
||||
placeholder: '请输入端口',
|
||||
isRequired: true,
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'messagePostFormat',
|
||||
@@ -75,38 +75,38 @@ const WebsocketServerForm: React.FC<WebsocketServerFormProps> = ({
|
||||
isRequired: true,
|
||||
options: [
|
||||
{ key: 'array', value: 'Array' },
|
||||
{ key: 'string', value: 'String' }
|
||||
{ key: 'string', value: 'String' },
|
||||
],
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'reportSelfMessage',
|
||||
label: '上报自身消息',
|
||||
type: 'switch',
|
||||
description: '是否上报自身消息',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'enableForcePushEvent',
|
||||
label: '强制推送事件',
|
||||
type: 'switch',
|
||||
description: '是否强制推送事件',
|
||||
colSpan: 1
|
||||
colSpan: 1,
|
||||
},
|
||||
{
|
||||
name: 'token',
|
||||
label: 'Token',
|
||||
type: 'input',
|
||||
placeholder: '请输入Token'
|
||||
placeholder: '请输入Token',
|
||||
},
|
||||
{
|
||||
name: 'heartInterval',
|
||||
label: '心跳间隔',
|
||||
type: 'input',
|
||||
placeholder: '请输入心跳间隔',
|
||||
isRequired: true
|
||||
}
|
||||
]
|
||||
isRequired: true,
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<GenericForm
|
||||
@@ -116,7 +116,7 @@ const WebsocketServerForm: React.FC<WebsocketServerFormProps> = ({
|
||||
onSubmit={onSubmit}
|
||||
fields={fields}
|
||||
/>
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default WebsocketServerForm
|
||||
export default WebsocketServerForm;
|
||||
Reference in new issue
Block a user