lbry-desktop/ui/component/snackBar/view.jsx

71 lines
1.8 KiB
React
Raw Normal View History

2018-04-17 08:24:02 +03:00
// @flow
import * as ICONS from 'constants/icons';
import React from 'react';
2018-03-26 14:32:43 -07:00
import Button from 'component/button';
import classnames from 'classnames';
import Icon from 'component/common/icon';
2020-09-17 23:49:45 -04:00
import LbcMessage from 'component/common/lbc-message';
2017-05-23 11:21:21 +04:00
2018-04-17 08:24:02 +03:00
type Props = {
removeSnack: (any) => void,
2018-04-19 14:49:47 -04:00
snack: ?{
2018-04-17 08:24:02 +03:00
linkTarget: ?string,
linkText: ?string,
message: string,
isError: boolean,
2018-04-17 08:24:02 +03:00
},
};
class SnackBar extends React.PureComponent<Props> {
constructor(props: Props) {
2017-05-23 11:21:21 +04:00
super(props);
2018-04-17 08:24:02 +03:00
this.displayTime = 5; // in seconds
this.hideTimeout = null;
2017-05-23 11:21:21 +04:00
}
hideTimeout: ?TimeoutID;
displayTime: number;
2017-05-23 11:21:21 +04:00
render() {
2018-04-19 14:49:47 -04:00
const { snack, removeSnack } = this.props;
2017-05-23 11:21:21 +04:00
2018-04-19 14:49:47 -04:00
if (!snack) {
2018-04-17 08:24:02 +03:00
this.hideTimeout = null; // should be unmounting anyway, but be safe?
2017-05-23 11:21:21 +04:00
return null;
}
const { message, linkText, linkTarget, isError } = snack;
2017-05-23 11:21:21 +04:00
2018-04-17 08:24:02 +03:00
if (this.hideTimeout === null) {
this.hideTimeout = setTimeout(() => {
this.hideTimeout = null;
2017-06-06 17:19:12 -04:00
removeSnack();
2018-04-17 08:24:02 +03:00
}, this.displayTime * 1000);
2017-05-23 11:21:21 +04:00
}
return (
<div
className={classnames('snack-bar', {
'snack-bar--error': isError,
})}
>
2018-04-17 08:24:02 +03:00
<div className="snack-bar__message">
<Icon icon={isError ? ICONS.ALERT : ICONS.COMPLETED} size={18} />
<p className="snack-bar__messageText">
2020-09-17 23:49:45 -04:00
<LbcMessage>{message}</LbcMessage>
</p>
2018-04-17 08:24:02 +03:00
</div>
2019-07-01 14:51:57 -04:00
{linkText && linkTarget && (
// This is a little weird because of `linkTarget` code in `lbry-redux`
// Any navigation code should happen in the app, and that should be removed from lbry-redux
<Button navigate={`/$${linkTarget}`} className="snack-bar__action" label={linkText} />
)}
2017-05-23 11:21:21 +04:00
</div>
);
}
}
2017-06-05 21:21:55 -07:00
export default SnackBar;