cabana/src/components/GithubDbcList.js

92 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-06-16 23:28:51 -06:00
import React, {Component} from 'react';
import { StyleSheet, css } from 'aphrodite/no-important';
import PropTypes from 'prop-types';
import OpenDbc from '../api/opendbc';
export default class GithubDbcList extends Component {
static propTypes = {
onDbcLoaded: PropTypes.func.isRequired,
repo: PropTypes.string.isRequired,
openDbcClient: PropTypes.instanceOf(OpenDbc).isRequired
2017-06-16 23:28:51 -06:00
};
constructor(props){
super(props);
this.state = {
paths: [],
selectedPath: null,
};
this.rowForPath = this.rowForPath.bind(this);
}
componentWillReceiveProps(nextProps) {
if(nextProps.repo != this.props.repo) {
this.props.openDbcClient.list(nextProps.repo).then((paths) => {
this.setState({paths, selectedPath: null})
2017-06-16 23:28:51 -06:00
})
}
}
componentWillMount() {
this.props.openDbcClient.list(this.props.repo).then((paths) => {
paths = paths.filter((path) => path.indexOf(".dbc") !== -1);
2017-06-16 23:28:51 -06:00
this.setState({paths})
})
}
selectPath(path) {
this.setState({selectedPath: path})
this.props.openDbcClient.getDbcContents(path, this.props.repo).then((dbcContents) => {
2017-06-16 23:28:51 -06:00
this.props.onDbcLoaded(path, dbcContents);
})
}
rowForPath(path) {
const textClassName = this.state.selectedPath === path ? css(Styles.selectedPath) : null
return (<div
key={path}
className={css(Styles.row)}
onClick={() => {this.selectPath(path)}}>
<p className={textClassName}>{path}</p>
</div>);
}
render() {
return (<div className={css(Styles.root)}>
2017-06-21 00:01:41 -06:00
<p className={css(Styles.repoName)}><a href={`https://github.com/${this.props.repo}`}
target={"_blank"}
className={css(Styles.repoLink)}>{this.props.repo}</a></p>
2017-06-16 23:28:51 -06:00
<input className={css(Styles.search)} type="text" />
<div className={css(Styles.list)}>
{this.state.paths.map(this.rowForPath)}
</div>
</div>)
}
}
const Styles = StyleSheet.create({
2017-06-21 00:01:41 -06:00
repoName: {
paddingBottom: 10
},
repoLink: {
color: 'inherit',
':hover': {textDecoration: 'underline'},
':visited': {
color: 'inherit'
}
2017-06-16 23:28:51 -06:00
},
row: {
width: '100%',
borderBottom: '1px solid black',
cursor: 'pointer',
paddingTop: 10,
paddingBottom: 10
2017-06-16 23:28:51 -06:00
},
selectedPath: {
fontWeight: 'bold'
}
})