pl-fe: avoid unnecessary requests when not logged in
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
/* eslint-disable jsx-a11y/interactive-supports-focus */
|
||||
import { useInfiniteQuery } from '@tanstack/react-query';
|
||||
import clsx from 'clsx';
|
||||
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { defineMessages, useIntl, FormattedMessage } from 'react-intl';
|
||||
import { Link, NavLink } from 'react-router-dom';
|
||||
|
||||
@ -93,15 +93,21 @@ const SidebarMenu: React.FC = React.memo((): JSX.Element | null => {
|
||||
|
||||
const { isSidebarOpen, closeSidebar } = useUiStore();
|
||||
|
||||
const me = useAppSelector((state) => state.me);
|
||||
|
||||
const authenticatedScheduledStatusesCountQueryOptions = useMemo(() => ({
|
||||
...scheduledStatusesCountQueryOptions,
|
||||
enabled: !!me,
|
||||
}), [me]);
|
||||
|
||||
const getOtherAccounts = useCallback(makeGetOtherAccounts(), []);
|
||||
const features = useFeatures();
|
||||
const me = useAppSelector((state) => state.me);
|
||||
const { account } = useAccount(me || undefined);
|
||||
const otherAccounts = useAppSelector((state) => getOtherAccounts(state));
|
||||
const { settings } = useSettingsStore();
|
||||
const followRequestsCount = useFollowRequestsCount().data || 0;
|
||||
const interactionRequestsCount = useInteractionRequestsCount().data || 0;
|
||||
const scheduledStatusCount = useInfiniteQuery(scheduledStatusesCountQueryOptions).data || 0;
|
||||
const scheduledStatusCount = useInfiniteQuery(authenticatedScheduledStatusesCountQueryOptions).data || 0;
|
||||
const draftCount = useAppSelector((state) => Object.keys(state.draft_statuses).length);
|
||||
// const dashboardCount = useAppSelector((state) => state.admin.openReports.count() + state.admin.awaitingApproval.count());
|
||||
const [sidebarVisible, setSidebarVisible] = useState(isSidebarOpen);
|
||||
|
||||
@ -46,11 +46,16 @@ const SidebarNavigation = React.memo(() => {
|
||||
const { account } = useOwnAccount();
|
||||
const { isOpen } = useRegistrationStatus();
|
||||
|
||||
const authenticatedScheduledStatusesCountQueryOptions = useMemo(() => ({
|
||||
...scheduledStatusesCountQueryOptions,
|
||||
enabled: !!account,
|
||||
}), [!!account]);
|
||||
|
||||
const notificationCount = useAppSelector((state) => state.notifications.unread);
|
||||
const followRequestsCount = useFollowRequestsCount().data || 0;
|
||||
const interactionRequestsCount = useInteractionRequestsCount().data || 0;
|
||||
const dashboardCount = useAppSelector((state) => state.admin.openReports.length + state.admin.awaitingApproval.length);
|
||||
const scheduledStatusCount = useInfiniteQuery(scheduledStatusesCountQueryOptions).data || 0;
|
||||
const scheduledStatusCount = useInfiniteQuery(authenticatedScheduledStatusesCountQueryOptions).data || 0;
|
||||
const draftCount = useAppSelector((state) => Object.keys(state.draft_statuses).length);
|
||||
|
||||
const restrictUnauth = instance.pleroma.metadata.restrict_unauthenticated;
|
||||
|
||||
@ -29,6 +29,7 @@ const makeUseFollowRequests = <T>(select: ((data: InfiniteData<PaginatedResponse
|
||||
() => ['accountsLists', 'followRequests'],
|
||||
(client) => client.myAccount.getFollowRequests().then(minifyAccountList),
|
||||
select,
|
||||
'isLoggedIn',
|
||||
);
|
||||
|
||||
const useFollowRequests = makeUseFollowRequests((data) => data.pages.map(page => page.items).flat());
|
||||
|
||||
@ -4,6 +4,7 @@ import { importEntities } from 'pl-fe/actions/importer';
|
||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { useFeatures } from 'pl-fe/hooks/use-features';
|
||||
import { useLoggedIn } from 'pl-fe/hooks/use-logged-in';
|
||||
|
||||
import type { InteractionRequest, PaginatedResponse } from 'pl-api';
|
||||
import type { AppDispatch } from 'pl-fe/store';
|
||||
@ -36,13 +37,14 @@ const useInteractionRequests = <T>(
|
||||
const client = useClient();
|
||||
const features = useFeatures();
|
||||
const dispatch = useAppDispatch();
|
||||
const { isLoggedIn } = useLoggedIn();
|
||||
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['interactionRequests'],
|
||||
queryFn: ({ pageParam }) => pageParam.next?.() || client.interactionRequests.getInteractionRequests().then(response => minifyInteractionRequestsList(dispatch, response)),
|
||||
initialPageParam: { previous: null, next: null, items: [], partial: false } as PaginatedResponse<MinifiedInteractionRequest>,
|
||||
getNextPageParam: (page) => page.next ? page : undefined,
|
||||
enabled: features.interactionRequests,
|
||||
enabled: isLoggedIn && features.interactionRequests,
|
||||
select,
|
||||
});
|
||||
};
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import { type InfiniteData, useInfiniteQuery } from '@tanstack/react-query';
|
||||
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
import { useLoggedIn } from 'pl-fe/hooks/use-logged-in';
|
||||
|
||||
import type { PaginatedResponse, PlApiClient } from 'pl-api';
|
||||
|
||||
@ -8,9 +9,10 @@ const makePaginatedResponseQuery = <T1 extends Array<any>, T2, T3 = Array<T2>>(
|
||||
queryKey: (...params: T1) => Array<string | undefined>,
|
||||
queryFn: (client: PlApiClient, params: T1) => Promise<PaginatedResponse<T2>>,
|
||||
select?: (data: InfiniteData<PaginatedResponse<T2>>) => T3,
|
||||
enabled?: (...params: T1) => boolean,
|
||||
enabled?: ((...params: T1) => boolean) | 'isLoggedIn',
|
||||
) => (...params: T1) => {
|
||||
const client = useClient();
|
||||
const { isLoggedIn } = useLoggedIn();
|
||||
|
||||
return useInfiniteQuery({
|
||||
queryKey: queryKey(...params),
|
||||
@ -18,7 +20,7 @@ const makePaginatedResponseQuery = <T1 extends Array<any>, T2, T3 = Array<T2>>(
|
||||
initialPageParam: { previous: null, next: null, items: [], partial: false } as Awaited<ReturnType<typeof queryFn>>,
|
||||
getNextPageParam: (page) => page.next ? page : undefined,
|
||||
select: select ?? ((data) => data.pages.map(page => page.items).flat() as T3),
|
||||
enabled: enabled?.(...params) ?? true,
|
||||
enabled: enabled === 'isLoggedIn' ? isLoggedIn : (enabled?.(...params) ?? true),
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user