Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/second-window #6104

Merged
merged 10 commits into from Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 0 additions & 14 deletions packages/insomnia-send-request/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 19 additions & 29 deletions packages/insomnia/src/account/session.ts
Expand Up @@ -37,22 +37,12 @@ export interface SessionData {
publicKey: JsonWebKey;
encPrivateKey: crypt.AESMessage;
}

const loginCallbacks: LoginCallback[] = [];

function _callCallbacks() {
const loggedIn = isLoggedIn();
console.log('[session] Sync state changed loggedIn=' + loggedIn);

for (const cb of loginCallbacks) {
if (typeof cb === 'function') {
cb(loggedIn);
}
}
}

export function onLoginLogout(loginCallback: LoginCallback) {
loginCallbacks.push(loginCallback);
window.main.on('loggedIn', () => {
console.log('onLoginLogout', isLoggedIn());

loginCallback(isLoggedIn());
});
}

/** Creates a session from a sessionId and derived symmetric key. */
Expand Down Expand Up @@ -80,7 +70,7 @@ export async function absorbKey(sessionId: string, key: string) {
JSON.parse(encPrivateKey),
);

_callCallbacks();
window.main.loginStateChange();
}

export async function changePasswordWithToken(rawNewPassphrase: string, confirmationCode: string) {
Expand Down Expand Up @@ -193,13 +183,12 @@ export async function logout() {
}

_unsetSessionData();

_callCallbacks();
window.main.loginStateChange();
}

/** Set data for the new session and store it encrypted with the sessionId */
export function setSessionData(
sessionId: string,
id: string,
accountId: string,
firstName: string,
lastName: string,
Expand All @@ -209,19 +198,20 @@ export function setSessionData(
encPrivateKey: crypt.AESMessage,
) {
const sessionData: SessionData = {
id: sessionId,
accountId: accountId,
symmetricKey: symmetricKey,
publicKey: publicKey,
encPrivateKey: encPrivateKey,
email: email,
firstName: firstName,
lastName: lastName,
id,
accountId,
symmetricKey,
publicKey,
encPrivateKey,
email,
firstName,
lastName,
};
const dataStr = JSON.stringify(sessionData);
window.localStorage.setItem(_getSessionKey(sessionId), dataStr);
window.localStorage.setItem(_getSessionKey(id), dataStr);
// NOTE: We're setting this last because the stuff above might fail
window.localStorage.setItem('currentSessionId', sessionId);
window.localStorage.setItem('currentSessionId', id);
return sessionData;
}
export async function listTeams() {
return fetch.get('/api/teams', getCurrentSessionId());
Expand Down
15 changes: 13 additions & 2 deletions packages/insomnia/src/main.development.ts
Expand Up @@ -149,6 +149,7 @@ const _launchApp = async () => {
console.log('[main] Window ready, handling command line arguments', process.argv);
const args = process.argv.slice(1).filter(a => a !== '.');
if (args.length) {
window = windowUtils.getOrCreateWindow();
window.webContents.send('shell:open', args.join());
}
});
Expand All @@ -163,6 +164,7 @@ const _launchApp = async () => {
// Called when second instance launched with args (Windows/Linux)
app.on('second-instance', (_1, args) => {
console.log('Second instance listener received:', args.join('||'));
window = windowUtils.getOrCreateWindow();
if (window) {
if (window.isMinimized()) {
window.restore();
Expand All @@ -173,15 +175,24 @@ const _launchApp = async () => {
console.log('[main] Open Deep Link URL sent from second instance', lastArg);
window.webContents.send('shell:open', lastArg);
});
window = windowUtils.createWindow();
window = windowUtils.getOrCreateWindow();

app.on('open-url', (_event, url) => {
console.log('[main] Open Deep Link URL', url);
window = windowUtils.getOrCreateWindow();
if (window) {
if (window.isMinimized()) {
window.restore();
}
window.focus();
} else {
window = windowUtils.getOrCreateWindow();
}
window.webContents.send('shell:open', url);
});
}
} else {
window = windowUtils.createWindow();
window = windowUtils.getOrCreateWindow();
}

// Don't send origin header from Insomnia because we're not technically using CORS
Expand Down
10 changes: 9 additions & 1 deletion packages/insomnia/src/main/ipc/main.ts
Expand Up @@ -4,7 +4,7 @@ import { Spectral } from '@stoplight/spectral-core';
// @ts-expect-error - This is a bundled file not sure why it's not found
import { bundleAndLoadRuleset } from '@stoplight/spectral-ruleset-bundler/with-loader';
import { oas } from '@stoplight/spectral-rulesets';
import { app, ipcMain, IpcRendererEvent } from 'electron';
import { app, BrowserWindow, ipcMain, IpcRendererEvent } from 'electron';
import fs from 'fs';

import { axiosRequest } from '../../network/axios-request';
Expand All @@ -16,6 +16,7 @@ import { WebSocketBridgeAPI } from '../network/websocket';
import { gRPCBridgeAPI } from './grpc';

export interface MainBridgeAPI {
loginStateChange: () => void;
restart: () => void;
halfSecondAfterAppStart: () => void;
manualUpdateCheck: () => void;
Expand All @@ -32,6 +33,13 @@ export interface MainBridgeAPI {
grpc: gRPCBridgeAPI;
}
export function registerMainHandlers() {
ipcMain.on('loginStateChange', async () => {
console.log('get windows');
BrowserWindow.getAllWindows().forEach(w => {
console.log('sending login state change to window');
w.webContents.send('loggedIn');
});
});
ipcMain.handle('exportAllWorkspaces', async () => {
return exportAllWorkspaces();
});
Expand Down