mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-30 06:02:00 +02:00
Add test actions to copy-curl plugin and add WebSocket request actions to Sidebar
This commit is contained in:
52
src-web/hooks/useWebSocketRequestActions.ts
Normal file
52
src-web/hooks/useWebSocketRequestActions.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import type { WebsocketRequest } from '@yaakapp-internal/models';
|
||||
import type {
|
||||
CallWebSocketRequestActionRequest,
|
||||
GetWebSocketRequestActionsResponse,
|
||||
WebSocketRequestAction,
|
||||
} from '@yaakapp-internal/plugins';
|
||||
import { useMemo } from 'react';
|
||||
import { invokeCmd } from '../lib/tauri';
|
||||
import { usePluginsKey } from './usePlugins';
|
||||
|
||||
export type CallableWebSocketRequestAction = Pick<WebSocketRequestAction, 'label' | 'icon'> & {
|
||||
call: (request: WebsocketRequest) => Promise<void>;
|
||||
};
|
||||
|
||||
export function useWebSocketRequestActions() {
|
||||
const pluginsKey = usePluginsKey();
|
||||
|
||||
const actionsResult = useQuery<CallableWebSocketRequestAction[]>({
|
||||
queryKey: ['websocket_request_actions', pluginsKey],
|
||||
queryFn: () => getWebSocketRequestActions(),
|
||||
});
|
||||
|
||||
// biome-ignore lint/correctness/useExhaustiveDependencies: none
|
||||
const actions = useMemo(() => {
|
||||
return actionsResult.data ?? [];
|
||||
}, [JSON.stringify(actionsResult.data)]);
|
||||
|
||||
return actions;
|
||||
}
|
||||
|
||||
export async function getWebSocketRequestActions() {
|
||||
const responses = await invokeCmd<GetWebSocketRequestActionsResponse[]>(
|
||||
'cmd_websocket_request_actions',
|
||||
);
|
||||
const actions = responses.flatMap((r) =>
|
||||
r.actions.map((a, i) => ({
|
||||
label: a.label,
|
||||
icon: a.icon,
|
||||
call: async (websocketRequest: WebsocketRequest) => {
|
||||
const payload: CallWebSocketRequestActionRequest = {
|
||||
index: i,
|
||||
pluginRefId: r.pluginRefId,
|
||||
args: { websocketRequest },
|
||||
};
|
||||
await invokeCmd('cmd_call_websocket_request_action', { req: payload });
|
||||
},
|
||||
})),
|
||||
);
|
||||
|
||||
return actions;
|
||||
}
|
||||
Reference in New Issue
Block a user