Better cookie handling/editing

This commit is contained in:
Gregory Schier 2017-08-22 18:16:21 -07:00
parent 2887282032
commit c69c8d2dd0
4 changed files with 167 additions and 116 deletions

View File

@ -4,3 +4,4 @@ indent_style = space
indent_size = 2 indent_size = 2
insert_final_newline = true insert_final_newline = true
trim_trailing_whitespace = true trim_trailing_whitespace = true
end_of_line = lf

View File

@ -2,8 +2,10 @@
import React, {PureComponent} from 'react'; import React, {PureComponent} from 'react';
import {Tabs, TabList, Tab, TabPanel} from 'react-tabs'; import {Tabs, TabList, Tab, TabPanel} from 'react-tabs';
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import deepEqual from 'deep-equal';
import * as toughCookie from 'tough-cookie'; import * as toughCookie from 'tough-cookie';
import * as models from '../../../models'; import * as models from '../../../models';
import clone from 'clone';
import {DEBOUNCE_MILLIS} from '../../../common/constants'; import {DEBOUNCE_MILLIS} from '../../../common/constants';
import {trackEvent} from '../../../analytics/index'; import {trackEvent} from '../../../analytics/index';
import Modal from '../base/modal'; import Modal from '../base/modal';
@ -96,9 +98,11 @@ class CookieModifyModal extends PureComponent {
} }
async _handleCookieUpdate (oldCookie: Cookie, cookie: Cookie) { async _handleCookieUpdate (oldCookie: Cookie, cookie: Cookie) {
const {cookieJar} = this.props; // Clone so we don't modify the original
const cookieJar = clone(this.props.cookieJar);
const {cookies} = cookieJar; const {cookies} = cookieJar;
const index = cookies.findIndex(c => c.domain === oldCookie.domain && c.key === oldCookie.key); const index = cookies.findIndex(c => deepEqual(c, oldCookie));
cookieJar.cookies = [ cookieJar.cookies = [
...cookies.slice(0, index), ...cookies.slice(0, index),
@ -144,6 +148,21 @@ class CookieModifyModal extends PureComponent {
return str.charAt(0).toUpperCase() + str.slice(1); return str.charAt(0).toUpperCase() + str.slice(1);
} }
_getRawCookieString () {
const {cookie} = this.state;
if (!cookie) {
return '';
}
try {
return cookieToString(toughCookie.Cookie.fromJSON(JSON.stringify(cookie)));
} catch (err) {
console.warn('Failed to parse cookie string', err);
return '';
}
}
render () { render () {
const { const {
cookieJar, cookieJar,
@ -151,33 +170,19 @@ class CookieModifyModal extends PureComponent {
handleGetRenderContext handleGetRenderContext
} = this.props; } = this.props;
if (!cookieJar) {
return null;
}
const { const {
isValid, isValid,
cookie cookie
} = this.state; } = this.state;
if (!cookie || !cookieJar) {
return null;
}
const textFields = ['key', 'value', 'domain', 'path', 'expires']; const textFields = ['key', 'value', 'domain', 'path', 'expires'];
const checkFields = ['secure', 'httpOnly']; const checkFields = ['secure', 'httpOnly'];
let rawCookieString = '';
try {
rawCookieString = cookieToString(toughCookie.Cookie.fromJSON(JSON.stringify(cookie)));
} catch (err) {
console.warn('Failed to parse cookie', err);
}
return ( return (
<Modal ref={this._setModalRef} {...this.props}> <Modal ref={this._setModalRef} {...this.props}>
<ModalHeader>Edit Cookie</ModalHeader> <ModalHeader>Edit Cookie</ModalHeader>
<ModalBody className="cookie-modify"> <ModalBody className="cookie-modify">
{cookieJar && cookie && (
<Tabs> <Tabs>
<TabList> <TabList>
<Tab> <Tab>
@ -231,11 +236,12 @@ class CookieModifyModal extends PureComponent {
<label>Raw Cookie String <label>Raw Cookie String
<input type="text" <input type="text"
onChange={this._handleChangeRawValue} onChange={this._handleChangeRawValue}
defaultValue={rawCookieString}/> defaultValue={this._getRawCookieString()}/>
</label> </label>
</div> </div>
</TabPanel> </TabPanel>
</Tabs> </Tabs>
)}
</ModalBody> </ModalBody>
<ModalFooter> <ModalFooter>
<button className="btn" onClick={this.hide}> <button className="btn" onClick={this.hide}>

View File

@ -1,5 +1,6 @@
// @flow // @flow
import React, {PureComponent} from 'react'; import React, {PureComponent} from 'react';
import deepEqual from 'deep-equal';
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Modal from '../base/modal'; import Modal from '../base/modal';
import ModalBody from '../base/modal-body'; import ModalBody from '../base/modal-body';
@ -10,6 +11,7 @@ import * as models from '../../../models';
import {trackEvent} from '../../../analytics/index'; import {trackEvent} from '../../../analytics/index';
import type {Cookie, CookieJar} from '../../../models/cookie-jar'; import type {Cookie, CookieJar} from '../../../models/cookie-jar';
import type {Workspace} from '../../../models/workspace'; import type {Workspace} from '../../../models/workspace';
import {fuzzyMatch} from '../../../common/misc';
@autobind @autobind
class CookiesModal extends PureComponent { class CookiesModal extends PureComponent {
@ -21,7 +23,8 @@ class CookiesModal extends PureComponent {
}; };
state: { state: {
filter: string filter: string,
visibleCookieIndexes: Array<number> | null
}; };
modal: Modal | null; modal: Modal | null;
@ -29,8 +32,10 @@ class CookiesModal extends PureComponent {
constructor (props: any) { constructor (props: any) {
super(props); super(props);
this.state = { this.state = {
filter: '' filter: '',
visibleCookieIndexes: null
}; };
} }
@ -74,21 +79,56 @@ class CookiesModal extends PureComponent {
trackEvent('Cookie', 'Delete'); trackEvent('Cookie', 'Delete');
} }
_handleFilterChange (e: Event & {target: HTMLInputElement}) { async _handleFilterChange (e: Event & {target: HTMLInputElement}) {
const filter = e.target.value; const filter = e.target.value;
this.setState({filter}); this._applyFilter(filter, this.props.cookieJar.cookies);
trackEvent('Cookie Editor', 'Filter Change');
} }
_getFilteredSortedCookies () { componentWillReceiveProps (nextProps: any) {
const {cookieJar} = this.props; // Re-filter if we received new cookies
const {filter} = this.state; // Compare cookies with Dates cast to strings
const sameCookies = deepEqual(
this.props.cookieJar.cookies,
nextProps.cookieJar.cookies
);
const {cookies} = cookieJar; if (!sameCookies) {
return cookies.filter(c => { this._applyFilter(this.state.filter, nextProps.cookieJar.cookies);
const toSearch = JSON.stringify(c).toLowerCase(); }
return toSearch.indexOf(filter.toLowerCase()) !== -1; }
});
async _applyFilter (filter: string, cookies: Array<Cookie>) {
const renderedCookies = await this.props.handleRender(cookies);
let visibleCookieIndexes;
if (filter) {
visibleCookieIndexes = [];
for (let i = 0; i < renderedCookies.length; i++) {
const toSearch = JSON.stringify(renderedCookies[i]);
const matched = fuzzyMatch(filter, toSearch);
if (matched) {
visibleCookieIndexes.push(i);
}
}
} else {
visibleCookieIndexes = null;
}
console.log('APPLIED FILTER', filter, visibleCookieIndexes);
this.setState({filter, visibleCookieIndexes});
}
_getVisibleCookies () {
const {cookieJar} = this.props;
const {visibleCookieIndexes} = this.state;
if (visibleCookieIndexes === null) {
return cookieJar.cookies;
}
return cookieJar.cookies.filter((c, i) => visibleCookieIndexes.includes(i));
} }
async show () { async show () {
@ -98,6 +138,9 @@ class CookiesModal extends PureComponent {
this.filterInput && this.filterInput.focus(); this.filterInput && this.filterInput.focus();
}, 100); }, 100);
// make sure the filter is up to date
await this._applyFilter(this.state.filter, this.props.cookieJar.cookies);
this.modal && this.modal.show(); this.modal && this.modal.show();
trackEvent('Cookie Manager', 'Show'); trackEvent('Cookie Manager', 'Show');
} }
@ -113,20 +156,16 @@ class CookiesModal extends PureComponent {
cookieJar cookieJar
} = this.props; } = this.props;
if (!cookieJar) {
return null;
}
const { const {
filter filter
} = this.state; } = this.state;
const filteredCookies = this._getFilteredSortedCookies();
return ( return (
<Modal ref={this._setModalRef} wide tall {...this.props}> <Modal ref={this._setModalRef} wide tall {...this.props}>
<ModalHeader>Manage Cookies</ModalHeader> <ModalHeader>Manage Cookies</ModalHeader>
<ModalBody className="cookie-list" noScroll> <ModalBody className="cookie-list" noScroll>
{cookieJar && (
<div>
<div className="pad"> <div className="pad">
<div className="form-control form-control--outlined"> <div className="form-control form-control--outlined">
<label>Filter Cookies <label>Filter Cookies
@ -143,7 +182,7 @@ class CookiesModal extends PureComponent {
<CookieList <CookieList
handleShowModifyCookieModal={handleShowModifyCookieModal} handleShowModifyCookieModal={handleShowModifyCookieModal}
handleRender={handleRender} handleRender={handleRender}
cookies={filteredCookies} cookies={this._getVisibleCookies()}
onCookieAdd={this._handleCookieAdd} onCookieAdd={this._handleCookieAdd}
onCookieDelete={this._handleCookieDelete} onCookieDelete={this._handleCookieDelete}
// Set the domain to the filter so that it shows up if we're filtering // Set the domain to the filter so that it shows up if we're filtering
@ -151,6 +190,8 @@ class CookiesModal extends PureComponent {
/> />
</div> </div>
</div> </div>
</div>
)}
</ModalBody> </ModalBody>
<ModalFooter> <ModalFooter>
<div className="margin-left faint italic txt-sm tall"> <div className="margin-left faint italic txt-sm tall">

3
flow-typed/deep-equal.js vendored Normal file
View File

@ -0,0 +1,3 @@
declare module 'deep-equal' {
declare module.exports: Function
}