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

Commit

Permalink
on app start-up: if workspace is empty, auto-create a new request & r…
Browse files Browse the repository at this point in the history
…edirect to it
  • Loading branch information
archywillhe committed Oct 8, 2023
1 parent 8f1d3fc commit 9d63bfe
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
24 changes: 14 additions & 10 deletions packages/insomnia/src/common/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,19 @@ export const isApiSpecImport = ({ id }: Pick<InsomniaImporter, 'id'>) =>
export const dummyStartingWorkspace = () => {
const currentUnixTime = Date.now();
const oneSec = 1000;
return ({
const wId = generateId(models.workspace.prefix)
const rId = generateId(models.request.prefix)
const eId = generateId(models.environment.prefix)
const jId = generateId(models.cookieJar.prefix)
return [({
"_type": "export",
"__export_format": 4,
"__export_date": "2023-10-04T20:25:03.318Z",
"__export_source": "insomnia.desktop.app:v0.1.3",
"resources": [
{
"_id": "req_27805db4efde4432b27c5961a44e41b7",
"parentId": "wrk_7f7845eb4c2043c2b9f327cbe81714b7",
"_id": rId,
"parentId": wId,
"modified": currentUnixTime - oneSec * 3,
"created": currentUnixTime - oneSec * 3,
"url": "",
Expand All @@ -289,7 +293,7 @@ export const dummyStartingWorkspace = () => {
"type": "Request"
},
{
"_id": "wrk_7f7845eb4c2043c2b9f327cbe81714b7",
"_id": wId,
"parentId": null,
"modified": currentUnixTime - oneSec * 2,
"created": currentUnixTime - oneSec * 2,
Expand All @@ -298,11 +302,11 @@ export const dummyStartingWorkspace = () => {
"scope": "collection",
// "_type": "workspace",
"type": "Workspace",
// fixed wrong import field bug from Insomnia?
// hmm where is the fn for import field transfromation from Insomnia
},
{
"_id": "env_7b68f82b6819ad3e7ff7db3f463fbeaaf47b816c",
"parentId": "wrk_7f7845eb4c2043c2b9f327cbe81714b7",
"_id": eId,
"parentId": wId,
"modified": currentUnixTime - oneSec * 1,
"created": currentUnixTime - oneSec * 1,
"name": "Base Environment",
Expand All @@ -314,14 +318,14 @@ export const dummyStartingWorkspace = () => {
"type": "Environment"
},
{
"_id": "jar_7b68f82b6819ad3e7ff7db3f463fbeaaf47b816c",
"parentId": "wrk_7f7845eb4c2043c2b9f327cbe81714b7",
"_id": jId,
"parentId": wId,
"modified": currentUnixTime,
"created": currentUnixTime,
"name": "Default Jar",
"cookies": [],
"type": "CookieJar"
}
]
})
}), wId, rId]
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const importResourcesToNewWorkspace = async (projectId: string, _resource
scope: 'design',
parentId: projectId,
});
models.apiSpec.updateOrCreateForParentId(newWorkspace._id, {
await models.apiSpec.updateOrCreateForParentId(newWorkspace._id, {
contents: ResourceCache.content,
contentType: 'yaml',
fileName: workspaceToImport?.name,
Expand Down
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-desu3' : userDataFolder);
const newPath = path.join(defaultPath, '../', isDevelopment() ? 'insomnium-dev-desu5' : userDataFolder);
app.setPath('userData', newPath);
}

Expand Down
23 changes: 21 additions & 2 deletions packages/insomnia/src/ui/renderApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,34 @@ import { guard } from '../utils/guard';
import { setupRouterStuff } from './router';
import { dummyStartingWorkspace, importPure } from '../common/import';
import { Workspace } from '../models/workspace';
import { BaseModel } from '../models';

export async function renderApp() {

const prevLocationHistoryEntry = localStorage.getItem('locationHistoryEntry');
let beginningPath: string | null = null

// if (!prevLocationHistoryEntry) {
const workspaceNumber = await database.count<Workspace>(models.workspace.type)
console.log("workspaces detected ~>", workspaceNumber);
if (workspaceNumber === 0) await importPure(dummyStartingWorkspace())

if (workspaceNumber === 0) {
const [d, wId, rId] = 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}`

// console.log("newPath", beginningPath)
}

// }

const router = setupRouterStuff();
const router = setupRouterStuff(beginningPath);

await database.initClient();

Expand Down
5 changes: 3 additions & 2 deletions packages/insomnia/src/ui/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ if (prevLocationHistoryEntry && matchPath({ path: '/organization/:organizationId
locationHistoryEntry = prevLocationHistoryEntry;
}

export const setupRouterStuff = () => {
export const setupRouterStuff = (beginningPath: string | null = null) => {

const router = createMemoryRouter(
// @TODO - Investigate file based routing to generate these routes:
Expand Down Expand Up @@ -792,7 +792,7 @@ export const setupRouterStuff = () => {
},
],
{
initialEntries: [locationHistoryEntry],
initialEntries: [beginningPath || locationHistoryEntry],
},
);

Expand All @@ -811,6 +811,7 @@ export const setupRouterStuff = () => {
}
localStorage.setItem('locationHistoryEntry', location.pathname);
match?.params.organizationId && localStorage.setItem(`locationHistoryEntry:${match?.params.organizationId}`, location.pathname);
console.log("location.pathname", location.pathname)
})

return router
Expand Down

0 comments on commit 9d63bfe

Please sign in to comment.