perf: useIsLoggedIn (#6462)

This commit is contained in:
chenos 2025-03-19 10:28:27 +08:00 committed by GitHub
parent 6541fad14b
commit 645cef5825
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 18 deletions

View File

@ -74,6 +74,7 @@ export const ACLRolesCheckProvider = (props) => {
url: 'roles:check', url: 'roles:check',
}, },
{ {
manual: !api.auth.token,
onSuccess(data) { onSuccess(data) {
if (!data?.data?.snippets.includes('ui.*')) { if (!data?.data?.snippets.includes('ui.*')) {
setDesignable(false); setDesignable(false);

View File

@ -20,6 +20,11 @@ export const useCurrentUserContext = () => {
return useContext(CurrentUserContext); return useContext(CurrentUserContext);
}; };
export const useIsLoggedIn = () => {
const ctx = useContext(CurrentUserContext);
return !!ctx?.data?.data;
};
export const useCurrentRoles = () => { export const useCurrentRoles = () => {
const { allowAnonymous } = useACLRoleContext(); const { allowAnonymous } = useACLRoleContext();
const { data } = useCurrentUserContext(); const { data } = useCurrentUserContext();
@ -39,14 +44,18 @@ export const useCurrentRoles = () => {
export const CurrentUserProvider = (props) => { export const CurrentUserProvider = (props) => {
const api = useAPIClient(); const api = useAPIClient();
const result = useRequest<any>(() => const result = useRequest<any>(
api () =>
.request({ api
url: '/auth:check', .request({
skipNotify: true, url: '/auth:check',
skipAuth: true, skipNotify: true,
}) skipAuth: true,
.then((res) => res?.data), })
.then((res) => res?.data),
{
manual: !api.auth.token,
},
); );
const { render } = useAppSpin(); const { render } = useAppSpin();

View File

@ -8,7 +8,7 @@
*/ */
import { observer } from '@formily/react'; import { observer } from '@formily/react';
import { useIsAdminPage, useRequest } from '@nocobase/client'; import { useIsLoggedIn, useRequest } from '@nocobase/client';
import React, { createContext } from 'react'; import React, { createContext } from 'react';
const EnvAndSecretsContext = createContext<any>({}); const EnvAndSecretsContext = createContext<any>({});
@ -22,8 +22,8 @@ const InternalProvider = (props) => {
const EnvironmentVariablesAndSecretsProvider = observer( const EnvironmentVariablesAndSecretsProvider = observer(
(props) => { (props) => {
const isAdminPage = useIsAdminPage(); const isLoggedIn = useIsLoggedIn();
if (!isAdminPage) { if (!isLoggedIn) {
return <>{props.children}</>; return <>{props.children}</>;
} }
return <InternalProvider {...props} />; return <InternalProvider {...props} />;

View File

@ -6,12 +6,12 @@
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement. * For more information, please refer to: https://www.nocobase.com/agreement.
*/ */
import { CheckCircleOutlined } from '@ant-design/icons';
import { PageHeader } from '@ant-design/pro-layout';
import { Badge, Button, Layout, Menu, Tabs, Tooltip } from 'antd';
import classnames from 'classnames';
import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { Link, Outlet, useNavigate, useParams } from 'react-router-dom'; import { Link, Outlet, useNavigate, useParams } from 'react-router-dom';
import { Button, Layout, Menu, Badge, Tooltip, Tabs } from 'antd';
import { PageHeader } from '@ant-design/pro-layout';
import { CheckCircleOutlined } from '@ant-design/icons';
import classnames from 'classnames';
import { import {
css, css,
@ -22,7 +22,7 @@ import {
useApp, useApp,
useCompile, useCompile,
useDocumentTitle, useDocumentTitle,
useIsAdminPage, useIsLoggedIn,
usePlugin, usePlugin,
useRequest, useRequest,
useToken, useToken,
@ -371,7 +371,7 @@ function TasksCountsProvider(props: any) {
} }
export const TasksProvider = (props: any) => { export const TasksProvider = (props: any) => {
const isAdminPage = useIsAdminPage(); const isLoggedIn = useIsLoggedIn();
const content = ( const content = (
<PinnedPluginListProvider <PinnedPluginListProvider
@ -389,5 +389,5 @@ export const TasksProvider = (props: any) => {
</PinnedPluginListProvider> </PinnedPluginListProvider>
); );
return isAdminPage ? <TasksCountsProvider>{content}</TasksCountsProvider> : content; return isLoggedIn ? <TasksCountsProvider>{content}</TasksCountsProvider> : content;
}; };