-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationManager.tsx
More file actions
164 lines (150 loc) · 3.95 KB
/
NotificationManager.tsx
File metadata and controls
164 lines (150 loc) · 3.95 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import React, { useState, useEffect } from 'react';
import { useTheme } from '../../context/ThemeContext';
import { SoundType } from '../../../shared/types';
interface NotificationProps {
id: string;
title: string;
message: string;
icon?: string;
onDismiss: (id: string) => void;
onAction?: () => void;
actionText?: string;
}
const Notification: React.FC<NotificationProps> = ({
id,
title,
message,
icon,
onDismiss,
onAction,
actionText
}) => {
const { theme } = useTheme();
const [isVisible, setIsVisible] = useState(true);
// 自动关闭通知
useEffect(() => {
const timer = setTimeout(() => {
setIsVisible(false);
setTimeout(() => onDismiss(id), 300); // 等待动画完成后移除
}, 5000);
return () => clearTimeout(timer);
}, [id, onDismiss]);
return (
<div
className="notification"
style={{
opacity: isVisible ? 1 : 0,
transform: isVisible ? 'translateX(0)' : 'translateX(100%)',
transition: 'opacity 0.3s, transform 0.3s',
border: `2px solid ${theme.primary}`,
backgroundColor: theme.background,
color: theme.textPrimary
}}
>
<div style={{
display: 'flex',
alignItems: 'flex-start',
marginBottom: '10px'
}}>
{icon && (
<div style={{
width: '24px',
height: '24px',
marginRight: '10px',
border: `1px solid ${theme.primary}`,
display: 'flex',
justifyContent: 'center',
alignItems: 'center'
}}>
{icon.charAt(0)}
</div>
)}
<div style={{ flex: 1 }}>
<div style={{
fontWeight: 'bold',
marginBottom: '5px',
textTransform: 'uppercase'
}}>
{title}
</div>
<div>{message}</div>
</div>
<button
onClick={() => {
setIsVisible(false);
setTimeout(() => onDismiss(id), 300);
}}
style={{
background: 'none',
border: 'none',
color: theme.primary,
cursor: 'pointer',
fontSize: '16px',
padding: '0 5px'
}}
>
×
</button>
</div>
{onAction && actionText && (
<button
className="pip-boy-button"
onClick={() => {
onAction();
setIsVisible(false);
setTimeout(() => onDismiss(id), 300);
}}
style={{ width: '100%' }}
>
{actionText}
</button>
)}
</div>
);
};
// 通知管理器
export const NotificationManager = () => {
const [notifications, setNotifications] = useState<any[]>([]);
// 添加通知
const addNotification = (notification: Omit<NotificationProps, 'id' | 'onDismiss'>) => {
const id = Date.now().toString();
setNotifications(prev => [...prev, { ...notification, id }]);
// 播放通知音效
playSound(SoundType.CONFIRM);
return id;
};
// 移除通知
const removeNotification = (id: string) => {
setNotifications(prev => prev.filter(notification => notification.id !== id));
};
// 播放音效
const playSound = (type: SoundType) => {
// 在实际应用中,这里会调用音效系统播放声音
console.log('播放音效:', type);
};
return {
notifications,
addNotification,
removeNotification,
NotificationContainer: () => (
<div style={{
position: 'fixed',
bottom: '20px',
right: '20px',
zIndex: 1000,
display: 'flex',
flexDirection: 'column',
gap: '10px'
}}>
{notifications.map(notification => (
<Notification
key={notification.id}
{...notification}
onDismiss={removeNotification}
/>
))}
</div>
)
};
};
export default NotificationManager;