2022-02-23 05:31:15 -06:00
|
|
|
import { useMemo } from 'react';
|
2020-08-26 04:38:39 -05:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
|
|
|
import { getMessageFromError } from 'app/core/utils/errors';
|
|
|
|
import { AppNotification, AppNotificationSeverity, useDispatch } from 'app/types';
|
|
|
|
|
2022-02-23 05:31:15 -06:00
|
|
|
import { notifyApp } from '../actions';
|
2018-10-24 07:33:53 -05:00
|
|
|
|
2019-08-05 05:07:35 -05:00
|
|
|
const defaultSuccessNotification = {
|
2018-10-24 07:33:53 -05:00
|
|
|
title: '',
|
|
|
|
text: '',
|
|
|
|
severity: AppNotificationSeverity.Success,
|
2020-04-12 15:20:02 -05:00
|
|
|
icon: 'check',
|
2018-10-24 07:33:53 -05:00
|
|
|
};
|
|
|
|
|
2019-08-05 05:07:35 -05:00
|
|
|
const defaultWarningNotification = {
|
2018-10-24 07:33:53 -05:00
|
|
|
title: '',
|
|
|
|
text: '',
|
|
|
|
severity: AppNotificationSeverity.Warning,
|
2020-04-12 15:20:02 -05:00
|
|
|
icon: 'exclamation-triangle',
|
2018-10-24 07:33:53 -05:00
|
|
|
};
|
|
|
|
|
2019-08-05 05:07:35 -05:00
|
|
|
const defaultErrorNotification = {
|
2018-10-24 07:33:53 -05:00
|
|
|
title: '',
|
|
|
|
text: '',
|
|
|
|
severity: AppNotificationSeverity.Error,
|
2020-04-12 15:20:02 -05:00
|
|
|
icon: 'exclamation-triangle',
|
2018-10-24 07:33:53 -05:00
|
|
|
};
|
|
|
|
|
2022-04-20 04:42:32 -05:00
|
|
|
export const createSuccessNotification = (title: string, text = '', traceId?: string): AppNotification => ({
|
2018-10-24 07:33:53 -05:00
|
|
|
...defaultSuccessNotification,
|
2022-04-20 04:42:32 -05:00
|
|
|
title,
|
|
|
|
text,
|
2020-08-26 04:38:39 -05:00
|
|
|
id: uuidv4(),
|
2022-04-20 04:42:32 -05:00
|
|
|
timestamp: Date.now(),
|
|
|
|
showing: true,
|
2018-10-24 07:33:53 -05:00
|
|
|
});
|
|
|
|
|
2020-04-21 07:18:56 -05:00
|
|
|
export const createErrorNotification = (
|
|
|
|
title: string,
|
|
|
|
text: string | Error = '',
|
2022-04-20 04:42:32 -05:00
|
|
|
traceId?: string,
|
2020-04-21 07:18:56 -05:00
|
|
|
component?: React.ReactElement
|
|
|
|
): AppNotification => {
|
2019-02-05 07:42:29 -06:00
|
|
|
return {
|
|
|
|
...defaultErrorNotification,
|
2019-02-06 12:42:04 -06:00
|
|
|
text: getMessageFromError(text),
|
2019-11-14 03:59:41 -06:00
|
|
|
title,
|
2020-08-26 04:38:39 -05:00
|
|
|
id: uuidv4(),
|
2022-04-20 04:42:32 -05:00
|
|
|
traceId,
|
2019-11-14 03:59:41 -06:00
|
|
|
component,
|
2022-04-20 04:42:32 -05:00
|
|
|
timestamp: Date.now(),
|
|
|
|
showing: true,
|
2019-02-05 07:42:29 -06:00
|
|
|
};
|
|
|
|
};
|
2018-10-24 07:33:53 -05:00
|
|
|
|
2022-04-20 04:42:32 -05:00
|
|
|
export const createWarningNotification = (title: string, text = '', traceId?: string): AppNotification => ({
|
2018-10-24 07:33:53 -05:00
|
|
|
...defaultWarningNotification,
|
2022-04-20 04:42:32 -05:00
|
|
|
title,
|
|
|
|
text,
|
|
|
|
traceId,
|
2020-08-26 04:38:39 -05:00
|
|
|
id: uuidv4(),
|
2022-04-20 04:42:32 -05:00
|
|
|
timestamp: Date.now(),
|
|
|
|
showing: true,
|
2018-10-24 07:33:53 -05:00
|
|
|
});
|
2022-02-23 05:31:15 -06:00
|
|
|
|
|
|
|
/** Hook for showing toast notifications with varying severity (success, warning error).
|
|
|
|
* @example
|
|
|
|
* const notifyApp = useAppNotification();
|
|
|
|
* notifyApp.success('Success!', 'Some additional text');
|
|
|
|
* notifyApp.warning('Warning!');
|
|
|
|
* notifyApp.error('Error!');
|
|
|
|
*/
|
|
|
|
export function useAppNotification() {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
return useMemo(
|
|
|
|
() => ({
|
|
|
|
success: (title: string, text = '') => {
|
|
|
|
dispatch(notifyApp(createSuccessNotification(title, text)));
|
|
|
|
},
|
2022-04-20 04:42:32 -05:00
|
|
|
warning: (title: string, text = '', traceId?: string) => {
|
|
|
|
dispatch(notifyApp(createWarningNotification(title, text, traceId)));
|
2022-02-23 05:31:15 -06:00
|
|
|
},
|
2022-04-20 04:42:32 -05:00
|
|
|
error: (title: string, text = '', traceId?: string) => {
|
|
|
|
dispatch(notifyApp(createErrorNotification(title, text, traceId)));
|
2022-02-23 05:31:15 -06:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
[dispatch]
|
|
|
|
);
|
|
|
|
}
|