From 71a62caf8fd7c312f846e1a0fc7929085d6dd48d Mon Sep 17 00:00:00 2001 From: bietiaop <1527109126@qq.com> Date: Fri, 24 Jan 2025 22:36:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20sse=E9=85=8D=E7=BD=AE=E7=BC=BA=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../display_card/http_sse_server.tsx | 59 +++++++++ .../src/components/network_edit/http_sse.tsx | 120 ++++++++++++++++++ .../src/components/network_edit/modal.tsx | 10 ++ napcat.webui/src/pages/dashboard/network.tsx | 37 +++++- napcat.webui/src/store/modules/config.ts | 1 + napcat.webui/src/types/onebot_conf.d.ts | 5 + 6 files changed, 230 insertions(+), 2 deletions(-) create mode 100644 napcat.webui/src/components/display_card/http_sse_server.tsx create mode 100644 napcat.webui/src/components/network_edit/http_sse.tsx diff --git a/napcat.webui/src/components/display_card/http_sse_server.tsx b/napcat.webui/src/components/display_card/http_sse_server.tsx new file mode 100644 index 00000000..0b3bbd6f --- /dev/null +++ b/napcat.webui/src/components/display_card/http_sse_server.tsx @@ -0,0 +1,59 @@ +import { Chip } from '@heroui/chip' + +import NetworkDisplayCard from './common_card' +import type { NetworkDisplayCardFields } from './common_card' + +interface HTTPSSEServerDisplayCardProps { + data: OneBotConfig['network']['httpSseServers'][0] + showType?: boolean + onEdit: () => void + onEnable: () => Promise + onDelete: () => Promise + onEnableDebug: () => Promise +} + +const HTTPSSEServerDisplayCard: React.FC = ( + props +) => { + const { data, showType, onEdit, onEnable, onDelete, onEnableDebug } = props + const { host, port, enableCors, enableWebsocket, messagePostFormat } = data + + const fields: NetworkDisplayCardFields<'httpServers'> = [ + { label: '主机', value: host }, + { label: '端口', value: port }, + { label: '消息格式', value: messagePostFormat }, + { + label: 'CORS', + value: enableCors, + render: (value) => ( + + {value ? '已启用' : '未启用'} + + ) + }, + { + label: 'WS', + value: enableWebsocket, + render: (value) => ( + + {value ? '已启用' : '未启用'} + + ) + } + ] + + return ( + + ) +} + +export default HTTPSSEServerDisplayCard diff --git a/napcat.webui/src/components/network_edit/http_sse.tsx b/napcat.webui/src/components/network_edit/http_sse.tsx new file mode 100644 index 00000000..fa94f278 --- /dev/null +++ b/napcat.webui/src/components/network_edit/http_sse.tsx @@ -0,0 +1,120 @@ +import GenericForm from './generic_form' +import type { Field } from './generic_form' + +export interface HTTPServerSSEFormProps { + data?: OneBotConfig['network']['httpSseServers'][0] + onClose: () => void + onSubmit: ( + data: OneBotConfig['network']['httpSseServers'][0] + ) => Promise +} + +type HTTPServerSSEFormType = OneBotConfig['network']['httpSseServers'] + +const HTTPServerSSEForm: React.FC = ({ + data, + onClose, + onSubmit +}) => { + const defaultValues: HTTPServerSSEFormType[0] = { + enable: false, + name: '', + host: '0.0.0.0', + port: 3000, + enableCors: true, + enableWebsocket: true, + messagePostFormat: 'array', + token: '', + debug: false, + reportSelfMessage: false + } + + const fields: Field<'httpSseServers'>[] = [ + { + name: 'enable', + label: '启用', + type: 'switch', + description: '保存后启用此配置', + colSpan: 1 + }, + { + name: 'debug', + label: '开启Debug', + type: 'switch', + description: '是否开启调试模式', + colSpan: 1 + }, + { + name: 'name', + label: '名称', + type: 'input', + placeholder: '请输入名称', + isRequired: true, + isDisabled: !!data + }, + { + name: 'host', + label: 'Host', + type: 'input', + placeholder: '请输入主机地址', + isRequired: true + }, + { + name: 'port', + label: 'Port', + type: 'input', + placeholder: '请输入端口', + isRequired: true + }, + { + name: 'enableCors', + label: '启用CORS', + type: 'switch', + description: '是否启用CORS跨域', + colSpan: 1 + }, + { + name: 'enableWebsocket', + label: '启用Websocket', + type: 'switch', + description: '是否启用Websocket', + colSpan: 1 + }, + { + name: 'messagePostFormat', + label: '消息格式', + type: 'select', + placeholder: '请选择消息格式', + isRequired: true, + options: [ + { key: 'array', value: 'Array' }, + { key: 'string', value: 'String' } + ] + }, + { + name: 'token', + label: 'Token', + type: 'input', + placeholder: '请输入Token' + }, + { + name: 'reportSelfMessage', + label: '上报自身消息', + type: 'switch', + description: '是否上报自身消息', + colSpan: 1 + } + ] + + return ( + + ) +} + +export default HTTPServerSSEForm diff --git a/napcat.webui/src/components/network_edit/modal.tsx b/napcat.webui/src/components/network_edit/modal.tsx index b451f043..39e2567e 100644 --- a/napcat.webui/src/components/network_edit/modal.tsx +++ b/napcat.webui/src/components/network_edit/modal.tsx @@ -5,12 +5,14 @@ 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' const modalTitle = { httpServers: 'HTTP Server', httpClients: 'HTTP Client', + httpSseServers: 'HTTP SSE Server', websocketServers: 'Websocket Server', websocketClients: 'Websocket Client' } @@ -82,6 +84,14 @@ const NetworkFormModal = ( onSubmit={onSubmit} /> ) + case 'httpSseServers': + return ( + + ) default: return null } diff --git a/napcat.webui/src/pages/dashboard/network.tsx b/napcat.webui/src/pages/dashboard/network.tsx index e9a5224d..f914a052 100644 --- a/napcat.webui/src/pages/dashboard/network.tsx +++ b/napcat.webui/src/pages/dashboard/network.tsx @@ -9,6 +9,7 @@ import { IoMdRefresh } from 'react-icons/io' import AddButton from '@/components/button/add_button' import HTTPClientDisplayCard from '@/components/display_card/http_client' import HTTPServerDisplayCard from '@/components/display_card/http_server' +import HTTPSSEServerDisplayCard from '@/components/display_card/http_sse_server' import WebsocketClientDisplayCard from '@/components/display_card/ws_client' import WebsocketServerDisplayCard from '@/components/display_card/ws_server' import NetworkFormModal from '@/components/network_edit/modal' @@ -59,7 +60,13 @@ export default function NetworkPage() { useState('httpServers') const [activeName, setActiveName] = useState('') const { - network: { httpServers, httpClients, websocketServers, websocketClients } + network: { + httpServers, + httpClients, + httpSseServers, + websocketServers, + websocketClients + } } = config const [loading, setLoading] = useState(false) const { isOpen, onOpen, onOpenChange } = useDisclosure() @@ -204,6 +211,26 @@ export default function NetworkPage() { }} /> ) + case 'httpSseServers': + return ( + { + await onDelete('httpSseServers', item.name) + }} + onEdit={() => { + onEdit('httpSseServers', item.name) + }} + onEnable={async () => { + await onEnable('httpSseServers', item.name) + }} + onEnableDebug={async () => { + await onEnableDebug('httpSseServers', item.name) + }} + /> + ) case 'websocketServers': return ( a.name.localeCompare(b.name)) .map((item) => { @@ -300,6 +328,11 @@ export default function NetworkPage() { title: 'HTTP客户端', items: httpClients.map((item) => renderCard('httpClients', item)) }, + { + key: 'httpSseServers', + title: 'HTTP SSE服务器', + items: httpSseServers.map((item) => renderCard('httpSseServers', item)) + }, { key: 'websocketServers', title: 'Websocket服务器', diff --git a/napcat.webui/src/store/modules/config.ts b/napcat.webui/src/store/modules/config.ts index 603838f7..2ff4eb6d 100644 --- a/napcat.webui/src/store/modules/config.ts +++ b/napcat.webui/src/store/modules/config.ts @@ -11,6 +11,7 @@ const initialState: ConfigState = { network: { httpServers: [], httpClients: [], + httpSseServers: [], websocketServers: [], websocketClients: [] }, diff --git a/napcat.webui/src/types/onebot_conf.d.ts b/napcat.webui/src/types/onebot_conf.d.ts index 4db673f6..0453f334 100644 --- a/napcat.webui/src/types/onebot_conf.d.ts +++ b/napcat.webui/src/types/onebot_conf.d.ts @@ -44,9 +44,14 @@ interface WebsocketClientConfig extends AdapterConfig { heartInterval: number } +interface HttpSseServerConfig extends HttpServerConfig { + reportSelfMessage: boolean +} + interface NetworkConfig { httpServers: Array httpClients: Array + httpSseServers: Array websocketServers: Array websocketClients: Array }