From 35c19ed305a90b1584239101d75ce33134e6199a Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Fri, 28 Oct 2016 10:51:08 -0700 Subject: [PATCH] Response download and fix GA --- app/backend/ganalytics.js | 17 ++++---- app/backend/sync/storage.js | 6 +-- app/package.json | 1 + app/ui/components/ResponsePane.js | 42 +++++++++++++++++++ .../dropdowns/PreviewModeDropdown.js | 8 +++- package.json | 1 + 6 files changed, 61 insertions(+), 14 deletions(-) diff --git a/app/backend/ganalytics.js b/app/backend/ganalytics.js index 00a01d4522..99c006833a 100644 --- a/app/backend/ganalytics.js +++ b/app/backend/ganalytics.js @@ -1,7 +1,6 @@ import * as constants from './constants'; import {isDevelopment} from './appInfo'; -let _ga = null; let _sessionId = null; export function initAnalytics () { @@ -11,7 +10,7 @@ export function initAnalytics () { } if (!_sessionId) { - _ga = _initGA(); + _injectGoogleAnalyticsScript(); } if (!localStorage['gaClientId']) { @@ -20,25 +19,25 @@ export function initAnalytics () { const _sessionId = window.localStorage['gaClientId']; - _ga('create', constants.GA_ID, {'storage': 'none', 'clientId': _sessionId}); + window.ga('create', constants.GA_ID, {'storage': 'none', 'clientId': _sessionId}); // Disable URL protocol check - _ga('set', 'checkProtocolTask', () => null); + window.ga('set', 'checkProtocolTask', () => null); // Set a fake location - _ga('set', 'location', `https://${constants.GA_HOST}/`); + window.ga('set', 'location', `https://${constants.GA_HOST}/`); // Track the initial page view - _ga('send', 'pageview'); + window.ga('send', 'pageview'); console.log(`-- Analytics Initialized for ${_sessionId} --`); } export function trackEvent (category, action, label) { - _ga && _ga('send', 'event', category, action, label); + window.ga && window.ga('send', 'event', category, action, label); } -function _initGA () { +function _injectGoogleAnalyticsScript () { (function (i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () { @@ -50,6 +49,4 @@ function _initGA () { a.src = g; m.parentNode.insertBefore(a, m) })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); - - return window.ga; } diff --git a/app/backend/sync/storage.js b/app/backend/sync/storage.js index 3749f43c7c..9c13405a99 100644 --- a/app/backend/sync/storage.js +++ b/app/backend/sync/storage.js @@ -81,9 +81,9 @@ export function allConfigs () { return findConfigs({}) } -export function findInactiveConfigs (resourceGroupId = null) { - if (resourceGroupId) { - return findConfigs({syncMode: SYNC_MODE_OFF, $not: {resourceGroupId}}) +export function findInactiveConfigs (excludedResourceGroupId = null) { + if (excludedResourceGroupId) { + return findConfigs({syncMode: SYNC_MODE_OFF, $not: {excludedResourceGroupId}}) } else { return findConfigs({syncMode: SYNC_MODE_OFF}) } diff --git a/app/package.json b/app/package.json index 2fca3163c9..19e411f058 100644 --- a/app/package.json +++ b/app/package.json @@ -17,6 +17,7 @@ "httpsnippet": "git@github.com:getinsomnia/httpsnippet.git#a3a2c0a0167fa844bf92df52a1442fa1d68a9053", "json-lint": "^0.1.0", "jsonpath-plus": "^0.15.0", + "mime-types": "^2.1.12", "mkdirp": "^0.5.1", "nedb": "^1.8.0", "node-forge": "^0.6.43", diff --git a/app/ui/components/ResponsePane.js b/app/ui/components/ResponsePane.js index 0f2e961045..0c0c1f8181 100644 --- a/app/ui/components/ResponsePane.js +++ b/app/ui/components/ResponsePane.js @@ -1,4 +1,7 @@ import React, {PropTypes, Component} from 'react'; +import fs from 'fs'; +import mime from 'mime-types'; +import {remote} from 'electron'; import {Tab, Tabs, TabList, TabPanel} from 'react-tabs'; import ElmComponent from './ElmComponent'; import {ResponsePaneHeader} from './ResponsePaneHeader.elm'; @@ -18,6 +21,7 @@ import { } from '../../backend/constants'; import {getSetCookieHeaders} from '../../backend/util'; import {cancelCurrentRequest} from '../../backend/network'; +import {trackEvent} from '../../backend/ganalytics'; class ResponsePane extends Component { constructor (props) { @@ -37,6 +41,43 @@ class ResponsePane extends Component { } } + async _handleDownloadResponseBody () { + if (!this.state.response) { + // Should never happen + console.warn('No response to download'); + return; + } + + const {body, encoding, contentType} = this.state.response; + const bodyBuffer = new Buffer(body, encoding); + const extension = mime.extension(contentType) || ''; + + const options = { + title: 'Save Response', + buttonLabel: 'Save', + filters: [{ + name: 'Download', extensions: [extension], + }] + }; + + remote.dialog.showSaveDialog(options, filename => { + if (!filename) { + trackEvent('Save Response', 'Cancel'); + return; + } + + fs.writeFile(filename, bodyBuffer, {}, err => { + if (err) { + console.warn('Failed to save response body', err); + trackEvent('Save Response', 'Failure'); + } else { + trackEvent('Save Response', 'Success'); + + } + }); + }); + } + componentWillReceiveProps (nextProps) { this._getResponse(nextProps.request); } @@ -171,6 +212,7 @@ class ResponsePane extends Component { {getPreviewModeName(previewMode)} diff --git a/app/ui/components/dropdowns/PreviewModeDropdown.js b/app/ui/components/dropdowns/PreviewModeDropdown.js index d6b1ab7eff..59f7606f70 100644 --- a/app/ui/components/dropdowns/PreviewModeDropdown.js +++ b/app/ui/components/dropdowns/PreviewModeDropdown.js @@ -1,8 +1,9 @@ import React, {PropTypes} from 'react'; import Dropdown from '../base/Dropdown'; +import DropdownDivider from '../base/DropdownDivider'; import {PREVIEW_MODES, getPreviewModeName} from '../../../backend/previewModes'; -const PreviewModeDropdown = ({updatePreviewMode}) => ( +const PreviewModeDropdown = ({updatePreviewMode, download}) => ( ))} + +
  • + +
  • ); @@ -22,6 +27,7 @@ const PreviewModeDropdown = ({updatePreviewMode}) => ( PreviewModeDropdown.propTypes = { // Functions updatePreviewMode: PropTypes.func.isRequired, + download: PropTypes.func.isRequired, // Required previewMode: PropTypes.string.isRequired diff --git a/package.json b/package.json index 928f1b95a4..7ec6542a09 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "httpsnippet": "git@github.com:getinsomnia/httpsnippet.git#a3a2c0a0167fa844bf92df52a1442fa1d68a9053", "json-lint": "^0.1.0", "jsonpath-plus": "^0.15.0", + "mime-types": "^2.1.12", "mkdirp": "^0.5.1", "mousetrap": "^1.6.0", "nedb": "^1.8.0",