From 5efacc274f2b75c4932626f5696d42511acda7b2 Mon Sep 17 00:00:00 2001 From: mkljczk Date: Wed, 4 Dec 2024 19:46:48 +0100 Subject: [PATCH] pl-fe: remove unused Signed-off-by: mkljczk --- .../account-lists/use-follow-requests.ts | 17 +++++++++--- .../pl-fe/src/components/sidebar-menu.tsx | 3 ++- .../src/components/sidebar-navigation.tsx | 3 ++- packages/pl-fe/src/reducers/user-lists.ts | 26 ++++--------------- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/packages/pl-fe/src/api/hooks/account-lists/use-follow-requests.ts b/packages/pl-fe/src/api/hooks/account-lists/use-follow-requests.ts index 55c621fc7..f3554d496 100644 --- a/packages/pl-fe/src/api/hooks/account-lists/use-follow-requests.ts +++ b/packages/pl-fe/src/api/hooks/account-lists/use-follow-requests.ts @@ -23,7 +23,7 @@ const removeFollowRequest = (accountId: string) => pages: data.pages.map(({ items, ...page }) => ({ ...page, items: items.filter((id) => id !== accountId) })), } : undefined); -const useFollowRequests = () => { +const makeUseFollowRequests = (select: ((data: InfiniteData, PaginatedResponse>) => T)) => () => { const client = useClient(); return useInfiniteQuery({ @@ -31,10 +31,14 @@ const useFollowRequests = () => { queryFn: ({ pageParam }) => pageParam.next?.() || client.myAccount.getFollowRequests().then(minifyAccountList), initialPageParam: { previous: null, next: null, items: [], partial: false } as PaginatedResponse, getNextPageParam: (page) => page.next ? page : undefined, - select: (data) => data.pages.map(page => page.items).flat(), + select, }); }; +const useFollowRequests = makeUseFollowRequests((data) => data.pages.map(page => page.items).flat()); + +const useFollowRequestsCount = makeUseFollowRequests((data) => data.pages.map(page => page.items).flat().length); + const useAcceptFollowRequestMutation = (accountId: string) => { const client = useClient(); @@ -61,4 +65,11 @@ const prefetchFollowRequests = (client: PlApiClient) => queryClient.prefetchInfi initialPageParam: { previous: null, next: null, items: [], partial: false } as PaginatedResponse, }); -export { appendFollowRequest, useFollowRequests, useAcceptFollowRequestMutation, useRejectFollowRequestMutation, prefetchFollowRequests }; +export { + appendFollowRequest, + useFollowRequests, + useFollowRequestsCount, + useAcceptFollowRequestMutation, + useRejectFollowRequestMutation, + prefetchFollowRequests, +}; diff --git a/packages/pl-fe/src/components/sidebar-menu.tsx b/packages/pl-fe/src/components/sidebar-menu.tsx index 928b57377..aabaaff18 100644 --- a/packages/pl-fe/src/components/sidebar-menu.tsx +++ b/packages/pl-fe/src/components/sidebar-menu.tsx @@ -5,6 +5,7 @@ import { defineMessages, useIntl, FormattedMessage } from 'react-intl'; import { Link, NavLink } from 'react-router-dom'; import { fetchOwnAccounts, logOut, switchAccount } from 'pl-fe/actions/auth'; +import { useFollowRequestsCount } from 'pl-fe/api/hooks/account-lists/use-follow-requests'; import { useAccount } from 'pl-fe/api/hooks/accounts/use-account'; import { useInteractionRequestsCount } from 'pl-fe/api/hooks/statuses/use-interaction-requests'; import Account from 'pl-fe/components/account'; @@ -96,7 +97,7 @@ const SidebarMenu: React.FC = (): JSX.Element | null => { const { account } = useAccount(me || undefined); const otherAccounts = useAppSelector((state) => getOtherAccounts(state)); const { settings } = useSettingsStore(); - const followRequestsCount = useAppSelector((state) => state.user_lists.follow_requests.items.length); + const followRequestsCount = useFollowRequestsCount().data || 0; const interactionRequestsCount = useInteractionRequestsCount().data || 0; const scheduledStatusCount = useAppSelector((state) => Object.keys(state.scheduled_statuses).length); const draftCount = useAppSelector((state) => Object.keys(state.draft_statuses).length); diff --git a/packages/pl-fe/src/components/sidebar-navigation.tsx b/packages/pl-fe/src/components/sidebar-navigation.tsx index e24dfecb1..bb18d825e 100644 --- a/packages/pl-fe/src/components/sidebar-navigation.tsx +++ b/packages/pl-fe/src/components/sidebar-navigation.tsx @@ -1,6 +1,7 @@ import React from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; +import { useFollowRequestsCount } from 'pl-fe/api/hooks/account-lists/use-follow-requests'; import { useInteractionRequestsCount } from 'pl-fe/api/hooks/statuses/use-interaction-requests'; import Icon from 'pl-fe/components/ui/icon'; import Stack from 'pl-fe/components/ui/stack'; @@ -48,7 +49,7 @@ const SidebarNavigation = () => { const logoSrc = useLogo(); const notificationCount = useAppSelector((state) => state.notifications.unread); - const followRequestsCount = useAppSelector((state) => state.user_lists.follow_requests.items.length); + const followRequestsCount = useFollowRequestsCount().data || 0; const interactionRequestsCount = useInteractionRequestsCount().data || 0; const dashboardCount = useAppSelector((state) => state.admin.openReports.length + state.admin.awaitingApproval.length); const scheduledStatusCount = useAppSelector((state) => Object.keys(state.scheduled_statuses).length); diff --git a/packages/pl-fe/src/reducers/user-lists.ts b/packages/pl-fe/src/reducers/user-lists.ts index faa26e367..3a1412f6f 100644 --- a/packages/pl-fe/src/reducers/user-lists.ts +++ b/packages/pl-fe/src/reducers/user-lists.ts @@ -18,39 +18,23 @@ interface List { isLoading: boolean; } -type ListKey = 'follow_requests'; -type NestedListKey = 'pinned' | 'familiar_followers' | 'membership_requests' | 'group_blocks'; +type NestedListKey = 'pinned' | 'familiar_followers' | 'group_blocks'; -type State = Record & Record>; +type State = Record>; const initialState: State = { - follow_requests: { next: null, items: [], isLoading: false }, pinned: {}, familiar_followers: {}, - membership_requests: {}, group_blocks: {}, }; type NestedListPath = [NestedListKey, string]; -type ListPath = [ListKey]; -const normalizeList = (state: State, path: NestedListPath | ListPath, accounts: Array>, next: (() => Promise>) | null = null) => +const normalizeList = (state: State, path: NestedListPath, accounts: Array>, next: (() => Promise>) | null = null) => create(state, (draft) => { - let list: List; - - if (path.length === 1) { - list = draft[path[0]]; - } else { - list = draft[path[0]][path[1]]; - } - + const list = draft[path[0]][path[1]]; const newList = { ...list, next, items: accounts.map(item => item.id), isLoading: false }; - - if (path.length === 1) { - draft[path[0]] = newList; - } else { - draft[path[0]][path[1]] = newList; - } + draft[path[0]][path[1]] = newList; }); const userLists = (state = initialState, action: AccountsAction | FamiliarFollowersAction | GroupsAction): State => {