mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-15 13:43:39 +01:00
45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import { useNavigate } from '@tanstack/react-router';
|
|
import type { HttpRequest } from '@yaakapp-internal/models';
|
|
import { trackEvent } from '../lib/analytics';
|
|
import { invokeCmd } from '../lib/tauri';
|
|
import { getActiveRequest } from './useActiveRequest';
|
|
import { getActiveWorkspaceId } from './useActiveWorkspace';
|
|
import { useFastMutation } from './useFastMutation';
|
|
|
|
export function useCreateHttpRequest() {
|
|
const navigate = useNavigate();
|
|
|
|
return useFastMutation<HttpRequest, unknown, Partial<HttpRequest>>({
|
|
mutationKey: ['create_http_request'],
|
|
mutationFn: async (patch = {}) => {
|
|
const workspaceId = getActiveWorkspaceId();
|
|
if (workspaceId == null) {
|
|
throw new Error("Cannot create request when there's no active workspace");
|
|
}
|
|
|
|
const activeRequest = getActiveRequest();
|
|
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<HttpRequest>('cmd_create_http_request', {
|
|
request: { workspaceId, ...patch },
|
|
});
|
|
},
|
|
onSettled: () => trackEvent('http_request', 'create'),
|
|
onSuccess: async (request) => {
|
|
await navigate({
|
|
to: '/workspaces/$workspaceId/requests/$requestId',
|
|
params: { workspaceId: request.workspaceId, requestId: request.id },
|
|
search: (prev) => ({ ...prev }),
|
|
});
|
|
},
|
|
});
|
|
}
|