Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
req-auto-init on app start: patch a production-only bug for first-tim…
Browse files Browse the repository at this point in the history
…e users
  • Loading branch information
archywillhe committed Oct 8, 2023
1 parent e9eea1b commit 367018b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/insomnia/src/main.development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/insomnia/src/models/workspace-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ export async function getByGitRepositoryId(gitRepositoryId: string) {
return db.getWhere<WorkspaceMeta>(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() {
Expand Down
42 changes: 34 additions & 8 deletions packages/insomnia/src/ui/renderApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Workspace>(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();

Expand All @@ -51,7 +75,9 @@ export async function renderApp() {

guard(root, 'Could not find root element');


ReactDOM.createRoot(root).render(
<RouterProvider router={router} />
);

}
1 change: 1 addition & 0 deletions packages/insomnia/src/ui/routes/request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const loader: LoaderFunction = async ({ params }): Promise<RequestLoaderD
throw redirect(`/organization/${organizationId}/project/${projectId}/workspace/${workspaceId}/debug`);
}
const activeWorkspaceMeta = await models.workspaceMeta.getByParentId(workspaceId);

guard(activeWorkspaceMeta, 'Active workspace meta not found');
// NOTE: loaders shouldnt mutate data, this should be moved somewhere else
await models.workspaceMeta.update(activeWorkspaceMeta, { activeRequestId: requestId });
Expand Down

0 comments on commit 367018b

Please sign in to comment.