mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-25 19:01:38 +01:00
32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import type { GrpcRequest, HttpRequest, WebsocketRequest } from "@yaakapp-internal/models";
|
|
import { createWorkspaceModel } from "@yaakapp-internal/models";
|
|
import { activeRequestAtom } from "../hooks/useActiveRequest";
|
|
import { jotaiStore } from "./jotai";
|
|
import { router } from "./router";
|
|
|
|
export async function createRequestAndNavigate<
|
|
T extends HttpRequest | GrpcRequest | WebsocketRequest,
|
|
>(patch: Partial<T> & Pick<T, "model" | "workspaceId">) {
|
|
const activeRequest = jotaiStore.get(activeRequestAtom);
|
|
|
|
if (patch.sortPriority === undefined) {
|
|
if (activeRequest != null) {
|
|
// Place below the currently active request
|
|
patch.sortPriority = activeRequest.sortPriority;
|
|
} else {
|
|
// Place at the very top
|
|
patch.sortPriority = -Date.now();
|
|
}
|
|
}
|
|
patch.folderId = patch.folderId || activeRequest?.folderId;
|
|
|
|
const newId = await createWorkspaceModel(patch);
|
|
|
|
await router.navigate({
|
|
to: "/workspaces/$workspaceId",
|
|
params: { workspaceId: patch.workspaceId },
|
|
search: (prev) => ({ ...prev, request_id: newId }),
|
|
});
|
|
return newId;
|
|
}
|