lbry-desktop/ui/js/component/snack-bar.js

61 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-04-10 14:32:40 +02:00
import React from 'react';
import lbry from '../lbry.js';
2017-05-17 10:10:25 +02:00
export class SnackBar extends React.Component {
constructor(props) {
super(props);
2017-04-10 14:32:40 +02:00
2017-05-17 10:10:25 +02:00
this._displayTime = 5; // in seconds
this._hideTimeout = null;
2017-04-10 14:32:40 +02:00
2017-05-17 10:10:25 +02:00
this.state = {
2017-04-10 14:32:40 +02:00
snacks: []
2017-05-17 10:10:25 +02:00
};
}
handleSnackReceived(event) {
2017-04-10 14:32:40 +02:00
// if (this._hideTimeout) {
// clearTimeout(this._hideTimeout);
// }
let snacks = this.state.snacks;
snacks.push(event.detail);
this.setState({ snacks: snacks});
2017-05-17 10:10:25 +02:00
}
componentWillMount() {
2017-04-10 14:32:40 +02:00
document.addEventListener('globalNotice', this.handleSnackReceived);
2017-05-17 10:10:25 +02:00
}
componentWillUnmount() {
2017-04-10 14:32:40 +02:00
document.removeEventListener('globalNotice', this.handleSnackReceived);
2017-05-17 10:10:25 +02:00
}
render() {
2017-04-10 14:32:40 +02:00
if (!this.state.snacks.length) {
this._hideTimeout = null; //should be unmounting anyway, but be safe?
return null;
}
let snack = this.state.snacks[0];
if (this._hideTimeout === null) {
this._hideTimeout = setTimeout(() => {
2017-04-10 14:32:40 +02:00
this._hideTimeout = null;
let snacks = this.state.snacks;
snacks.shift();
this.setState({ snacks: snacks });
}, this._displayTime * 1000);
2017-04-10 14:32:40 +02:00
}
return (
<div className="snack-bar">
{snack.message}
{snack.linkText && snack.linkTarget ?
<a className="snack-bar__action" href={snack.linkTarget}>{snack.linkText}</a> : ''}
</div>
);
2017-05-17 10:10:25 +02:00
}
}
2017-04-10 14:32:40 +02:00
export default SnackBar;