|
| 1 | +import React, { useEffect } from 'react'; |
| 2 | +import Moment from 'moment'; |
| 3 | +import { notification, Button, Space } from 'coding-oa-uikit'; |
| 4 | +import { ArgsProps } from 'coding-oa-uikit/lib/notification'; |
| 5 | + |
| 6 | +/** 通知存储数据结构 */ |
| 7 | +interface NotifyStorageData { |
| 8 | + /** 不再提醒 */ |
| 9 | + neverRemind: boolean; |
| 10 | + /** 过期时间 */ |
| 11 | + expireTimestamp?: number; |
| 12 | +} |
| 13 | + |
| 14 | +/** 获取在LocalStorage的通知数据 */ |
| 15 | +const getNotificationDataByLocalStorage = (key: string) => { |
| 16 | + const keyValue = localStorage.getItem(key); |
| 17 | + if (keyValue) { |
| 18 | + try { |
| 19 | + const keyObj: NotifyStorageData = JSON.parse(keyValue); |
| 20 | + return keyObj; |
| 21 | + } catch (error) { |
| 22 | + // 解析失败 |
| 23 | + } |
| 24 | + } |
| 25 | + return null; |
| 26 | +}; |
| 27 | + |
| 28 | +/** 设置通知数据到LocalStorage */ |
| 29 | +const setNotificationDataToLocalStorage = (key: string, notifyStorageData: NotifyStorageData) => { |
| 30 | + localStorage.setItem(key, JSON.stringify(notifyStorageData)); |
| 31 | +}; |
| 32 | + |
| 33 | +/** 通知入参数数据结构 */ |
| 34 | +interface NotificationProps { |
| 35 | + /** 唯一标识 */ |
| 36 | + key: string, |
| 37 | + /** 通知类型 */ |
| 38 | + type: 'info' | 'success' | 'error' | 'warning', |
| 39 | + /** 是否开启过期操作 */ |
| 40 | + useExpireOper?: boolean; |
| 41 | + /** 通知其他参数 */ |
| 42 | + notifyArgs: Omit<ArgsProps, 'key'> |
| 43 | +} |
| 44 | + |
| 45 | +/** 通知hooks */ |
| 46 | +const useNotification = ({ key, type, useExpireOper = false, notifyArgs }: NotificationProps) => { |
| 47 | + const { btn, ...rest } = notifyArgs; |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (useExpireOper) { |
| 51 | + const notifyStorageData = getNotificationDataByLocalStorage(key); |
| 52 | + if (notifyStorageData?.neverRemind |
| 53 | + || (notifyStorageData?.expireTimestamp && Moment().valueOf() < notifyStorageData.expireTimestamp) |
| 54 | + ) { |
| 55 | + return; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + notification[type]({ |
| 60 | + duration: null, |
| 61 | + btn: useExpireOper ? <Space> |
| 62 | + <Button type="primary" size="small" onClick={() => { |
| 63 | + // 明日凌点时间戳 |
| 64 | + const timestamp = Moment().startOf('day') |
| 65 | + .add(1, 'day') |
| 66 | + .valueOf(); |
| 67 | + const notifyStorageData: NotifyStorageData = { |
| 68 | + neverRemind: false, |
| 69 | + expireTimestamp: timestamp, |
| 70 | + }; |
| 71 | + setNotificationDataToLocalStorage(key, notifyStorageData); |
| 72 | + notification.close(key); |
| 73 | + }}> |
| 74 | + 今日不再提醒 |
| 75 | + </Button> |
| 76 | + <Button type="secondary" size="small" onClick={() => { |
| 77 | + const notifyStorageData: NotifyStorageData = { neverRemind: true }; |
| 78 | + setNotificationDataToLocalStorage(key, notifyStorageData); |
| 79 | + notification.close(key); |
| 80 | + }}> |
| 81 | + 不再提醒 |
| 82 | + </Button> |
| 83 | + </Space> : btn, |
| 84 | + ...rest, |
| 85 | + key, |
| 86 | + }); |
| 87 | + }, []); |
| 88 | +}; |
| 89 | + |
| 90 | +export default useNotification; |
0 commit comments