2018-04-17 07:24:02 +02:00
|
|
|
// @flow
|
2019-10-16 20:11:00 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
2018-03-26 23:32:43 +02:00
|
|
|
import Button from 'component/button';
|
2018-10-29 18:23:53 +01:00
|
|
|
import classnames from 'classnames';
|
2019-10-16 20:11:00 +02:00
|
|
|
import Icon from 'component/common/icon';
|
2020-09-18 05:49:45 +02:00
|
|
|
import LbcMessage from 'component/common/lbc-message';
|
2017-05-23 09:21:21 +02:00
|
|
|
|
2018-04-17 07:24:02 +02:00
|
|
|
type Props = {
|
2021-10-04 15:21:59 +02:00
|
|
|
removeSnack: (any) => void,
|
2022-03-18 04:24:58 +01:00
|
|
|
snack: ?ToastParams,
|
2022-05-06 12:08:09 +02:00
|
|
|
snackCount: number,
|
2018-04-17 07:24:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class SnackBar extends React.PureComponent<Props> {
|
|
|
|
constructor(props: Props) {
|
2017-05-23 09:21:21 +02:00
|
|
|
super(props);
|
2022-05-06 12:08:09 +02:00
|
|
|
this.intervalId = null;
|
2017-05-23 09:21:21 +02:00
|
|
|
}
|
|
|
|
|
2022-05-06 12:08:09 +02:00
|
|
|
intervalId: ?IntervalID;
|
2019-01-08 00:29:40 +01:00
|
|
|
|
2017-05-23 09:21:21 +02:00
|
|
|
render() {
|
2022-05-06 12:08:09 +02:00
|
|
|
const { snack, snackCount, removeSnack } = this.props;
|
2017-05-23 09:21:21 +02:00
|
|
|
|
2018-04-19 20:49:47 +02:00
|
|
|
if (!snack) {
|
2022-05-06 12:08:09 +02:00
|
|
|
clearInterval(this.intervalId);
|
|
|
|
this.intervalId = null; // should be unmounting anyway, but be safe?
|
2017-05-23 09:21:21 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-18 04:24:58 +01:00
|
|
|
const { message, subMessage, duration, linkText, linkTarget, isError } = snack;
|
2017-05-23 09:21:21 +02:00
|
|
|
|
2022-05-06 12:08:09 +02:00
|
|
|
if (this.intervalId) {
|
|
|
|
// TODO: render should be pure
|
|
|
|
clearInterval(this.intervalId);
|
2017-05-23 09:21:21 +02:00
|
|
|
}
|
|
|
|
|
2022-05-06 12:08:09 +02:00
|
|
|
this.intervalId = setInterval(
|
|
|
|
() => {
|
|
|
|
removeSnack();
|
|
|
|
},
|
|
|
|
duration === 'long' ? 10000 : 5000
|
|
|
|
);
|
|
|
|
|
2017-05-23 09:21:21 +02:00
|
|
|
return (
|
2018-12-19 06:44:53 +01:00
|
|
|
<div
|
|
|
|
className={classnames('snack-bar', {
|
|
|
|
'snack-bar--error': isError,
|
2022-05-06 12:08:09 +02:00
|
|
|
'snack-bar--stacked-error': snackCount > 1 && isError,
|
|
|
|
'snack-bar--stacked-non-error': snackCount > 1 && !isError,
|
2018-12-19 06:44:53 +01:00
|
|
|
})}
|
|
|
|
>
|
2018-04-17 07:24:02 +02:00
|
|
|
<div className="snack-bar__message">
|
2020-08-27 21:36:05 +02:00
|
|
|
<Icon icon={isError ? ICONS.ALERT : ICONS.COMPLETED} size={18} />
|
2021-10-04 15:21:59 +02:00
|
|
|
<p className="snack-bar__messageText">
|
2020-09-18 05:49:45 +02:00
|
|
|
<LbcMessage>{message}</LbcMessage>
|
2022-03-18 04:24:58 +01:00
|
|
|
{subMessage && (
|
|
|
|
<p className="snack-bar__messageText snack-bar__messageText--sub">
|
|
|
|
<LbcMessage>{subMessage}</LbcMessage>
|
|
|
|
</p>
|
|
|
|
)}
|
2021-10-04 15:21:59 +02:00
|
|
|
</p>
|
2022-03-18 04:24:58 +01:00
|
|
|
<Button
|
|
|
|
className="snack-bar__close"
|
|
|
|
icon={ICONS.REMOVE}
|
|
|
|
title={__('Dismiss')}
|
|
|
|
onClick={() => removeSnack()}
|
|
|
|
/>
|
2018-04-17 07:24:02 +02:00
|
|
|
</div>
|
2019-07-01 20:51:57 +02: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 09:21:21 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:21:55 +02:00
|
|
|
export default SnackBar;
|