mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-03-02 00:30:25 +00:00
refactor: 整体重构 (#1381)
* feat: pnpm new * Refactor build and release workflows, update dependencies Switch build scripts and workflows from npm to pnpm, update build and artifact paths, and simplify release workflow by removing version detection and changelog steps. Add new dependencies (silk-wasm, express, ws, node-pty-prebuilt-multiarch), update exports in package.json files, and add vite config for napcat-framework. Also, rename manifest.json for framework package and fix static asset copying in shell build config.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import type { Selection } from '@react-types/shared';
|
||||
import { useReactive } from 'ahooks';
|
||||
import { useCallback, useState } from 'react';
|
||||
import toast from 'react-hot-toast';
|
||||
import useWebSocket, { ReadyState } from 'react-use-websocket';
|
||||
|
||||
import { renderFilterMessageType } from '@/components/onebot/filter_message_type';
|
||||
|
||||
import { isOB11Event, isOB11RequestResponse } from '@/utils/onebot';
|
||||
|
||||
import type { AllOB11WsResponse } from '@/types/onebot';
|
||||
|
||||
export { ReadyState } from 'react-use-websocket';
|
||||
export function useWebSocketDebug (url: string, token: string) {
|
||||
const messageHistory = useReactive<AllOB11WsResponse[]>([]);
|
||||
const [filterTypes, setFilterTypes] = useState<Selection>('all');
|
||||
|
||||
const filteredMessages = messageHistory.filter((msg) => {
|
||||
if (filterTypes === 'all' || filterTypes.size === 0) return true;
|
||||
if (isOB11Event(msg)) return filterTypes.has(msg.post_type);
|
||||
if (isOB11RequestResponse(msg)) return filterTypes.has('request');
|
||||
return false;
|
||||
});
|
||||
|
||||
const { sendMessage, readyState } = useWebSocket(url, {
|
||||
onMessage: useCallback((event: WebSocketEventMap['message']) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
messageHistory.unshift(data);
|
||||
} catch (_error) {
|
||||
toast.error('WebSocket 消息解析失败');
|
||||
}
|
||||
}, []),
|
||||
queryParams: {
|
||||
access_token: token,
|
||||
},
|
||||
onError: (event) => {
|
||||
toast.error('WebSocket 连接失败');
|
||||
console.error('WebSocket error:', event);
|
||||
},
|
||||
onOpen: () => {
|
||||
messageHistory.splice(0, messageHistory.length);
|
||||
},
|
||||
});
|
||||
|
||||
const _sendMessage = (msg: string) => {
|
||||
if (readyState !== ReadyState.OPEN) {
|
||||
throw new Error('WebSocket 连接未建立');
|
||||
}
|
||||
sendMessage(msg);
|
||||
};
|
||||
|
||||
const FilterMessagesType = renderFilterMessageType(
|
||||
filterTypes,
|
||||
setFilterTypes
|
||||
);
|
||||
|
||||
return {
|
||||
sendMessage: _sendMessage,
|
||||
readyState,
|
||||
messageHistory,
|
||||
filteredMessages,
|
||||
filterTypes,
|
||||
setFilterTypes,
|
||||
FilterMessagesType,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user