2020-05-08 13:50:55 -04:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2021-09-22 06:44:54 -04:00
|
|
|
import { useToasts } from 'src/components/MessageToasts/withToasts';
|
2023-03-15 13:48:03 -04:00
|
|
|
import { useComponentDidMount } from '@superset-ui/core';
|
2020-05-08 13:50:55 -04:00
|
|
|
|
2021-02-23 11:46:12 -05:00
|
|
|
type FlashMessageType = 'info' | 'alert' | 'danger' | 'warning' | 'success';
|
|
|
|
export type FlashMessage = [FlashMessageType, string];
|
2020-05-08 13:50:55 -04:00
|
|
|
|
|
|
|
interface Props {
|
2021-02-23 11:46:12 -05:00
|
|
|
children: JSX.Element;
|
|
|
|
messages: FlashMessage[];
|
2020-05-08 13:50:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const flashObj = {
|
|
|
|
info: 'addInfoToast',
|
2020-07-05 18:08:37 -04:00
|
|
|
alert: 'addDangerToast',
|
2020-05-08 13:50:55 -04:00
|
|
|
danger: 'addDangerToast',
|
|
|
|
warning: 'addWarningToast',
|
|
|
|
success: 'addSuccessToast',
|
|
|
|
};
|
|
|
|
|
2021-02-23 11:46:12 -05:00
|
|
|
export default function FlashProvider({ children, messages }: Props) {
|
|
|
|
const toasts = useToasts();
|
|
|
|
useComponentDidMount(() => {
|
|
|
|
messages.forEach(message => {
|
2020-05-08 13:50:55 -04:00
|
|
|
const [type, text] = message;
|
|
|
|
const flash = flashObj[type];
|
2021-02-23 11:46:12 -05:00
|
|
|
const toast = toasts[flash];
|
|
|
|
if (toast) {
|
|
|
|
toast(text);
|
2020-07-05 18:08:37 -04:00
|
|
|
}
|
2020-05-08 13:50:55 -04:00
|
|
|
});
|
2021-02-23 11:46:12 -05:00
|
|
|
});
|
|
|
|
return children;
|
2020-05-08 13:50:55 -04:00
|
|
|
}
|