mirror of
https://github.com/mountain-loop/yaak.git
synced 2025-12-23 22:48:55 -05:00
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import { useQuery } from '@tanstack/react-query';
|
|
import type { HttpRequest } from '@yaakapp-internal/models';
|
|
import type {
|
|
CallHttpRequestActionRequest,
|
|
GetHttpRequestActionsResponse,
|
|
HttpRequestAction,
|
|
} from '@yaakapp-internal/plugins';
|
|
import { useMemo } from 'react';
|
|
import { invokeCmd } from '../lib/tauri';
|
|
import { usePluginsKey } from './usePlugins';
|
|
|
|
export type CallableHttpRequestAction = Pick<HttpRequestAction, 'label' | 'icon'> & {
|
|
call: (httpRequest: HttpRequest) => Promise<void>;
|
|
};
|
|
|
|
export function useHttpRequestActions() {
|
|
const pluginsKey = usePluginsKey();
|
|
|
|
const actionsResult = useQuery<CallableHttpRequestAction[]>({
|
|
queryKey: ['http_request_actions', pluginsKey],
|
|
queryFn: () => getHttpRequestActions(),
|
|
});
|
|
|
|
// biome-ignore lint/correctness/useExhaustiveDependencies: none
|
|
const actions = useMemo(() => {
|
|
return actionsResult.data ?? [];
|
|
}, [JSON.stringify(actionsResult.data)]);
|
|
|
|
return actions;
|
|
}
|
|
|
|
export async function getHttpRequestActions() {
|
|
const responses = await invokeCmd<GetHttpRequestActionsResponse[]>('cmd_http_request_actions');
|
|
const actions = responses.flatMap((r) =>
|
|
r.actions.map((a, i) => ({
|
|
label: a.label,
|
|
icon: a.icon,
|
|
call: async (httpRequest: HttpRequest) => {
|
|
const payload: CallHttpRequestActionRequest = {
|
|
index: i,
|
|
pluginRefId: r.pluginRefId,
|
|
args: { httpRequest },
|
|
};
|
|
await invokeCmd('cmd_call_http_request_action', { req: payload });
|
|
},
|
|
})),
|
|
);
|
|
|
|
return actions;
|
|
}
|