Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Commit

Permalink
serialize (trimmed) appState before opening Give feedback window
Browse files Browse the repository at this point in the history
  • Loading branch information
ericpyle committed Dec 5, 2019
1 parent db67708 commit 9d785cb
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 22 deletions.
47 changes: 27 additions & 20 deletions app/components/SubmitHelpTicket.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ class SubmitHelpTicket extends React.Component<Props> {
super(props);
this.state = {
title: '',
description: ''
description: '',
isLoadingAppStateFile: true
};
// initial a parser;
this.mdParser = new MarkdownIt({
Expand Down Expand Up @@ -308,10 +309,9 @@ class SubmitHelpTicket extends React.Component<Props> {
async componentWillMount() {
ipcRenderer.on(
ipcRendererConstants.KEY_IPC_ATTACH_APP_STATE_SNAPSHOT,
async (event, appState) => {
async (event, appStateFilePath) => {
try {
await this.refreshMarkdownDescription(appState);
this.setState({ appState });
await this.refreshMarkdownDescription(appStateFilePath);
} catch (error) {
log.error(error);
}
Expand All @@ -331,9 +331,9 @@ class SubmitHelpTicket extends React.Component<Props> {
await this.removeTempFiles();
}

async refreshMarkdownDescription(appState) {
async refreshMarkdownDescription(appStateFilePath) {
const { dispatchLoginSuccess } = this.props;
const appStateFilePath = await createAppStateAttachment(appState);
const appState = await deserializeAppState(appStateFilePath);
const {
authentication,
router,
Expand Down Expand Up @@ -382,7 +382,13 @@ class SubmitHelpTicket extends React.Component<Props> {
...bundleAttachments
]
});
this.setState({ description, appStateFilePath, activeBundleFilePath });
this.setState({
description,
appStateFilePath,
appState,
activeBundleFilePath,
isLoadingAppStateFile: false
});
}

async removeTempFiles() {
Expand All @@ -404,6 +410,10 @@ class SubmitHelpTicket extends React.Component<Props> {
};

handleEditorChange = ({ text }) => {
const { isLoadingAppStateFile } = this.state;
if (isLoadingAppStateFile) {
return;
}
this.setState({
description: text.replace(linkPattern, replaceEmptyAltTextWithFileName)
});
Expand Down Expand Up @@ -533,8 +543,10 @@ class SubmitHelpTicket extends React.Component<Props> {
title,
description,
isUploading,
isLoadingAppStateFile,
feedbackType = 'BugReport'
} = this.state;
const isLoading = isLoadingAppStateFile || isUploading;
const { classes } = this.props;
return (
<React.Fragment>
Expand All @@ -545,11 +557,11 @@ class SubmitHelpTicket extends React.Component<Props> {
color="inherit"
classes={classes}
onClick={this.handleClickSendFeedback}
disabled={title.trim().length < 3 || isUploading}
disabled={title.trim().length < 3 || isLoading}
>
<CloudUpload style={{ marginRight: '10px' }} />
Send {splitCamelCaseToSpaced(feedbackType)}
{isUploading && (
{isLoading && (
<CircularProgress
className={classes.buttonProgress}
size={50}
Expand All @@ -569,7 +581,7 @@ class SubmitHelpTicket extends React.Component<Props> {
}}
>
<nav className="nav">
<FormControl fullWidth>
<FormControl fullWidth disabled={isLoading}>
<InputLabel color="secondary">Type of Feedback</InputLabel>
<Select
value={feedbackType}
Expand All @@ -589,6 +601,7 @@ class SubmitHelpTicket extends React.Component<Props> {
variant="outlined"
placeholder="Provide a general summary of the issue"
onChange={this.handleTitleInputChange}
disabled={isLoading}
/>
{/*
<button onClick={this.handleGetMdValue} >getMdValue</button>
Expand All @@ -600,6 +613,7 @@ class SubmitHelpTicket extends React.Component<Props> {
ref={node => {
this.mdEditor = node;
}}
disabled={isLoading}
value={description}
style={{ height: '500px', width: '100%' }}
renderHTML={text => this.mdParser.render(text)}
Expand Down Expand Up @@ -681,16 +695,9 @@ function getWorkspaceAttachments(workspaceFullPath) {
return workspaceAttachments;
}

async function createAppStateAttachment(appState) {
const app = servicesHelpers.getApp();
const tempPath = app.getPath('temp');
const uuid1 = uuidv1();
const uid = uuid1.substr(0, 5);
const appStateFilePath = utilities.normalizeLinkPath(
path.join(tempPath, `appState-${uid}.json`)
);
await fs.writeFile(appStateFilePath, JSON.stringify(appState, null, 4));
return appStateFilePath;
async function deserializeAppState(appStateFilePath) {
const fileContents = await fs.readFile(appStateFilePath, 'utf8');
return JSON.parse(fileContents);
}

async function createActiveBundleAttachment(activeBundle) {
Expand Down
52 changes: 50 additions & 2 deletions app/containers/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @flow
import fs from 'fs-extra';
import uuidv1 from 'uuid/v1';
import * as React from 'react';
import { connect } from 'react-redux';
import path from 'path';
Expand All @@ -9,6 +11,7 @@ import { logHelpers } from '../helpers/log.helpers';
import { browserWindowService } from '../services/browserWindow.service';
import { navigationConstants } from '../constants/navigation.constants';
import { servicesHelpers } from '../helpers/services';
import { utilities } from '../utils/utilities';

const { ipcRenderer } = require('electron');

Expand Down Expand Up @@ -53,11 +56,12 @@ class App extends React.Component<Props> {
navigationConstants.NAVIGATION_SUBMIT_HELP_TICKET
}`
);
feedbackWindow.webContents.once('dom-ready', () => {
feedbackWindow.webContents.once('dom-ready', async () => {
const { appState } = this.props;
const appStateFilePath = await createAppStateAttachment(appState);
feedbackWindow.webContents.send(
ipcRendererConstants.KEY_IPC_ATTACH_APP_STATE_SNAPSHOT,
appState
appStateFilePath
);
});
});
Expand All @@ -70,3 +74,47 @@ class App extends React.Component<Props> {
}

export default connect(mapStateToProps)(App);

async function createAppStateAttachment(appState) {
const app = servicesHelpers.getApp();
const tempPath = app.getPath('temp');
const uuid1 = uuidv1();
const uid = uuid1.substr(0, 5);
const appStateFilePath = utilities.normalizeLinkPath(
path.join(tempPath, `appState-${uid}.json`)
);
/*
items,
allBundles,
addedByBundleIds
*/
const { navigation, bundles, ...restAppState } = appState;
const { items, allBundles, addedByBundleIds, ...restBundleData } = bundles;
const MAX_ITEMS = 20;
const trimmedItems =
items.length <= MAX_ITEMS ? items : items.slice(0, MAX_ITEMS);
const trimmedAllBundles =
items.length <= MAX_ITEMS ? allBundles : allBundles.slice(0, MAX_ITEMS);
const lastNavigation = navigation[navigation.length - 1];
const { bundle: lastBundleIdVisited = {} } = lastNavigation || {};
const trimmedAddedByBundleIds =
items.length <= MAX_ITEMS
? addedByBundleIds
: { [lastBundleIdVisited]: addedByBundleIds[lastBundleIdVisited] };
const trimmedBundles = {
...restBundleData,
items: trimmedItems,
allBundles: trimmedAllBundles,
addedByBundleIds: trimmedAddedByBundleIds
};
const trimmedAppState = {
navigation,
...restAppState,
bundles: trimmedBundles
};
await fs.writeFile(
appStateFilePath,
JSON.stringify(trimmedAppState, null, 1)
);
return appStateFilePath;
}

0 comments on commit 9d785cb

Please sign in to comment.