Skip to content

Commit

Permalink
Merge branch 'staging' into feature/reopen-closed-tabs-999
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Nov 3, 2019
2 parents 5e347f9 + b8a9b29 commit 0f2beb4
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 68 deletions.
3 changes: 3 additions & 0 deletions packages/altair-electron/electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ fileAssociations:
- ext: agc
description: Altair GraphQL Collection
role: Viewer
- ext: agbkp
description: Altair GraphQL Backup
role: Viewer
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const ElectronStore = require('electron-store');
const { getPersistentStore } = require('../store');

// An interface between localStorage and electron-store
class ElectronStoreAdapter {
constructor() {
this.store = new ElectronStore();
this.store = getPersistentStore();
}

/**
Expand Down
123 changes: 64 additions & 59 deletions packages/altair-electron/src/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const { app, protocol } = require('electron');
const { app, protocol, dialog } = require('electron');
const { readFile } = require('fs');
const isDev = require('electron-is-dev');
const { setupAutoUpdates } = require('./updates');
const { createWindow, getInstance } = require('./window');
const { getStore } = require('./store');
const { checkMultipleDataVersions } = require('./utils/check-multi-data-versions');

// require('electron-debug')();
// try {
Expand All @@ -14,64 +13,70 @@ const { checkMultipleDataVersions } = require('./utils/check-multi-data-versions
// Default Squirrel.Windows event handler for your Electron apps.
// if (require('electron-squirrel-startup')) return; // Not required when using NSIS target

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win = getInstance();
const gotTheLock = app.requestSingleInstanceLock();

console.log(protocol);
// protocol.registerStandardSchemes(['altair'], { secure: true });
protocol.registerSchemesAsPrivileged([
{ scheme: 'altair', privileges: { standard: true, secure: true, corsEnabled: true, supportFetchAPI: true } }
]);

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', () => {
createWindow();
if (!isDev) {
setupAutoUpdates();
}
// checkMultipleDataVersions();
});

// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});

app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (getInstance() === null) {
if (!gotTheLock) {
console.log('An instance already exists.');
app.quit();
} else {
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win = getInstance();

console.log(protocol);
// protocol.registerStandardSchemes(['altair'], { secure: true });
protocol.registerSchemesAsPrivileged([
{ scheme: 'altair', privileges: { standard: true, secure: true, corsEnabled: true, supportFetchAPI: true } }
]);

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', () => {
createWindow();
}
});

app.on('will-finish-launching', function() {
app.on('open-file', function(ev, path) {
readFile(path, 'utf8', (err, data) => {
if (err) {
return;
}
const instance = getInstance();

if (instance) {
instance.webContents.send('file-opened', data);
}

getStore().set('file-opened', data);
if (!isDev) {
setupAutoUpdates();
}
});

// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});

app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (getInstance() === null) {
createWindow();
}
});

app.on('will-finish-launching', function() {
app.on('open-file', function(ev, path) {
readFile(path, 'utf8', (err, data) => {
if (err) {
return;
}
const instance = getInstance();

if (instance) {
instance.webContents.send('file-opened', data);
}

getStore().set('file-opened', data);
});
});
});
});

app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
event.preventDefault();
callback(true);
// Inform user of invalid certificate
webContents.send('certificate-error', error);
});
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
event.preventDefault();
callback(true);
// Inform user of invalid certificate
webContents.send('certificate-error', error);
});
}
15 changes: 14 additions & 1 deletion packages/altair-electron/src/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,20 @@ const createMenu = (actions) => {
{ role: "minimize" },
{ role: "close" }
]
}
},
{
label: "Data",
submenu: [
{
label: 'Export data...',
click: actions.exportAppData,
},
{
label: 'Restore data...',
click: actions.importAppData,
},
]
},
];

if (process.platform === "darwin") {
Expand Down
12 changes: 9 additions & 3 deletions packages/altair-electron/src/store.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Store {
const ElectronStore = require('electron-store');

class InMemoryStore {
constructor() {
this.data = {};
}
Expand All @@ -15,10 +17,14 @@ class Store {
}
}

const store = new Store();
const store = new InMemoryStore();

const getStore = () => store;

const persistentStore = new ElectronStore();
const getPersistentStore = () => persistentStore;

module.exports = {
getStore
getStore,
getPersistentStore,
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const checkMultipleDataVersions = (win) => {
cancelId: 2,
title: 'Multiple versions found',
message: 'We found multiple versions of your Altair data',
detail: `Due to some misconfiguration that happened in some of the recent versions of Altair, there are several versions of your data. We want to help you keep the right data.\n\nTo do that, check if the current data is correct, and we will clear out the rest.`
detail: `Due to some misconfiguration that happened in some of the recent versions of Altair, there are several versions of your data. We want to help you keep the right data.\n\nTo do that, check if the current data is correct, and we will clear out the rest.\n\nTip: You can click the 'Remind me later' and we'll ask again when you restart.`
}, (response) => {
switch(response) {
case 0: // Correct version
Expand Down
43 changes: 41 additions & 2 deletions packages/altair-electron/src/window.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { BrowserWindow, protocol, ipcMain, session, app } = require('electron');
const { BrowserWindow, protocol, ipcMain, session, app, dialog } = require('electron');
const path = require('path');
const url = require('url');
const fs = require('fs');
Expand All @@ -7,7 +7,7 @@ const windowStateKeeper = require('electron-window-state');

const { getDistDirectory } = require('altair-static');

const { getStore } = require('./store');
const { getStore, getPersistentStore } = require('./store');
const { createMenu } = require('./menu');
const { createTouchBar } = require('./touchbar');
const { checkForUpdates } = require('./updates');
Expand Down Expand Up @@ -42,6 +42,45 @@ const actions = {
showSettings: () => {
instance.webContents.send('show-settings', true);
},
importAppData: () => {
dialog.showOpenDialog(instance, {
title: 'Import application data',
message: 'Only import a valid Altair config file',
properties: [ 'openFile' ],
filters: [
{ name: 'Altair GraphQL Backup Files', extensions: [ 'agbkp' ] },
],
}, ([ filePath ]) => {
if (filePath) {
const fileContent = fs.readFileSync(filePath, 'utf8');
try {
const fileObj = JSON.parse(fileContent);
// Check for the presence of some basic altair data to try to validate the file
if (fileObj.altair__debug_current_version) {
fs.copyFileSync(filePath, getPersistentStore().path);
app.relaunch();
app.exit(0);
} else {
throw new Error('Invalid file content.');
}
} catch (error) {
dialog.showErrorBox('Invalid file', 'The selected file is either invalid or corrupted. Please check the file and try again.');
}
}
});
},
exportAppData: () => {
dialog.showSaveDialog(instance, {
title: 'Backup application data',
defaultPath: 'altair_backup.agbkp',
filters: [
{ name: 'Altair GraphQL Backup Files', extensions: [ 'agbkp' ] },
],
}, (saveFilePath) => {
// Save to file
fs.copyFileSync(getPersistentStore().path, saveFilePath);
});
},
checkForUpdates,
};

Expand Down

0 comments on commit 0f2beb4

Please sign in to comment.