2020-01-07 02:20:16 +00:00
|
|
|
import * as React from 'react';
|
|
|
|
import { last, noop } from 'lodash';
|
|
|
|
import { Toast } from '../elements/Toast';
|
|
|
|
|
|
|
|
export type Props = React.HTMLProps<HTMLDivElement> & {
|
|
|
|
loaf: Array<{ id: number; text: string }>;
|
|
|
|
onDismiss: () => unknown;
|
|
|
|
};
|
|
|
|
|
|
|
|
const DEFAULT_DISMISS = 1e4;
|
|
|
|
|
|
|
|
export const Toaster = React.memo(({ loaf, onDismiss, className }: Props) => {
|
|
|
|
const slice = last(loaf);
|
|
|
|
|
2020-01-08 17:44:54 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (!slice) {
|
|
|
|
return noop;
|
|
|
|
}
|
|
|
|
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
onDismiss();
|
|
|
|
}, DEFAULT_DISMISS);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
clearTimeout(timer);
|
|
|
|
};
|
|
|
|
}, [slice, onDismiss]);
|
2020-01-07 02:20:16 +00:00
|
|
|
|
|
|
|
if (!slice) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={className}>
|
|
|
|
<Toast key={slice.id} onClick={onDismiss} tabIndex={0}>
|
|
|
|
{slice.text}
|
|
|
|
</Toast>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|