From 32d25d9f541b62ca04cc9271c5286e7cb06935cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Tue, 12 Nov 2024 22:12:57 +0100 Subject: [PATCH] pl-fe: add NotificationsAction type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- packages/pl-fe/src/actions/notifications.ts | 52 +++++++++++++------- packages/pl-fe/src/reducers/notifications.ts | 9 +--- packages/pl-fe/src/reducers/user-lists.ts | 4 +- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/packages/pl-fe/src/actions/notifications.ts b/packages/pl-fe/src/actions/notifications.ts index 7e697ebed..75332b199 100644 --- a/packages/pl-fe/src/actions/notifications.ts +++ b/packages/pl-fe/src/actions/notifications.ts @@ -23,7 +23,6 @@ import type { AppDispatch, RootState } from 'pl-fe/store'; const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE' as const; const NOTIFICATIONS_UPDATE_NOOP = 'NOTIFICATIONS_UPDATE_NOOP' as const; -const NOTIFICATIONS_UPDATE_QUEUE = 'NOTIFICATIONS_UPDATE_QUEUE' as const; const NOTIFICATIONS_EXPAND_REQUEST = 'NOTIFICATIONS_EXPAND_REQUEST' as const; const NOTIFICATIONS_EXPAND_SUCCESS = 'NOTIFICATIONS_EXPAND_SUCCESS' as const; @@ -31,13 +30,8 @@ const NOTIFICATIONS_EXPAND_FAIL = 'NOTIFICATIONS_EXPAND_FAIL' as const; const NOTIFICATIONS_FILTER_SET = 'NOTIFICATIONS_FILTER_SET' as const; -const NOTIFICATIONS_CLEAR = 'NOTIFICATIONS_CLEAR' as const; const NOTIFICATIONS_SCROLL_TOP = 'NOTIFICATIONS_SCROLL_TOP' as const; -const NOTIFICATIONS_MARK_READ_REQUEST = 'NOTIFICATIONS_MARK_READ_REQUEST' as const; -const NOTIFICATIONS_MARK_READ_SUCCESS = 'NOTIFICATIONS_MARK_READ_SUCCESS' as const; -const NOTIFICATIONS_MARK_READ_FAIL = 'NOTIFICATIONS_MARK_READ_FAIL' as const; - const MAX_QUEUED_NOTIFICATIONS = 40; const FILTER_TYPES = { @@ -65,6 +59,11 @@ const fetchRelatedRelationships = (dispatch: AppDispatch, notifications: Array (dispatch: AppDispatch) => { const selectedFilter = useSettingsStore.getState().settings.notifications.quickFilter.active; @@ -78,7 +77,7 @@ const updateNotifications = (notification: BaseNotification) => if (showInColumn) { const normalizedNotification = normalizeNotification(notification); - dispatch({ + dispatch({ type: NOTIFICATIONS_UPDATE, notification: normalizedNotification, }); @@ -87,6 +86,11 @@ const updateNotifications = (notification: BaseNotification) => } }; +interface NotificationsUpdateNoopAction { + type: typeof NOTIFICATIONS_UPDATE_NOOP; + meta: { sound: 'boop' }; +} + const updateNotificationsQueue = (notification: BaseNotification, intlMessages: Record, intlLocale: string) => (dispatch: AppDispatch, getState: () => RootState) => { if (!notification.type) return; // drop invalid notifications @@ -130,7 +134,7 @@ const updateNotificationsQueue = (notification: BaseNotification, intlMessages: } if (playSound && !filtered) { - dispatch({ + dispatch({ type: NOTIFICATIONS_UPDATE_NOOP, meta: { sound: 'boop' }, }); @@ -217,15 +221,24 @@ const expandNotificationsFail = (error: unknown) => ({ error, }); +interface NotificationsScrollTopAction { + type: typeof NOTIFICATIONS_SCROLL_TOP; + top: boolean; +} + const scrollTopNotifications = (top: boolean) => (dispatch: AppDispatch) => { - dispatch({ + dispatch(markReadNotifications()); + return dispatch({ type: NOTIFICATIONS_SCROLL_TOP, top, }); - dispatch(markReadNotifications()); }; +interface SetFilterAction { + type: typeof NOTIFICATIONS_FILTER_SET; +} + const setFilter = (filterType: FilterType, abort?: boolean) => (dispatch: AppDispatch) => { const settingsStore = useSettingsStore.getState(); @@ -233,10 +246,10 @@ const setFilter = (filterType: FilterType, abort?: boolean) => settingsStore.changeSetting(['notifications', 'quickFilter', 'active'], filterType); - dispatch({ type: NOTIFICATIONS_FILTER_SET }); dispatch(expandNotifications(undefined, undefined, abort)); - if (activeFilter !== filterType) dispatch(saveSettings()); + + return dispatch({ type: NOTIFICATIONS_FILTER_SET }); }; const markReadNotifications = () => @@ -258,19 +271,23 @@ const markReadNotifications = () => } }; +type NotificationsAction = + | NotificationsUpdateAction + | NotificationsUpdateNoopAction + | ReturnType + | ReturnType + | ReturnType + | NotificationsScrollTopAction + | SetFilterAction; + export { NOTIFICATIONS_UPDATE, NOTIFICATIONS_UPDATE_NOOP, - NOTIFICATIONS_UPDATE_QUEUE, NOTIFICATIONS_EXPAND_REQUEST, NOTIFICATIONS_EXPAND_SUCCESS, NOTIFICATIONS_EXPAND_FAIL, NOTIFICATIONS_FILTER_SET, - NOTIFICATIONS_CLEAR, NOTIFICATIONS_SCROLL_TOP, - NOTIFICATIONS_MARK_READ_REQUEST, - NOTIFICATIONS_MARK_READ_SUCCESS, - NOTIFICATIONS_MARK_READ_FAIL, MAX_QUEUED_NOTIFICATIONS, type FilterType, updateNotifications, @@ -282,4 +299,5 @@ export { scrollTopNotifications, setFilter, markReadNotifications, + type NotificationsAction, }; diff --git a/packages/pl-fe/src/reducers/notifications.ts b/packages/pl-fe/src/reducers/notifications.ts index 7c50f7a48..c325adf5d 100644 --- a/packages/pl-fe/src/reducers/notifications.ts +++ b/packages/pl-fe/src/reducers/notifications.ts @@ -18,9 +18,8 @@ import { NOTIFICATIONS_EXPAND_REQUEST, NOTIFICATIONS_EXPAND_FAIL, NOTIFICATIONS_FILTER_SET, - NOTIFICATIONS_CLEAR, NOTIFICATIONS_SCROLL_TOP, - NOTIFICATIONS_MARK_READ_REQUEST, + type NotificationsAction, } from '../actions/notifications'; import { TIMELINE_DELETE, type TimelineAction } from '../actions/timelines'; @@ -110,7 +109,7 @@ const importMarker = (state: State, marker: Markers) => { }); }; -const notifications = (state: State = ReducerRecord(), action: AccountsAction | AnyAction | TimelineAction) => { +const notifications = (state: State = ReducerRecord(), action: AccountsAction | AnyAction | NotificationsAction | TimelineAction) => { switch (action.type) { case NOTIFICATIONS_EXPAND_REQUEST: return state.set('isLoading', true); @@ -132,10 +131,6 @@ const notifications = (state: State = ReducerRecord(), action: AccountsAction | case FOLLOW_REQUEST_AUTHORIZE_SUCCESS: case FOLLOW_REQUEST_REJECT_SUCCESS: return filterNotificationIds(state, [action.accountId], 'follow_request'); - case NOTIFICATIONS_CLEAR: - return state.set('items', ImmutableOrderedMap()).set('hasMore', false); - case NOTIFICATIONS_MARK_READ_REQUEST: - return state.set('lastRead', action.lastRead); case MARKER_FETCH_SUCCESS: case MARKER_SAVE_REQUEST: case MARKER_SAVE_SUCCESS: diff --git a/packages/pl-fe/src/reducers/user-lists.ts b/packages/pl-fe/src/reducers/user-lists.ts index f0d323a0a..712d96f4a 100644 --- a/packages/pl-fe/src/reducers/user-lists.ts +++ b/packages/pl-fe/src/reducers/user-lists.ts @@ -43,7 +43,7 @@ import { REACTIONS_FETCH_SUCCESS, InteractionsAction, } from 'pl-fe/actions/interactions'; -import { NOTIFICATIONS_UPDATE } from 'pl-fe/actions/notifications'; +import { NOTIFICATIONS_UPDATE, type NotificationsAction } from 'pl-fe/actions/notifications'; import type { Account, EmojiReaction, Notification, PaginatedResponse } from 'pl-api'; import type { APIEntity } from 'pl-fe/types/entities'; @@ -157,7 +157,7 @@ const normalizeFollowRequest = (state: State, notification: Notification) => draft.follow_requests.items = [...new Set([notification.account.id, ...draft.follow_requests.items])]; }); -const userLists = (state = initialState, action: AccountsAction | DirectoryAction | InteractionsAction | AnyAction): State => { +const userLists = (state = initialState, action: AccountsAction | DirectoryAction | InteractionsAction | NotificationsAction | AnyAction): State => { switch (action.type) { case REBLOGS_FETCH_SUCCESS: return normalizeList(state, ['reblogged_by', action.statusId], action.accounts, action.next);