2021-03-11 12:09:27 +01:00
|
|
|
import React from 'react';
|
|
|
|
import { createPortal } from 'react-dom';
|
2022-02-22 13:16:01 +01:00
|
|
|
import invariant from 'tiny-invariant';
|
2021-03-11 12:09:27 +01:00
|
|
|
|
2022-02-08 12:49:51 +01:00
|
|
|
export function FlashMessage({
|
|
|
|
message,
|
|
|
|
level,
|
|
|
|
sticky,
|
|
|
|
fixed
|
|
|
|
}: {
|
|
|
|
message: string;
|
|
|
|
level: string;
|
|
|
|
sticky?: boolean;
|
|
|
|
fixed?: boolean;
|
|
|
|
}) {
|
2022-02-22 13:16:01 +01:00
|
|
|
const element = document.getElementById('flash_messages');
|
|
|
|
invariant(element, 'Flash messages root element not found');
|
2021-03-11 12:09:27 +01:00
|
|
|
return createPortal(
|
|
|
|
<div className="flash_message center">
|
|
|
|
<div className={flashClassName(level, sticky, fixed)}>{message}</div>
|
|
|
|
</div>,
|
2022-02-22 13:16:01 +01:00
|
|
|
element
|
2021-03-11 12:09:27 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-08 12:49:51 +01:00
|
|
|
function flashClassName(level: string, sticky = false, fixed = false) {
|
2021-03-11 12:09:27 +01:00
|
|
|
const className =
|
|
|
|
level == 'notice' ? ['alert', 'alert-success'] : ['alert', 'alert-danger'];
|
|
|
|
|
|
|
|
if (sticky) {
|
|
|
|
className.push('sticky');
|
|
|
|
}
|
|
|
|
if (fixed) {
|
|
|
|
className.push('alert-fixed');
|
|
|
|
}
|
|
|
|
return className.join(' ');
|
|
|
|
}
|