mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-02 11:29:02 -05:00
44 lines
1.6 KiB
TypeScript
44 lines
1.6 KiB
TypeScript
import { useMutation } from '@tanstack/react-query';
|
|
import { trackEvent } from '../lib/analytics';
|
|
import type { HttpRequest } from '../lib/models';
|
|
import { invokeCmd } from '../lib/tauri';
|
|
import { useActiveEnvironmentId } from './useActiveEnvironmentId';
|
|
import { useActiveRequest } from './useActiveRequest';
|
|
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
|
|
import { useAppRoutes } from './useAppRoutes';
|
|
|
|
export function useCreateHttpRequest() {
|
|
const workspaceId = useActiveWorkspaceId();
|
|
const activeEnvironmentId = useActiveEnvironmentId();
|
|
const activeRequest = useActiveRequest();
|
|
const routes = useAppRoutes();
|
|
|
|
return useMutation<HttpRequest, unknown, Partial<HttpRequest>>({
|
|
mutationKey: ['create_http_request'],
|
|
mutationFn: (patch = {}) => {
|
|
if (workspaceId === null) {
|
|
throw new Error("Cannot create request when there's no active workspace");
|
|
}
|
|
if (patch.sortPriority === undefined) {
|
|
if (activeRequest != null) {
|
|
// Place above currently-active request
|
|
patch.sortPriority = activeRequest.sortPriority + 0.0001;
|
|
} else {
|
|
// Place at the very top
|
|
patch.sortPriority = -Date.now();
|
|
}
|
|
}
|
|
patch.folderId = patch.folderId || activeRequest?.folderId;
|
|
return invokeCmd('cmd_create_http_request', { request: { workspaceId, ...patch } });
|
|
},
|
|
onSettled: () => trackEvent('http_request', 'create'),
|
|
onSuccess: async (request) => {
|
|
routes.navigate('request', {
|
|
workspaceId: request.workspaceId,
|
|
requestId: request.id,
|
|
environmentId: activeEnvironmentId ?? undefined,
|
|
});
|
|
},
|
|
});
|
|
}
|