@umituz/react-native-notifications 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +93 -0
  3. package/lib/index.d.ts +13 -0
  4. package/lib/index.d.ts.map +1 -0
  5. package/lib/index.js +15 -0
  6. package/lib/index.js.map +1 -0
  7. package/lib/infrastructure/config/notificationsConfig.d.ts +20 -0
  8. package/lib/infrastructure/config/notificationsConfig.d.ts.map +1 -0
  9. package/lib/infrastructure/config/notificationsConfig.js +81 -0
  10. package/lib/infrastructure/config/notificationsConfig.js.map +1 -0
  11. package/lib/infrastructure/hooks/actions/useNotificationActions.d.ts +17 -0
  12. package/lib/infrastructure/hooks/actions/useNotificationActions.d.ts.map +1 -0
  13. package/lib/infrastructure/hooks/actions/useNotificationActions.js +141 -0
  14. package/lib/infrastructure/hooks/actions/useNotificationActions.js.map +1 -0
  15. package/lib/infrastructure/hooks/state/useNotificationsState.d.ts +12 -0
  16. package/lib/infrastructure/hooks/state/useNotificationsState.d.ts.map +1 -0
  17. package/lib/infrastructure/hooks/state/useNotificationsState.js +30 -0
  18. package/lib/infrastructure/hooks/state/useNotificationsState.js.map +1 -0
  19. package/lib/infrastructure/hooks/types.d.ts +87 -0
  20. package/lib/infrastructure/hooks/types.d.ts.map +1 -0
  21. package/lib/infrastructure/hooks/types.js +8 -0
  22. package/lib/infrastructure/hooks/types.js.map +1 -0
  23. package/lib/infrastructure/hooks/useNotificationSettings.d.ts +10 -0
  24. package/lib/infrastructure/hooks/useNotificationSettings.d.ts.map +1 -0
  25. package/lib/infrastructure/hooks/useNotificationSettings.js +43 -0
  26. package/lib/infrastructure/hooks/useNotificationSettings.js.map +1 -0
  27. package/lib/infrastructure/hooks/useNotifications.d.ts +23 -0
  28. package/lib/infrastructure/hooks/useNotifications.d.ts.map +1 -0
  29. package/lib/infrastructure/hooks/useNotifications.js +51 -0
  30. package/lib/infrastructure/hooks/useNotifications.js.map +1 -0
  31. package/lib/infrastructure/hooks/utils/useNotificationRefresh.d.ts +13 -0
  32. package/lib/infrastructure/hooks/utils/useNotificationRefresh.d.ts.map +1 -0
  33. package/lib/infrastructure/hooks/utils/useNotificationRefresh.js +82 -0
  34. package/lib/infrastructure/hooks/utils/useNotificationRefresh.js.map +1 -0
  35. package/lib/infrastructure/services/NotificationManager.d.ts +138 -0
  36. package/lib/infrastructure/services/NotificationManager.d.ts.map +1 -0
  37. package/lib/infrastructure/services/NotificationManager.js +284 -0
  38. package/lib/infrastructure/services/NotificationManager.js.map +1 -0
  39. package/lib/infrastructure/services/NotificationService.d.ts +30 -0
  40. package/lib/infrastructure/services/NotificationService.d.ts.map +1 -0
  41. package/lib/infrastructure/services/NotificationService.js +41 -0
  42. package/lib/infrastructure/services/NotificationService.js.map +1 -0
  43. package/lib/infrastructure/services/channels/ChannelManager.d.ts +18 -0
  44. package/lib/infrastructure/services/channels/ChannelManager.d.ts.map +1 -0
  45. package/lib/infrastructure/services/channels/ChannelManager.js +87 -0
  46. package/lib/infrastructure/services/channels/ChannelManager.js.map +1 -0
  47. package/lib/infrastructure/services/delivery/NotificationDelivery.d.ts +16 -0
  48. package/lib/infrastructure/services/delivery/NotificationDelivery.d.ts.map +1 -0
  49. package/lib/infrastructure/services/delivery/NotificationDelivery.js +57 -0
  50. package/lib/infrastructure/services/delivery/NotificationDelivery.js.map +1 -0
  51. package/lib/infrastructure/services/preferences/PreferencesManager.d.ts +18 -0
  52. package/lib/infrastructure/services/preferences/PreferencesManager.d.ts.map +1 -0
  53. package/lib/infrastructure/services/preferences/PreferencesManager.js +65 -0
  54. package/lib/infrastructure/services/preferences/PreferencesManager.js.map +1 -0
  55. package/lib/infrastructure/services/types.d.ts +89 -0
  56. package/lib/infrastructure/services/types.d.ts.map +1 -0
  57. package/lib/infrastructure/services/types.js +7 -0
  58. package/lib/infrastructure/services/types.js.map +1 -0
  59. package/lib/infrastructure/storage/NotificationsStore.d.ts +23 -0
  60. package/lib/infrastructure/storage/NotificationsStore.d.ts.map +1 -0
  61. package/lib/infrastructure/storage/NotificationsStore.js +25 -0
  62. package/lib/infrastructure/storage/NotificationsStore.js.map +1 -0
  63. package/package.json +62 -0
  64. package/src/index.ts +34 -0
  65. package/src/infrastructure/config/notificationsConfig.ts +98 -0
  66. package/src/infrastructure/hooks/actions/useNotificationActions.ts +233 -0
  67. package/src/infrastructure/hooks/state/useNotificationsState.ts +46 -0
  68. package/src/infrastructure/hooks/types.ts +83 -0
  69. package/src/infrastructure/hooks/useNotificationSettings.ts +45 -0
  70. package/src/infrastructure/hooks/useNotifications.ts +70 -0
  71. package/src/infrastructure/hooks/utils/useNotificationRefresh.ts +107 -0
  72. package/src/infrastructure/services/NotificationManager.ts +326 -0
  73. package/src/infrastructure/services/NotificationService.ts +50 -0
  74. package/src/infrastructure/services/channels/ChannelManager.ts +111 -0
  75. package/src/infrastructure/services/delivery/NotificationDelivery.ts +65 -0
  76. package/src/infrastructure/services/preferences/PreferencesManager.ts +77 -0
  77. package/src/infrastructure/services/types.ts +81 -0
  78. package/src/infrastructure/storage/NotificationsStore.ts +39 -0
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Notifications Configuration
3
+ * Defines notification settings structure
4
+ */
5
+
6
+ export interface NotificationSetting {
7
+ id: string;
8
+ titleKey: string;
9
+ descKey: string;
10
+ icon: string;
11
+ }
12
+
13
+ export interface NotificationSection {
14
+ id: string;
15
+ titleKey: string;
16
+ settings: NotificationSetting[];
17
+ }
18
+
19
+ export interface NotificationsConfig {
20
+ sections: NotificationSection[];
21
+ }
22
+
23
+ export const notificationsConfig: NotificationsConfig = {
24
+ sections: [
25
+ {
26
+ id: 'channels',
27
+ titleKey: 'notifications.channels',
28
+ settings: [
29
+ {
30
+ id: 'pushNotifications',
31
+ titleKey: 'notifications.push',
32
+ descKey: 'notifications.pushDesc',
33
+ icon: 'notifications',
34
+ },
35
+ {
36
+ id: 'emailNotifications',
37
+ titleKey: 'notifications.email',
38
+ descKey: 'notifications.emailDesc',
39
+ icon: 'email',
40
+ },
41
+ {
42
+ id: 'smsNotifications',
43
+ titleKey: 'notifications.sms',
44
+ descKey: 'notifications.smsDesc',
45
+ icon: 'message',
46
+ },
47
+ ],
48
+ },
49
+ {
50
+ id: 'content',
51
+ titleKey: 'notifications.content',
52
+ settings: [
53
+ {
54
+ id: 'appUpdates',
55
+ titleKey: 'notifications.updates',
56
+ descKey: 'notifications.updatesDesc',
57
+ icon: 'update',
58
+ },
59
+ {
60
+ id: 'features',
61
+ titleKey: 'notifications.features',
62
+ descKey: 'notifications.featuresDesc',
63
+ icon: 'new-releases',
64
+ },
65
+ {
66
+ id: 'tips',
67
+ titleKey: 'notifications.tips',
68
+ descKey: 'notifications.tipsDesc',
69
+ icon: 'tips-and-updates',
70
+ },
71
+ ],
72
+ },
73
+ {
74
+ id: 'activity',
75
+ titleKey: 'notifications.activity',
76
+ settings: [
77
+ {
78
+ id: 'reminders',
79
+ titleKey: 'notifications.reminders',
80
+ descKey: 'notifications.remindersDesc',
81
+ icon: 'alarm',
82
+ },
83
+ {
84
+ id: 'achievements',
85
+ titleKey: 'notifications.achievements',
86
+ descKey: 'notifications.achievementsDesc',
87
+ icon: 'emoji-events',
88
+ },
89
+ {
90
+ id: 'social',
91
+ titleKey: 'notifications.social',
92
+ descKey: 'notifications.socialDesc',
93
+ icon: 'people',
94
+ },
95
+ ],
96
+ },
97
+ ],
98
+ };
@@ -0,0 +1,233 @@
1
+ import { useCallback } from 'react';
2
+ import AsyncStorage from '@react-native-async-storage/async-storage';
3
+ import * as Notifications from 'expo-notifications';
4
+ import type {
5
+ SendNotificationOptions,
6
+ Notification,
7
+ NotificationChannel,
8
+ NotificationPreferences,
9
+ } from '../types';
10
+ import { NotificationDelivery } from '../../services/delivery/NotificationDelivery';
11
+ import { ChannelManager } from '../../services/channels/ChannelManager';
12
+ import { PreferencesManager } from '../../services/preferences/PreferencesManager';
13
+
14
+ /**
15
+ * useNotificationActions - Offline Notification Actions
16
+ *
17
+ * All actions use AsyncStorage and expo-notifications.
18
+ * NO backend - pure offline.
19
+ */
20
+ export const useNotificationActions = (state: any, setters: any) => {
21
+ const {
22
+ setNotifications,
23
+ setUnreadCount,
24
+ setChannels,
25
+ setPreferences,
26
+ setError,
27
+ } = setters;
28
+
29
+ const notificationDelivery = new NotificationDelivery();
30
+ const channelManager = new ChannelManager();
31
+ const preferencesManager = new PreferencesManager();
32
+
33
+ const sendNotification = useCallback(
34
+ async (options: SendNotificationOptions): Promise<Notification[]> => {
35
+ try {
36
+ setError(null);
37
+
38
+ // Create notification
39
+ const notification: Notification = {
40
+ id: `notif_${Date.now()}`,
41
+ title: options.title,
42
+ body: options.body,
43
+ data: options.data,
44
+ scheduled_for: options.scheduled_for?.toISOString(),
45
+ created_at: new Date().toISOString(),
46
+ read: false,
47
+ };
48
+
49
+ // Save to AsyncStorage
50
+ const data = await AsyncStorage.getItem('@notifications:list');
51
+ const notifications: Notification[] = data ? JSON.parse(data) : [];
52
+ notifications.unshift(notification);
53
+ await AsyncStorage.setItem(
54
+ '@notifications:list',
55
+ JSON.stringify(notifications)
56
+ );
57
+
58
+ // Deliver using expo-notifications
59
+ await notificationDelivery.deliver(notification);
60
+
61
+ return [notification];
62
+ } catch (err) {
63
+ setError(
64
+ err instanceof Error ? err.message : 'Failed to send notification'
65
+ );
66
+ return [];
67
+ }
68
+ },
69
+ [setError]
70
+ );
71
+
72
+ const markAsRead = useCallback(
73
+ async (notificationId: string): Promise<boolean> => {
74
+ try {
75
+ const data = await AsyncStorage.getItem('@notifications:list');
76
+ const notifications: Notification[] = data ? JSON.parse(data) : [];
77
+
78
+ const updated = notifications.map((n) =>
79
+ n.id === notificationId ? { ...n, read: true } : n
80
+ );
81
+
82
+ await AsyncStorage.setItem(
83
+ '@notifications:list',
84
+ JSON.stringify(updated)
85
+ );
86
+
87
+ setNotifications((prev: Notification[]) =>
88
+ prev.map((n) =>
89
+ n.id === notificationId ? { ...n, read: true } : n
90
+ )
91
+ );
92
+ setUnreadCount((prev: number) => Math.max(0, prev - 1));
93
+
94
+ return true;
95
+ } catch (err) {
96
+ setError(
97
+ err instanceof Error ? err.message : 'Failed to mark as read'
98
+ );
99
+ return false;
100
+ }
101
+ },
102
+ [setNotifications, setUnreadCount, setError]
103
+ );
104
+
105
+ const markAllAsRead = useCallback(async (): Promise<boolean> => {
106
+ try {
107
+ const data = await AsyncStorage.getItem('@notifications:list');
108
+ const notifications: Notification[] = data ? JSON.parse(data) : [];
109
+
110
+ const updated = notifications.map((n) => ({ ...n, read: true }));
111
+
112
+ await AsyncStorage.setItem('@notifications:list', JSON.stringify(updated));
113
+
114
+ setNotifications((prev: Notification[]) =>
115
+ prev.map((n) => ({ ...n, read: true }))
116
+ );
117
+ setUnreadCount(0);
118
+
119
+ return true;
120
+ } catch (err) {
121
+ setError(
122
+ err instanceof Error ? err.message : 'Failed to mark all as read'
123
+ );
124
+ return false;
125
+ }
126
+ }, [setNotifications, setUnreadCount, setError]);
127
+
128
+ const deleteNotification = useCallback(
129
+ async (notificationId: string): Promise<boolean> => {
130
+ try {
131
+ const data = await AsyncStorage.getItem('@notifications:list');
132
+ const notifications: Notification[] = data ? JSON.parse(data) : [];
133
+
134
+ const deleted = notifications.find((n) => n.id === notificationId);
135
+ const filtered = notifications.filter((n) => n.id !== notificationId);
136
+
137
+ await AsyncStorage.setItem(
138
+ '@notifications:list',
139
+ JSON.stringify(filtered)
140
+ );
141
+
142
+ setNotifications((prev: Notification[]) =>
143
+ prev.filter((n) => n.id !== notificationId)
144
+ );
145
+
146
+ if (deleted && !deleted.read) {
147
+ setUnreadCount((prev: number) => Math.max(0, prev - 1));
148
+ }
149
+
150
+ return true;
151
+ } catch (err) {
152
+ setError(
153
+ err instanceof Error ? err.message : 'Failed to delete notification'
154
+ );
155
+ return false;
156
+ }
157
+ },
158
+ [setNotifications, setUnreadCount, setError]
159
+ );
160
+
161
+ const registerChannel = useCallback(
162
+ async (
163
+ channelType: 'push' | 'in_app',
164
+ preferences: Record<string, any> = {}
165
+ ): Promise<NotificationChannel | null> => {
166
+ try {
167
+ const channel = await channelManager.register(channelType, preferences);
168
+ if (channel) {
169
+ setChannels((prev: NotificationChannel[]) => [...prev, channel]);
170
+ }
171
+ return channel;
172
+ } catch (err) {
173
+ setError(
174
+ err instanceof Error ? err.message : 'Failed to register channel'
175
+ );
176
+ return null;
177
+ }
178
+ },
179
+ [setChannels, setError]
180
+ );
181
+
182
+ const verifyChannel = useCallback(
183
+ async (channelId: string): Promise<boolean> => {
184
+ try {
185
+ const success = await channelManager.verify(channelId);
186
+ if (success) {
187
+ setChannels((prev: NotificationChannel[]) =>
188
+ prev.map((c) =>
189
+ c.id === channelId ? { ...c, is_verified: true } : c
190
+ )
191
+ );
192
+ }
193
+ return success;
194
+ } catch (err) {
195
+ setError(
196
+ err instanceof Error ? err.message : 'Failed to verify channel'
197
+ );
198
+ return false;
199
+ }
200
+ },
201
+ [setChannels, setError]
202
+ );
203
+
204
+ const updatePreferences = useCallback(
205
+ async (newPreferences: Partial<NotificationPreferences>): Promise<boolean> => {
206
+ try {
207
+ const success = await preferencesManager.update(newPreferences);
208
+ if (success) {
209
+ setPreferences((prev: NotificationPreferences | null) =>
210
+ prev ? { ...prev, ...newPreferences } : null
211
+ );
212
+ }
213
+ return success;
214
+ } catch (err) {
215
+ setError(
216
+ err instanceof Error ? err.message : 'Failed to update preferences'
217
+ );
218
+ return false;
219
+ }
220
+ },
221
+ [setPreferences, setError]
222
+ );
223
+
224
+ return {
225
+ sendNotification,
226
+ markAsRead,
227
+ markAllAsRead,
228
+ deleteNotification,
229
+ registerChannel,
230
+ verifyChannel,
231
+ updatePreferences,
232
+ };
233
+ };
@@ -0,0 +1,46 @@
1
+ import { useState } from 'react';
2
+ import type { NotificationsState } from '../types';
3
+
4
+ export const useNotificationsState = () => {
5
+ const [state, setState] = useState<NotificationsState>({
6
+ notifications: [],
7
+ channels: [],
8
+ unreadCount: 0,
9
+ preferences: null,
10
+ loading: false,
11
+ error: null,
12
+ hasMore: true,
13
+ });
14
+
15
+ const setNotifications = (notifications: NotificationsState['notifications']) =>
16
+ setState(prev => ({ ...prev, notifications }));
17
+
18
+ const setChannels = (channels: NotificationsState['channels']) =>
19
+ setState(prev => ({ ...prev, channels }));
20
+
21
+ const setUnreadCount = (unreadCount: number) =>
22
+ setState(prev => ({ ...prev, unreadCount }));
23
+
24
+ const setPreferences = (preferences: NotificationsState['preferences']) =>
25
+ setState(prev => ({ ...prev, preferences }));
26
+
27
+ const setLoading = (loading: boolean) =>
28
+ setState(prev => ({ ...prev, loading }));
29
+
30
+ const setError = (error: string | null) =>
31
+ setState(prev => ({ ...prev, error }));
32
+
33
+ const setHasMore = (hasMore: boolean) =>
34
+ setState(prev => ({ ...prev, hasMore }));
35
+
36
+ return {
37
+ state,
38
+ setNotifications,
39
+ setChannels,
40
+ setUnreadCount,
41
+ setPreferences,
42
+ setLoading,
43
+ setError,
44
+ setHasMore,
45
+ };
46
+ };
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Notification Hook Types - Offline-First
3
+ *
4
+ * All types for offline local notifications.
5
+ * NO backend dependencies.
6
+ */
7
+
8
+ /**
9
+ * Local notification entity
10
+ */
11
+ export interface Notification {
12
+ id: string;
13
+ title: string;
14
+ body: string;
15
+ data?: Record<string, any>;
16
+ scheduled_for?: string;
17
+ created_at: string;
18
+ read: boolean;
19
+ }
20
+
21
+ /**
22
+ * Notification channel (push or in-app only)
23
+ */
24
+ export interface NotificationChannel {
25
+ id: string;
26
+ channel_type: 'push' | 'in_app';
27
+ channel_address: string;
28
+ preferences: Record<string, any>;
29
+ is_verified: boolean;
30
+ is_active: boolean;
31
+ created_at: string;
32
+ }
33
+
34
+ /**
35
+ * Notification preferences
36
+ */
37
+ export interface NotificationPreferences {
38
+ push_enabled: boolean;
39
+ quiet_hours: {
40
+ enabled: boolean;
41
+ start_time: string;
42
+ end_time: string;
43
+ timezone: string;
44
+ };
45
+ categories: {
46
+ reminders: { push: boolean; in_app: boolean };
47
+ updates: { push: boolean; in_app: boolean };
48
+ alerts: { push: boolean; in_app: boolean };
49
+ };
50
+ }
51
+
52
+ /**
53
+ * Options for sending notification
54
+ */
55
+ export interface SendNotificationOptions {
56
+ title: string;
57
+ body: string;
58
+ data?: Record<string, any>;
59
+ scheduled_for?: Date;
60
+ category?: 'reminders' | 'updates' | 'alerts';
61
+ }
62
+
63
+ /**
64
+ * State for useNotifications hook
65
+ */
66
+ export interface NotificationsState {
67
+ notifications: Notification[];
68
+ channels: NotificationChannel[];
69
+ unreadCount: number;
70
+ preferences: NotificationPreferences | null;
71
+ loading: boolean;
72
+ error: string | null;
73
+ hasMore: boolean;
74
+ }
75
+
76
+ /**
77
+ * Options for useNotifications hook
78
+ */
79
+ export interface UseNotificationsOptions {
80
+ autoRefresh?: boolean;
81
+ refreshInterval?: number;
82
+ pageSize?: number;
83
+ }
@@ -0,0 +1,45 @@
1
+ import { useState, useEffect } from 'react';
2
+ import AsyncStorage from '@react-native-async-storage/async-storage';
3
+
4
+ const STORAGE_KEY = 'notifications_enabled';
5
+
6
+ /**
7
+ * Simple notification settings hook
8
+ * Manages a single toggle for enabling/disabling notifications
9
+ */
10
+ export const useNotificationSettings = () => {
11
+ const [notificationsEnabled, setNotificationsEnabled] = useState(true);
12
+ const [isLoading, setIsLoading] = useState(true);
13
+
14
+ useEffect(() => {
15
+ const loadSettings = async () => {
16
+ try {
17
+ const value = await AsyncStorage.getItem(STORAGE_KEY);
18
+ if (value !== null) {
19
+ setNotificationsEnabled(value === 'true');
20
+ }
21
+ } catch (error) {
22
+ // Silent failure - use default value
23
+ } finally {
24
+ setIsLoading(false);
25
+ }
26
+ };
27
+
28
+ loadSettings();
29
+ }, []);
30
+
31
+ const toggleNotifications = async (value: boolean) => {
32
+ setNotificationsEnabled(value);
33
+ try {
34
+ await AsyncStorage.setItem(STORAGE_KEY, value.toString());
35
+ } catch (error) {
36
+ // Silent failure
37
+ }
38
+ };
39
+
40
+ return {
41
+ notificationsEnabled,
42
+ setNotificationsEnabled: toggleNotifications,
43
+ isLoading,
44
+ };
45
+ };
@@ -0,0 +1,70 @@
1
+ import { useEffect } from 'react';
2
+ import { useNotificationsState } from './state/useNotificationsState';
3
+ import { useNotificationActions } from './actions/useNotificationActions';
4
+ import { useNotificationRefresh } from './utils/useNotificationRefresh';
5
+ import type { UseNotificationsOptions } from './types';
6
+
7
+ export * from './types';
8
+
9
+ export function useNotifications(userId: string, options: UseNotificationsOptions = {}) {
10
+ const { autoRefresh = false, refreshInterval = 30000, pageSize = 20 } = options;
11
+
12
+ const {
13
+ state,
14
+ setNotifications,
15
+ setChannels,
16
+ setUnreadCount,
17
+ setPreferences,
18
+ setLoading,
19
+ setError,
20
+ setHasMore,
21
+ } = useNotificationsState();
22
+
23
+ const setters = {
24
+ setNotifications,
25
+ setChannels,
26
+ setUnreadCount,
27
+ setPreferences,
28
+ setLoading,
29
+ setError,
30
+ setHasMore,
31
+ };
32
+
33
+ const actions = useNotificationActions(state, setters);
34
+ const refresh = useNotificationRefresh(pageSize, setters);
35
+
36
+ const loadMoreNotifications = () =>
37
+ refresh.loadMoreNotifications(state.notifications.length, state.hasMore, state.loading);
38
+
39
+ // Load initial data
40
+ useEffect(() => {
41
+ if (userId) {
42
+ refresh.refreshNotifications();
43
+ refresh.refreshChannels();
44
+ refresh.refreshPreferences();
45
+ } else {
46
+ setNotifications([]);
47
+ setChannels([]);
48
+ setUnreadCount(0);
49
+ setPreferences(null);
50
+ }
51
+ }, [userId]);
52
+
53
+ // Auto-refresh setup
54
+ useEffect(() => {
55
+ if (!autoRefresh || !userId) return;
56
+
57
+ const interval = setInterval(() => {
58
+ refresh.refreshNotifications();
59
+ }, refreshInterval);
60
+
61
+ return () => clearInterval(interval);
62
+ }, [autoRefresh, userId, refreshInterval]);
63
+
64
+ return {
65
+ ...state,
66
+ ...actions,
67
+ ...refresh,
68
+ loadMoreNotifications,
69
+ };
70
+ }
@@ -0,0 +1,107 @@
1
+ import { useCallback } from 'react';
2
+ import AsyncStorage from '@react-native-async-storage/async-storage';
3
+ import type { Notification } from '../types';
4
+ import { ChannelManager } from '../../services/channels/ChannelManager';
5
+ import { PreferencesManager } from '../../services/preferences/PreferencesManager';
6
+
7
+ /**
8
+ * useNotificationRefresh - Offline Notification Refresh
9
+ *
10
+ * Uses AsyncStorage for local data.
11
+ * NO backend - pure offline.
12
+ */
13
+ export const useNotificationRefresh = (pageSize: number, setters: any) => {
14
+ const {
15
+ setNotifications,
16
+ setUnreadCount,
17
+ setChannels,
18
+ setPreferences,
19
+ setLoading,
20
+ setError,
21
+ setHasMore,
22
+ } = setters;
23
+
24
+ const channelManager = new ChannelManager();
25
+ const preferencesManager = new PreferencesManager();
26
+
27
+ const refreshNotifications = useCallback(async () => {
28
+ try {
29
+ setLoading(true);
30
+ setError(null);
31
+
32
+ // Load from AsyncStorage
33
+ const data = await AsyncStorage.getItem('@notifications:list');
34
+ const allNotifications: Notification[] = data ? JSON.parse(data) : [];
35
+
36
+ // Paginate
37
+ const paginated = allNotifications.slice(0, pageSize);
38
+ const unread = allNotifications.filter((n) => !n.read).length;
39
+
40
+ setNotifications(paginated);
41
+ setUnreadCount(unread);
42
+ setHasMore(allNotifications.length > pageSize);
43
+ } catch (err) {
44
+ setError(
45
+ err instanceof Error ? err.message : 'Failed to load notifications'
46
+ );
47
+ } finally {
48
+ setLoading(false);
49
+ }
50
+ }, [pageSize, setNotifications, setUnreadCount, setHasMore, setLoading, setError]);
51
+
52
+ const loadMoreNotifications = useCallback(
53
+ async (currentLength: number, hasMore: boolean, loading: boolean) => {
54
+ if (!hasMore || loading) return;
55
+
56
+ try {
57
+ setLoading(true);
58
+ setError(null);
59
+
60
+ const data = await AsyncStorage.getItem('@notifications:list');
61
+ const allNotifications: Notification[] = data ? JSON.parse(data) : [];
62
+
63
+ const moreNotifications = allNotifications.slice(
64
+ currentLength,
65
+ currentLength + pageSize
66
+ );
67
+
68
+ setNotifications((prev: any[]) => [...prev, ...moreNotifications]);
69
+ setHasMore(allNotifications.length > currentLength + pageSize);
70
+ } catch (err) {
71
+ setError(
72
+ err instanceof Error
73
+ ? err.message
74
+ : 'Failed to load more notifications'
75
+ );
76
+ } finally {
77
+ setLoading(false);
78
+ }
79
+ },
80
+ [pageSize, setNotifications, setHasMore, setLoading, setError]
81
+ );
82
+
83
+ const refreshChannels = useCallback(async () => {
84
+ try {
85
+ const channelsData = await channelManager.getActiveChannels();
86
+ setChannels(channelsData);
87
+ } catch (err) {
88
+ // Silent failure
89
+ }
90
+ }, [setChannels]);
91
+
92
+ const refreshPreferences = useCallback(async () => {
93
+ try {
94
+ const prefsData = await preferencesManager.get();
95
+ setPreferences(prefsData);
96
+ } catch (err) {
97
+ // Silent failure
98
+ }
99
+ }, [setPreferences]);
100
+
101
+ return {
102
+ refreshNotifications,
103
+ loadMoreNotifications,
104
+ refreshChannels,
105
+ refreshPreferences,
106
+ };
107
+ };