|
1 |
| -/** |
2 |
| - * Communities notif handler |
3 |
| - */ |
| 1 | +import { CommonActions } from '@react-navigation/native'; |
4 | 2 |
|
5 |
| -import { registerNotifHandlers } from '~/framework/util/notifications/routing'; |
| 3 | +import { communitiesRouteNames } from './navigation'; |
6 | 4 |
|
7 |
| -export default () => registerNotifHandlers([]); |
| 5 | +import timelineModuleConfig from '~/framework/modules/timeline/module-config'; |
| 6 | +import { computeTabRouteName } from '~/framework/navigation/tabModules'; |
| 7 | +import { getAsResourceUriNotification } from '~/framework/util/notifications'; |
| 8 | +import { |
| 9 | + handleNotificationNavigationAction, |
| 10 | + NotifHandlerThunkAction, |
| 11 | + registerNotifHandlers, |
| 12 | +} from '~/framework/util/notifications/routing'; |
| 13 | + |
| 14 | +const COMMUNITY_ID_REGEX = /\/community\/id\/([a-f0-9-]+)/i; |
| 15 | + |
| 16 | +const handleCommunitiesNotificationAction: NotifHandlerThunkAction = |
| 17 | + (notification, allowSwitchTab) => async (dispatch, getState) => { |
| 18 | + try { |
| 19 | + const communityNotif = getAsResourceUriNotification(notification); |
| 20 | + if (!communityNotif) return { managed: 0 }; |
| 21 | + |
| 22 | + const communityUri = communityNotif?.resource.uri; |
| 23 | + const communityId = communityUri?.match(COMMUNITY_ID_REGEX)?.[1]; |
| 24 | + if (!communityId) return { managed: 0 }; |
| 25 | + |
| 26 | + const navAction = CommonActions.navigate( |
| 27 | + allowSwitchTab |
| 28 | + ? { |
| 29 | + name: computeTabRouteName(timelineModuleConfig.routeName), |
| 30 | + params: { |
| 31 | + initial: false, |
| 32 | + params: { |
| 33 | + fromTimeline: false, |
| 34 | + notification: communityNotif, |
| 35 | + resourceId: communityId, |
| 36 | + }, |
| 37 | + screen: communitiesRouteNames.home, |
| 38 | + }, |
| 39 | + } |
| 40 | + : { |
| 41 | + name: communitiesRouteNames.home, |
| 42 | + params: { |
| 43 | + fromTimeline: true, |
| 44 | + id: communityId, |
| 45 | + notification: communityNotif, |
| 46 | + }, |
| 47 | + }, |
| 48 | + ); |
| 49 | + |
| 50 | + handleNotificationNavigationAction(navAction); |
| 51 | + |
| 52 | + return { |
| 53 | + managed: 1, |
| 54 | + trackInfo: { action: 'Communities', name: `${notification.type}.${notification['event-type']}` }, |
| 55 | + }; |
| 56 | + } catch { |
| 57 | + return { managed: 0 }; |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | +export default () => |
| 62 | + registerNotifHandlers([ |
| 63 | + { |
| 64 | + 'event-type': [ |
| 65 | + 'add_member', |
| 66 | + 'announcement_new', |
| 67 | + 'course_page_new', |
| 68 | + 'discussion_message_new', |
| 69 | + 'join_request_new', |
| 70 | + 'join_request_pending', |
| 71 | + 'join_request_accepted', |
| 72 | + 'join_request_rejected', |
| 73 | + 'resource_new', |
| 74 | + ], |
| 75 | + 'notifHandlerAction': handleCommunitiesNotificationAction, |
| 76 | + 'type': 'communities', |
| 77 | + }, |
| 78 | + ]); |
0 commit comments