Skip to content

Commit

Permalink
fix: parse and validate entries from note text (#78)
Browse files Browse the repository at this point in the history
* feat: CopyNotification component

* fix: parse and validate entries from note text

* chore: build

* chore: increment version

Co-authored-by: Johnny Almonte <johnny243@users.noreply.github.com>
  • Loading branch information
johnny243 and johnny243 authored Feb 15, 2022
1 parent 74eaf56 commit 6d33d0b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
18 changes: 18 additions & 0 deletions app/components/CopyNotification.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import PropTypes from 'prop-types';

const CopyNotification = ({ isVisible }) => (
<div
className={`auth-copy-notification ${isVisible ? 'visible' : 'hidden'}`}
>
<div className="sk-panel">
<div className="sk-font-small sk-bold">Copied value to clipboard.</div>
</div>
</div>
);

CopyNotification.propTypes = {
isVisible: PropTypes.bool.isRequired,
};

export default CopyNotification;
46 changes: 36 additions & 10 deletions app/components/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ConfirmDialog from '@Components/ConfirmDialog';
import DataErrorAlert from '@Components/DataErrorAlert';
import EditorKit from '@standardnotes/editor-kit';
import ReorderIcon from '../assets/svg/reorder-icon.svg';
import CopyNotification from './CopyNotification';

const initialState = {
text: '',
Expand Down Expand Up @@ -36,7 +37,7 @@ export default class Home extends React.Component {

if (text) {
try {
entries = JSON.parse(text);
entries = this.parseNote(text);
} catch (e) {
// Couldn't parse the content
parseError = true;
Expand All @@ -56,7 +57,7 @@ export default class Home extends React.Component {
generateCustomPreview: text => {
let entries = [];
try {
entries = JSON.parse(text);
entries = this.parseNote(text);
} finally {
// eslint-disable-next-line no-unsafe-finally
return {
Expand Down Expand Up @@ -85,6 +86,30 @@ export default class Home extends React.Component {
});
}

parseNote(text) {
const entries = JSON.parse(text);

if (entries instanceof Array) {
if (entries.length === 0) {
return [];
}

for (const entry of entries) {
if (!('service' in entry)) {
throw Error('Service key is missing for an entry.');
}

if (!('secret' in entry)) {
throw Error('Secret key is missing for an entry.');
}
}

return entries;
}

return [];
}

saveNote(entries) {
this.editorKit.onEditorValueChanged(JSON.stringify(entries, null, 2));
}
Expand Down Expand Up @@ -257,16 +282,17 @@ export default class Home extends React.Component {
lastUpdated
} = this.state;

if (parseError) {
return (
<div className="sn-component">
<DataErrorAlert />
</div>
);
}

return (
<div className="sn-component">
<div className={`auth-copy-notification ${displayCopy ? 'visible' : 'hidden'}`}>
<div className="sk-panel">
<div className="sk-font-small sk-bold">
Copied value to clipboard.
</div>
</div>
</div>
{parseError && <DataErrorAlert />}
<CopyNotification isVisible={displayCopy} />
{!editMode && (
<div id="header">
<div className={`sk-horizontal-group left align-items-center ${!canEdit && 'full-width'}`}>
Expand Down
2 changes: 1 addition & 1 deletion dist/dist.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sn-token-vault",
"version": "2.0.9",
"version": "2.0.10",
"main": "dist/dist.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down

0 comments on commit 6d33d0b

Please sign in to comment.