Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from 'react';
- import { useAtom } from 'jotai';
- import { useTranslation } from 'react-i18next';
- import { GlobalNotificationAtom } from '@store';
- type NotificationVariantType = 'info' | 'success' | 'warning' | 'error';
- type i18nNamespaceType =
- | 'common'
- | 'decision_details'
- | 'edge_details'
- | 'error'
- | 'login'
- | 'navigation'
- | 'node_details'
- | 'snippet_create'
- | 'snippet_details'
- | 'snippet_list'
- | 'workflow_create'
- | 'workflow_details'
- | 'workflow_list';
- export const useNotification = (namespaces: i18nNamespaceType[] = []): ((
- title: string,
- message: string,
- variant: NotificationVariantType,
- params?:
- | {
- [key: string]: unknown;
- }
- | undefined,
- ) => void) => {
- const [, setGlobalNotificationAtom] = useAtom(GlobalNotificationAtom);
- const { t, i18n } = useTranslation([...(namespaces || {})]);
- const openNotification = React.useCallback(
- (
- title: string,
- message: string,
- variant: NotificationVariantType,
- params?: { [key: string]: unknown },
- ) => {
- const titleFallback = t('error:missing-namespace');
- const messageFallback = t('error:missing-namespace');
- if (namespaces.length > 0) {
- for (let ns = 0; ns < namespaces.length; ns++) {
- if (i18n.exists(`${namespaces[ns]}:${title}`)) {
- title = `${namespaces[ns]}:${title}`;
- }
- if (i18n.exists(`${namespaces[ns]}:${message}`)) {
- message = `${namespaces[ns]}:${message}`;
- }
- }
- }
- title = !i18n.exists(title) ? titleFallback : title;
- message = !i18n.exists(message) ? messageFallback : message;
- setGlobalNotificationAtom({
- title: t(title, params),
- variant: variant,
- visible: true,
- message: t(message, params),
- });
- },
- [i18n, namespaces, setGlobalNotificationAtom, t],
- );
- return openNotification;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement