From 367018bb133c41e6fd1dfea6243d260af60324c2 Mon Sep 17 00:00:00 2001 From: archy Date: Mon, 9 Oct 2023 06:57:25 +0800 Subject: [PATCH] req-auto-init on app start: patch a production-only bug for first-time users --- packages/insomnia/src/main.development.ts | 2 +- .../insomnia/src/models/workspace-meta.ts | 4 +- packages/insomnia/src/ui/renderApp.tsx | 42 +++++++++++++++---- packages/insomnia/src/ui/routes/request.tsx | 1 + 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/packages/insomnia/src/main.development.ts b/packages/insomnia/src/main.development.ts index 017d795fd..5b23f1b44 100644 --- a/packages/insomnia/src/main.development.ts +++ b/packages/insomnia/src/main.development.ts @@ -41,7 +41,7 @@ if (envDataPath) { } else { // Explicitly set userData folder from config because it's sketchy to rely on electron-builder to use productName, which could be changed by accident. const defaultPath = app.getPath('userData'); - const newPath = path.join(defaultPath, '../', isDevelopment() ? 'insomnium-dev-desu5' : userDataFolder); + const newPath = path.join(defaultPath, '../', isDevelopment() ? 'insomnium-dev-dez4' : userDataFolder); app.setPath('userData', newPath); } diff --git a/packages/insomnia/src/models/workspace-meta.ts b/packages/insomnia/src/models/workspace-meta.ts index 8f001ab50..7af58f111 100644 --- a/packages/insomnia/src/models/workspace-meta.ts +++ b/packages/insomnia/src/models/workspace-meta.ts @@ -89,9 +89,9 @@ export async function getByGitRepositoryId(gitRepositoryId: string) { return db.getWhere(type, { gitRepositoryId }); } -export async function getOrCreateByParentId(parentId: string) { +export async function getOrCreateByParentId(parentId: string, patch: any = {}) { const doc = await getByParentId(parentId); - return doc || create({ parentId }); + return doc || create({ ...patch, parentId },); } export function all() { diff --git a/packages/insomnia/src/ui/renderApp.tsx b/packages/insomnia/src/ui/renderApp.tsx index faf87eff2..1ee4e61b3 100644 --- a/packages/insomnia/src/ui/renderApp.tsx +++ b/packages/insomnia/src/ui/renderApp.tsx @@ -10,33 +10,57 @@ import { setupRouterStuff } from './router'; import { dummyStartingWorkspace, importPure } from '../common/import'; import { Workspace } from '../models/workspace'; import { BaseModel } from '../models'; +import { getProductName } from '../common/constants'; export async function renderApp() { const prevLocationHistoryEntry = localStorage.getItem('locationHistoryEntry'); - let beginningPath: string | null = null + let beginningPathForFirstTimeUser: string | null = null + let wId: string | null = null + let eId: string | null = null - // if (!prevLocationHistoryEntry) { + // initalize a new req manually on user's first run + if (!prevLocationHistoryEntry) { const workspaceNumber = await database.count(models.workspace.type) console.log("workspaces detected ~>", workspaceNumber); if (workspaceNumber === 0) { - const [d, wId, rId] = dummyStartingWorkspace() + const [d] = dummyStartingWorkspace() const newObj = await importPure(d) as { resources: { resources: models.BaseModel[] }[]; } const r = (newObj.resources?.[0]?.resources as BaseModel[]).find((a) => a.type === "Request") const w = (newObj.resources?.[0]?.resources as BaseModel[]).find((a) => a.type === "Workspace") - if (w && r) - beginningPath = `/organization/org_default-project/project/proj_default-project/workspace/${w._id}/debug/request/${r._id}` + const e = (newObj.resources?.[0]?.resources as BaseModel[]).find((a) => a.type === "Environment") + if (w && r && e) { + wId = w._id + beginningPathForFirstTimeUser = `/organization/org_default-project/project/proj_default-project/workspace/${w._id}/debug/request/${r._id}` + + const defaultProject = await models.project.getById('proj_default-project'); + + if (!defaultProject) + (await models.project.create({ + _id: 'proj_default-project', + name: getProductName(), + remoteId: null, + })) + + eId = e._id + console.log("META DESU->", w._id, beginningPathForFirstTimeUser); + const id = await models.workspaceMeta.getByParentId(w._id); + if (!id) { + const activeWorkspaceMeta = await models.workspaceMeta.getOrCreateByParentId(w._id, { + activeEnvironmentId: eId, + }); + } + } - // console.log("newPath", beginningPath) } - // } + } - const router = setupRouterStuff(beginningPath); + const router = setupRouterStuff(beginningPathForFirstTimeUser); await database.initClient(); @@ -51,7 +75,9 @@ export async function renderApp() { guard(root, 'Could not find root element'); + ReactDOM.createRoot(root).render( ); + } diff --git a/packages/insomnia/src/ui/routes/request.tsx b/packages/insomnia/src/ui/routes/request.tsx index cb0f0b4e6..096607b8b 100644 --- a/packages/insomnia/src/ui/routes/request.tsx +++ b/packages/insomnia/src/ui/routes/request.tsx @@ -60,6 +60,7 @@ export const loader: LoaderFunction = async ({ params }): Promise