-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathGlobalNotification.tsx
More file actions
86 lines (75 loc) · 2.63 KB
/
Copy pathGlobalNotification.tsx
File metadata and controls
86 lines (75 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { getGlobalNotification } from '@/api/platform';
import { Alert, AlertIcon, AlertDescription, CloseButton, Box } from '@chakra-ui/react';
import { useMessage } from '@labring/sealos-ui';
import { Info, X } from 'lucide-react';
import { useEffect, memo } from 'react';
import { useTranslation } from 'react-i18next';
import { useQuery } from '@tanstack/react-query';
import { useGlobalNotificationStore } from '@/stores/globalNotification';
import DOMPurify from 'dompurify';
function GlobalNotificationComponent() {
const { i18n } = useTranslation();
const { message } = useMessage();
const { closedNotificationId, setClosedNotificationId } = useGlobalNotificationStore();
const { data: notifications } = useQuery({
queryKey: ['globalNotification'],
queryFn: async () => {
const { data } = await getGlobalNotification();
return data || [];
},
staleTime: 5 * 60 * 1000,
cacheTime: 10 * 60 * 1000,
refetchOnWindowFocus: false,
refetchOnMount: false,
refetchInterval: 30 * 60 * 1000,
retry: 1,
enabled: true
});
// Filter 'Desktop-Alert' type
const notification = notifications?.find((n) => n?.from === 'Desktop-Alert');
// [TODO] This is not the correct place to do popup notifications
useEffect(() => {
if (!notification || !notification.licenseFrontend) return;
const title = notification?.i18n[i18n?.language]?.title;
message({
title: title,
status: 'info',
isClosable: true
});
}, [notification, i18n?.language, message]);
if (!notification) return null;
const notificationId = notification?.uid;
if (!notificationId) {
return null;
}
if (closedNotificationId === notificationId) {
return null;
}
const rawContent = notification?.i18n[i18n?.language]?.title || '';
const sanitizedContent = DOMPurify.sanitize(rawContent);
const handleClose = () => {
setClosedNotificationId(notificationId);
};
return (
<Box padding={'8px'} paddingBlockEnd={'0'} marginBottom={'-6px'}>
<Alert
status="warning"
variant="subtle"
rounded={'0.75rem'}
backgroundColor={'#FFF7ED'}
border={'1px solid #FED7AA'}
fontSize={'0.875rem'}
lineHeight={'1.25rem'}
>
<AlertIcon color={'#EA580C'}>
<Info />
</AlertIcon>
<AlertDescription width={'full'} dangerouslySetInnerHTML={{ __html: sanitizedContent }} />
<CloseButton size={'24px'} alignSelf="center" position="relative" onClick={handleClose}>
<X size={16} />
</CloseButton>
</Alert>
</Box>
);
}
export const GlobalNotification = memo(GlobalNotificationComponent);