import { type BatchUpsertResult, type ImportDestination, type ImportPlan, type ImportSource, workspacesAtom, } from "@yaakapp-internal/models"; import { FormattedError, VStack } from "@yaakapp-internal/ui"; import { Button } from "../components/core/Button"; import { ImportDataDialog } from "../components/ImportDataDialog"; import { activeFolderAtom } from "../hooks/useActiveFolder"; import { activeWorkspaceAtom } from "../hooks/useActiveWorkspace"; import { createFastMutation } from "../hooks/useFastMutation"; import { showAlert } from "./alert"; import { showDialog } from "./dialog"; import { jotaiStore } from "./jotai"; import { pluralizeCount } from "./pluralize"; import { router } from "./router"; import { rpc } from "./rpc"; // Stable identities so the dialog's effects don't re-run (and cancel in-flight // fetches) every time the dialog container re-renders. const planFile = (filePath: string, destination: ImportDestination) => rpc("cmd_import_data", { filePath, destination }); const planUrl = (url: string, destination: ImportDestination) => rpc("cmd_import_url", { url, destination }); const listSources = (workspaceId: string) => rpc("cmd_list_import_sources", { workspaceId }); const findSourcesForOrigin = (args: { filePath?: string; url?: string }) => rpc("cmd_import_sources_for_origin", args); export const importData = createFastMutation({ mutationKey: ["import_data"], onError: (err: string) => { showAlert({ id: "import-failed", title: "Import Failed", size: "md", body: {err}, }); }, mutationFn: async () => { return new Promise((resolve, reject) => { const currentWorkspace = jotaiStore.get(activeWorkspaceAtom); const workspaces = jotaiStore.get(workspacesAtom); const selectedFolder = jotaiStore.get(activeFolderAtom); showDialog({ id: "import", title: "Import Data", size: "lg", disableClose: true, render: ({ hide }) => { const cancel = () => { hide(); resolve(); }; const fail = (err: unknown) => { hide(); reject(err); }; const commit = async (plan: ImportPlan) => { const imported = await rpc("cmd_commit_import", { plan }); hide(); await finishImport(imported); resolve(); }; return ( ); }, }); }); }, }); async function finishImport(imported: BatchUpsertResult): Promise { const importedWorkspace = imported.workspaces[0]; showDialog({ id: "import-complete", title: "Import Complete", size: "sm", hideX: true, render: ({ hide }) => { return (
    {imported.workspaces.length > 0 && (
  • {pluralizeCount("Workspace", imported.workspaces.length)}
  • )} {imported.environments.length > 0 && (
  • {pluralizeCount("Environment", imported.environments.length)}
  • )} {imported.folders.length > 0 && (
  • {pluralizeCount("Folder", imported.folders.length)}
  • )} {imported.httpRequests.length > 0 && (
  • {pluralizeCount("HTTP Request", imported.httpRequests.length)}
  • )} {imported.grpcRequests.length > 0 && (
  • {pluralizeCount("GRPC Request", imported.grpcRequests.length)}
  • )} {imported.websocketRequests.length > 0 && (
  • {pluralizeCount("Websocket Request", imported.websocketRequests.length)}
  • )}
); }, }); if (importedWorkspace != null) { const environmentId = imported.environments[0]?.id ?? null; await router.navigate({ to: "/workspaces/$workspaceId", params: { workspaceId: importedWorkspace.id }, search: { environment_id: environmentId }, }); } }