import * as q from '../../../../backend/src/Model' import * as React from 'react' import Copy from '../helper/Copy' import CustomIconButton from '../CustomIconButton' import Delete from '@material-ui/icons/Delete' import ExpandMore from '@material-ui/icons/ExpandMore' import NodeStats from './NodeStats' import Topic from './Topic' import ValuePanel from './ValueRenderer/ValuePanel' import { AppState } from '../../reducers' import { bindActionCreators } from 'redux' import { connect } from 'react-redux' import { settingsActions, sidebarActions } from '../../actions' import { StyleRulesCallback, Theme, withStyles } from '@material-ui/core/styles' import { TopicViewModel } from '../../TopicViewModel' import { ExpansionPanel, ExpansionPanelDetails, ExpansionPanelSummary, Typography, Tooltip, Badge, } from '@material-ui/core' const throttle = require('lodash.throttle') const Publish = React.lazy(() => import('./Publish/Publish')) interface Props { node?: q.TreeNode actions: typeof sidebarActions settingsActions: typeof settingsActions classes: any connectionId?: string } interface State { node: q.TreeNode compareMessage?: q.Message valueRenderWidth: number } class Sidebar extends React.Component { private updateNode = throttle(() => { this.setState(this.state) }, 300) private detailsStyle = { padding: '0px 16px 8px 8px', display: 'block' } constructor(props: any) { super(props) console.error('Find and fix me #state') this.state = { node: new q.Tree(), valueRenderWidth: 300 } } private registerUpdateListener(node: q.TreeNode) { node.onMerge.subscribe(this.updateNode) node.onMessage.subscribe(this.updateNode) } private removeUpdateListener(node: q.TreeNode) { node.onMerge.unsubscribe(this.updateNode) node.onMessage.unsubscribe(this.updateNode) } private renderTopicDeleteButton() { if (!this.props.node || (!this.props.node.message || !this.props.node.message.value)) { return null } return ( this.deleteTopic(this.props.node)}> ) } private renderRecursiveTopicDeleteButton() { const deleteLimit = 50 const topicCount = this.props.node ? this.props.node.childTopicCount() : 0 if ((!this.props.node || topicCount === 0) || (this.props.node.message && topicCount === 1)) { return null } return ( this.deleteTopic(this.props.node, true, deleteLimit)}> {topicCount >= deleteLimit ? '50+' : topicCount}} color="secondary" > ) } private deleteTopic = (topic?: q.TreeNode, recursive: boolean = false, maxCount = 50) => { if (!topic) { return } this.props.actions.clearTopic(topic, recursive, maxCount) } private renderNode() { const { classes, node } = this.props const copyTopic = node ? : null const deleteTopic = this.renderTopicDeleteButton() const deleteRecursiveTopic = this.renderRecursiveTopicDeleteButton() const summaryStyle = { minHeight: '0' } return (
} style={summaryStyle}> Topic {copyTopic} {deleteTopic} {deleteRecursiveTopic} {} } style={summaryStyle}> Publish Loading...
}> } style={summaryStyle}> Stats {this.renderNodeStats()} ) } private renderNodeStats() { if (!this.props.node) { return null } return ( ) } public componentWillReceiveProps(nextProps: Props) { this.props.node && this.removeUpdateListener(this.props.node) nextProps.node && this.registerUpdateListener(nextProps.node) this.props.node && this.setState({ node: this.props.node }) if (this.props.node !== nextProps.node) { this.setState({ compareMessage: undefined }) } } public componentWillUnmount() { this.props.node && this.removeUpdateListener(this.props.node) } public render() { return ( ) } } const mapStateToProps = (state: AppState) => { return { node: state.tree.selectedTopic, } } const mapDispatchToProps = (dispatch: any) => { return { actions: bindActionCreators(sidebarActions, dispatch), settingsActions: bindActionCreators(settingsActions, dispatch), } } const styles: StyleRulesCallback = (theme: Theme) => { return { drawer: { display: 'block', height: '100%', }, valuePaper: { margin: theme.spacing(1), }, heading: { fontSize: theme.typography.pxToRem(15), fontWeight: theme.typography.fontWeightRegular, }, } } export default withStyles(styles)(connect(mapStateToProps, mapDispatchToProps)(Sidebar))