diff --git a/scripts/components/Directory.js b/scripts/components/Directory.js index 9d18f98..3006192 100644 --- a/scripts/components/Directory.js +++ b/scripts/components/Directory.js @@ -3,19 +3,21 @@ import ReactMixin from 'react-mixin'; import LocalStorageMixin from 'react-localstorage'; import request from 'superagent'; import FileNodes from './FileNodes'; +import { stopAutoScroll } from './scroll'; export default class Directory extends React.Component { - static get propTypes() { return { url: PropTypes.string, - name: PropTypes.string + name: PropTypes.string, + current: PropTypes.bool }; } static get getDefaultProps() { return { url: '', - name: '' + name: '', + current: false }; } @@ -27,11 +29,40 @@ export default class Directory extends React.Component { }; } + // also runs when the expanded flag is restored from localStorage + componentDidUpdate(prevProps, prevState) { + if (this.state.expanded && !prevState.expanded) { + request + .get(this.getExploreUrl()) + .end((err, res) => { + if (err || !this.state.expanded) { + return; + } + try { + this.setState({ children: JSON.parse(res.text) }); + } catch (e) { + // not JSON, e.g. a sign-in page after the session expired + } + }); + } + } + getLocalStorageKey() { return this.props.url; } - toggleFolder(path) { + // persist only the expanded flag; children are always loaded from the server + // eslint-disable-next-line class-methods-use-this + getStateFilterKeys() { + return ['expanded']; + } + + getExploreUrl() { + return this.props.url; + } + + toggleFolder() { + stopAutoScroll(); if (this.state.expanded) { this.setState({ expanded: false, @@ -39,26 +70,14 @@ export default class Directory extends React.Component { }); } else { this.setState({ expanded: true }); - request - .get(path) - .end((err, res) => { - if (err) { - return; - } - try { - this.setState({ children: JSON.parse(res.text) }); - } catch (e) { - return; - } - }); } } render() { const arrow = this.state.expanded ? 'octicon octicon-chevron-down' : 'octicon octicon-chevron-right'; return ( -