From 5de47881b73ac75b1b8eed74257e7d75a45df5e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Tue, 19 Nov 2024 14:19:01 +0100 Subject: [PATCH] pl-fe: improve types 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/accounts.ts | 6 +-- packages/pl-fe/src/actions/events.ts | 29 ++++++++++++++ .../pl-fe/src/actions/familiar-followers.ts | 26 ++++++++++-- packages/pl-fe/src/actions/interactions.ts | 4 ++ packages/pl-fe/src/actions/markers.ts | 40 +++++++++++++++---- packages/pl-fe/src/reducers/notifications.ts | 12 +++--- packages/pl-fe/src/reducers/user-lists.ts | 27 ++++++------- 7 files changed, 110 insertions(+), 34 deletions(-) diff --git a/packages/pl-fe/src/actions/accounts.ts b/packages/pl-fe/src/actions/accounts.ts index d9de3e7f0..902e56995 100644 --- a/packages/pl-fe/src/actions/accounts.ts +++ b/packages/pl-fe/src/actions/accounts.ts @@ -495,7 +495,7 @@ const fetchPinnedAccountsRequest = (accountId: string) => ({ accountId, }); -const fetchPinnedAccountsSuccess = (accountId: string, accounts: Array, next: string | null) => ({ +const fetchPinnedAccountsSuccess = (accountId: string, accounts: Array, next: null) => ({ type: PINNED_ACCOUNTS_FETCH_SUCCESS, accountId, accounts, @@ -576,7 +576,7 @@ interface BirthdayRemindersFetchSuccessAction { type: typeof BIRTHDAY_REMINDERS_FETCH_SUCCESS; day: number; month: number; - accountId: Me; + accountId: string; accounts: Array; } @@ -591,7 +591,7 @@ const fetchBirthdayReminders = (month: number, day: number) => (dispatch: AppDispatch, getState: () => RootState) => { if (!isLoggedIn(getState)) return; - const me = getState().me; + const me = getState().me as string; dispatch({ type: BIRTHDAY_REMINDERS_FETCH_REQUEST, day, month, accountId: me }); diff --git a/packages/pl-fe/src/actions/events.ts b/packages/pl-fe/src/actions/events.ts index ce98a8525..abe34dd0a 100644 --- a/packages/pl-fe/src/actions/events.ts +++ b/packages/pl-fe/src/actions/events.ts @@ -489,6 +489,35 @@ const fetchJoinedEvents = () => }; type EventsAction = + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType | ReturnType | EventFormSetAction; diff --git a/packages/pl-fe/src/actions/familiar-followers.ts b/packages/pl-fe/src/actions/familiar-followers.ts index b344728e0..c651abcf6 100644 --- a/packages/pl-fe/src/actions/familiar-followers.ts +++ b/packages/pl-fe/src/actions/familiar-followers.ts @@ -5,12 +5,14 @@ import { getClient } from '../api'; import { fetchRelationships } from './accounts'; import { importEntities } from './importer'; +import type { Account } from 'pl-api'; + const FAMILIAR_FOLLOWERS_FETCH_REQUEST = 'FAMILIAR_FOLLOWERS_FETCH_REQUEST' as const; const FAMILIAR_FOLLOWERS_FETCH_SUCCESS = 'FAMILIAR_FOLLOWERS_FETCH_SUCCESS' as const; const FAMILIAR_FOLLOWERS_FETCH_FAIL = 'FAMILIAR_FOLLOWERS_FETCH_FAIL' as const; const fetchAccountFamiliarFollowers = (accountId: string) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ + dispatch({ type: FAMILIAR_FOLLOWERS_FETCH_REQUEST, accountId, }); @@ -21,13 +23,13 @@ const fetchAccountFamiliarFollowers = (accountId: string) => (dispatch: AppDispa dispatch(importEntities({ accounts })); dispatch(fetchRelationships(accounts.map((item) => item.id))); - dispatch({ + dispatch({ type: FAMILIAR_FOLLOWERS_FETCH_SUCCESS, accountId, accounts, }); }) - .catch(error => dispatch({ + .catch(error => dispatch({ type: FAMILIAR_FOLLOWERS_FETCH_FAIL, accountId, error, @@ -35,9 +37,27 @@ const fetchAccountFamiliarFollowers = (accountId: string) => (dispatch: AppDispa })); }; +type FamiliarFollowersAction = + | { + type: typeof FAMILIAR_FOLLOWERS_FETCH_REQUEST; + accountId: string; + } + | { + type: typeof FAMILIAR_FOLLOWERS_FETCH_SUCCESS; + accountId: string; + accounts: Array; + } + | { + type: typeof FAMILIAR_FOLLOWERS_FETCH_FAIL; + accountId: string; + error: unknown; + skipAlert: true; + } + export { FAMILIAR_FOLLOWERS_FETCH_REQUEST, FAMILIAR_FOLLOWERS_FETCH_SUCCESS, FAMILIAR_FOLLOWERS_FETCH_FAIL, fetchAccountFamiliarFollowers, + type FamiliarFollowersAction, }; diff --git a/packages/pl-fe/src/actions/interactions.ts b/packages/pl-fe/src/actions/interactions.ts index cf45cfa33..16cf7cdb0 100644 --- a/packages/pl-fe/src/actions/interactions.ts +++ b/packages/pl-fe/src/actions/interactions.ts @@ -698,9 +698,13 @@ type InteractionsAction = | ReturnType | ReturnType | ReturnType + | ReturnType + | ReturnType | ReturnType | ReturnType | ReturnType + | ReturnType + | ReturnType | ReturnType | ReturnType | ReturnType diff --git a/packages/pl-fe/src/actions/markers.ts b/packages/pl-fe/src/actions/markers.ts index 743a54744..4941ffa89 100644 --- a/packages/pl-fe/src/actions/markers.ts +++ b/packages/pl-fe/src/actions/markers.ts @@ -1,6 +1,6 @@ import { getClient } from '../api'; -import type { SaveMarkersParams } from 'pl-api'; +import type { Markers, SaveMarkersParams } from 'pl-api'; import type { AppDispatch, RootState } from 'pl-fe/store'; const MARKER_FETCH_REQUEST = 'MARKER_FETCH_REQUEST' as const; @@ -13,24 +13,49 @@ const MARKER_SAVE_FAIL = 'MARKER_SAVE_FAIL' as const; const fetchMarker = (timeline: Array) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: MARKER_FETCH_REQUEST }); + dispatch({ type: MARKER_FETCH_REQUEST }); return getClient(getState).timelines.getMarkers(timeline).then((marker) => { - dispatch({ type: MARKER_FETCH_SUCCESS, marker }); + dispatch({ type: MARKER_FETCH_SUCCESS, marker }); }).catch(error => { - dispatch({ type: MARKER_FETCH_FAIL, error }); + dispatch({ type: MARKER_FETCH_FAIL, error }); }); }; const saveMarker = (marker: SaveMarkersParams) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: MARKER_SAVE_REQUEST, marker }); + dispatch({ type: MARKER_SAVE_REQUEST, marker }); return getClient(getState).timelines.saveMarkers(marker).then((marker) => { - dispatch({ type: MARKER_SAVE_SUCCESS, marker }); + dispatch({ type: MARKER_SAVE_SUCCESS, marker }); }).catch(error => { - dispatch({ type: MARKER_SAVE_FAIL, error }); + dispatch({ type: MARKER_SAVE_FAIL, error }); }); }; +type MarkersAction = + | { + type: typeof MARKER_FETCH_REQUEST; + } + | { + type: typeof MARKER_FETCH_SUCCESS; + marker: Markers; + } + | { + type: typeof MARKER_FETCH_FAIL; + error: unknown; + } + | { + type: typeof MARKER_SAVE_REQUEST; + marker: SaveMarkersParams; + } + | { + type: typeof MARKER_SAVE_SUCCESS; + marker: Markers; + } + | { + type: typeof MARKER_SAVE_FAIL; + error: unknown; + } + export { MARKER_FETCH_REQUEST, MARKER_FETCH_SUCCESS, @@ -40,4 +65,5 @@ export { MARKER_SAVE_FAIL, fetchMarker, saveMarker, + type MarkersAction, }; diff --git a/packages/pl-fe/src/reducers/notifications.ts b/packages/pl-fe/src/reducers/notifications.ts index d0081c631..c67733b32 100644 --- a/packages/pl-fe/src/reducers/notifications.ts +++ b/packages/pl-fe/src/reducers/notifications.ts @@ -9,8 +9,8 @@ import { } from '../actions/accounts'; import { MARKER_FETCH_SUCCESS, - MARKER_SAVE_REQUEST, MARKER_SAVE_SUCCESS, + type MarkersAction, } from '../actions/markers'; import { NOTIFICATIONS_UPDATE, @@ -23,8 +23,7 @@ import { } from '../actions/notifications'; import { TIMELINE_DELETE, type TimelineAction } from '../actions/timelines'; -import type { Notification as BaseNotification, Markers, NotificationGroup, PaginatedResponse, Relationship } from 'pl-api'; -import type { AnyAction } from 'redux'; +import type { GroupedNotificationsResults, Markers, NotificationGroup, PaginatedResponse, Relationship } from 'pl-api'; interface State { items: Array; @@ -75,7 +74,7 @@ const importNotification = (state: State, notification: NotificationGroup) => draft.items = [notification, ...draft.items].toSorted(comparator).filter(filterUnique); }); -const expandNormalizedNotifications = (state: State, notifications: NotificationGroup[], next: (() => Promise>) | null) => +const expandNormalizedNotifications = (state: State, notifications: NotificationGroup[], next: (() => Promise>) | null) => create(state, (draft) => { draft.items = [...notifications, ...draft.items].toSorted(comparator).filter(filterUnique); @@ -122,14 +121,14 @@ const importMarker = (state: State, marker: Markers) => { }); }; -const notifications = (state: State = initialState, action: AccountsAction | AnyAction | NotificationsAction | TimelineAction): State => { +const notifications = (state: State = initialState, action: AccountsAction | MarkersAction | NotificationsAction | TimelineAction): State => { switch (action.type) { case NOTIFICATIONS_EXPAND_REQUEST: return create(state, (draft) => { draft.isLoading = true; }); case NOTIFICATIONS_EXPAND_FAIL: - if (action.error?.message === 'canceled') return state; + if ((action.error as any)?.message === 'canceled') return state; return create(state, (draft) => { draft.isLoading = false; }); @@ -152,7 +151,6 @@ const notifications = (state: State = initialState, action: AccountsAction | Any case FOLLOW_REQUEST_REJECT_SUCCESS: return filterNotificationIds(state, [action.accountId], 'follow_request'); case MARKER_FETCH_SUCCESS: - case MARKER_SAVE_REQUEST: case MARKER_SAVE_SUCCESS: return importMarker(state, action.marker); case TIMELINE_DELETE: diff --git a/packages/pl-fe/src/reducers/user-lists.ts b/packages/pl-fe/src/reducers/user-lists.ts index cafdf8ddc..0d3ddcce5 100644 --- a/packages/pl-fe/src/reducers/user-lists.ts +++ b/packages/pl-fe/src/reducers/user-lists.ts @@ -1,5 +1,4 @@ import { create } from 'mutative'; -import { AnyAction } from 'redux'; import { FOLLOW_REQUESTS_FETCH_SUCCESS, @@ -26,8 +25,9 @@ import { EVENT_PARTICIPATION_REQUESTS_FETCH_SUCCESS, EVENT_PARTICIPATION_REQUEST_AUTHORIZE_SUCCESS, EVENT_PARTICIPATION_REQUEST_REJECT_SUCCESS, + type EventsAction, } from 'pl-fe/actions/events'; -import { FAMILIAR_FOLLOWERS_FETCH_SUCCESS } from 'pl-fe/actions/familiar-followers'; +import { FAMILIAR_FOLLOWERS_FETCH_SUCCESS, type FamiliarFollowersAction } from 'pl-fe/actions/familiar-followers'; import { GROUP_BLOCKS_FETCH_REQUEST, GROUP_BLOCKS_FETCH_SUCCESS, @@ -46,8 +46,7 @@ import { } from 'pl-fe/actions/interactions'; 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'; +import type { Account, NotificationGroup, PaginatedResponse } from 'pl-api'; interface List { next: (() => Promise>) | null; @@ -57,9 +56,9 @@ interface List { interface Reaction { accounts: Array; - count: number; + count: number | null; name: string; - url: string | null; + url: string | undefined; } interface ReactionList { @@ -153,12 +152,12 @@ const removeFromList = (state: State, path: NestedListPath | ListPath, accountId list.items = list.items.filter(item => item !== accountId); }); -const normalizeFollowRequest = (state: State, notification: Notification) => +const normalizeFollowRequest = (state: State, notification: NotificationGroup) => create(state, (draft) => { - draft.follow_requests.items = [...new Set([notification.account.id, ...draft.follow_requests.items])]; + draft.follow_requests.items = [...new Set([...notification.sample_account_ids, ...draft.follow_requests.items])]; }); -const userLists = (state = initialState, action: AccountsAction | DirectoryAction | GroupsAction | InteractionsAction | NotificationsAction | AnyAction): State => { +const userLists = (state = initialState, action: AccountsAction | DirectoryAction | EventsAction | FamiliarFollowersAction | GroupsAction | InteractionsAction | NotificationsAction): State => { switch (action.type) { case REBLOGS_FETCH_SUCCESS: return normalizeList(state, ['reblogged_by', action.statusId], action.accounts, action.next); @@ -173,7 +172,7 @@ const userLists = (state = initialState, action: AccountsAction | DirectoryActio case REACTIONS_FETCH_SUCCESS: return create(state, (draft) => { draft.reactions[action.statusId] = { - items: action.reactions.map((reaction: EmojiReaction) => ({ ...reaction, accounts: reaction.accounts.map(({ id }) => id) })), + items: action.reactions.map((reaction) => ({ ...reaction, accounts: reaction.accounts.map(({ id }) => id) })), next: null, isLoading: false, }; @@ -206,7 +205,7 @@ const userLists = (state = initialState, action: AccountsAction | DirectoryActio case BIRTHDAY_REMINDERS_FETCH_SUCCESS: return normalizeList(state, ['birthday_reminders', action.accountId], action.accounts); case FAMILIAR_FOLLOWERS_FETCH_SUCCESS: - return normalizeList(state, ['familiar_followers', action.accountId], action.accounts, action.next); + return normalizeList(state, ['familiar_followers', action.accountId], action.accounts); case EVENT_PARTICIPATIONS_FETCH_SUCCESS: return normalizeList(state, ['event_participations', action.statusId], action.accounts, action.next); case EVENT_PARTICIPATIONS_EXPAND_SUCCESS: @@ -215,7 +214,7 @@ const userLists = (state = initialState, action: AccountsAction | DirectoryActio return create(state, (draft) => { draft.event_participation_requests[action.statusId] = { next: action.next, - items: action.participations.map(({ account, participation_message }: APIEntity) => ({ + items: action.participations.map(({ account, participation_message }) => ({ account: account.id, participation_message, })), @@ -226,7 +225,7 @@ const userLists = (state = initialState, action: AccountsAction | DirectoryActio return create(state, (draft) => { const list = draft.event_participation_requests[action.statusId]; list.next = action.next; - list.items = [...list.items, ...action.participations.map(({ account, participation_message }: APIEntity) => ({ + list.items = [...list.items, ...action.participations.map(({ account, participation_message }) => ({ account: account.id, participation_message, }))]; @@ -236,7 +235,7 @@ const userLists = (state = initialState, action: AccountsAction | DirectoryActio case EVENT_PARTICIPATION_REQUEST_REJECT_SUCCESS: return create(state, (draft) => { const list = draft.event_participation_requests[action.statusId]; - if (list.items) list.items = list.items.filter(item => item !== action.accountId); + if (list.items) list.items = list.items.filter(item => item.account !== action.accountId); }); case GROUP_BLOCKS_FETCH_SUCCESS: return normalizeList(state, ['group_blocks', action.groupId], action.accounts, action.next);