mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-06 11:59:51 +08:00
feat(core): add use guard hook (#11180)
Previously, we used `useLiveData(guardService.can$())` to get the guard result, but `guardService.can$()` will request the server to revalidate the permission when calling it, will cause additional network requests when re-render. This pr make a new hook `useGuard` to fix this problem. And the side effect in `can$` is moved to `revalidateCan()` to make that the subscribe method is pure
This commit is contained in:
+2
-4
@@ -1,4 +1,5 @@
|
||||
import { Button, Divider, useLitPortalFactory } from '@affine/component';
|
||||
import { useGuard } from '@affine/core/components/guard';
|
||||
import { useEnableAI } from '@affine/core/components/hooks/affine/use-enable-ai';
|
||||
import { DocService } from '@affine/core/modules/doc';
|
||||
import {
|
||||
@@ -7,7 +8,6 @@ import {
|
||||
type Link,
|
||||
} from '@affine/core/modules/doc-link';
|
||||
import { toURLSearchParams } from '@affine/core/modules/navigation';
|
||||
import { GuardService } from '@affine/core/modules/permissions';
|
||||
import { GlobalSessionStateService } from '@affine/core/modules/storage';
|
||||
import { WorkbenchLink } from '@affine/core/modules/workbench';
|
||||
import {
|
||||
@@ -26,7 +26,6 @@ import {
|
||||
LiveData,
|
||||
useFramework,
|
||||
useLiveData,
|
||||
useService,
|
||||
useServices,
|
||||
} from '@toeverything/infra';
|
||||
import {
|
||||
@@ -265,8 +264,7 @@ export const LinkPreview = ({
|
||||
linkGroup: BacklinkGroups;
|
||||
textRendererOptions: TextRendererOptions;
|
||||
}) => {
|
||||
const guardService = useService(GuardService);
|
||||
const canAccess = useLiveData(guardService.can$('Doc_Read', linkGroup.docId));
|
||||
const canAccess = useGuard('Doc_Read', linkGroup.docId);
|
||||
const t = useI18n();
|
||||
|
||||
if (!canAccess) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
MenuSub,
|
||||
} from '@affine/component/ui/menu';
|
||||
import { PageHistoryModal } from '@affine/core/components/affine/page-history-modal';
|
||||
import { useGuard } from '@affine/core/components/guard';
|
||||
import { useBlockSuiteMetaHelper } from '@affine/core/components/hooks/affine/use-block-suite-meta-helper';
|
||||
import { useEnableCloud } from '@affine/core/components/hooks/affine/use-enable-cloud';
|
||||
import { useExportPage } from '@affine/core/components/hooks/affine/use-export-page';
|
||||
@@ -333,8 +334,8 @@ const PageHeaderMenuItem = ({
|
||||
openInAppService?.showOpenInAppPage();
|
||||
}, [openInAppService]);
|
||||
|
||||
const canEdit = useLiveData(guardService.can$('Doc_Update', pageId));
|
||||
const canMoveToTrash = useLiveData(guardService.can$('Doc_Trash', pageId));
|
||||
const canEdit = useGuard('Doc_Update', pageId);
|
||||
const canMoveToTrash = useGuard('Doc_Trash', pageId);
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import type { InlineEditProps } from '@affine/component';
|
||||
import { InlineEdit } from '@affine/component';
|
||||
import { useGuard } from '@affine/core/components/guard';
|
||||
import { useAsyncCallback } from '@affine/core/components/hooks/affine-async-hooks';
|
||||
import { DocService, DocsService } from '@affine/core/modules/doc';
|
||||
import { GuardService } from '@affine/core/modules/permissions';
|
||||
import { WorkspaceService } from '@affine/core/modules/workspace';
|
||||
import { track } from '@affine/track';
|
||||
import { useLiveData, useService } from '@toeverything/infra';
|
||||
@@ -25,7 +25,6 @@ export const BlocksuiteHeaderTitle = (props: BlockSuiteHeaderTitleProps) => {
|
||||
const workspaceService = useService(WorkspaceService);
|
||||
const isSharedMode = workspaceService.workspace.openOptions.isSharedMode;
|
||||
const docsService = useService(DocsService);
|
||||
const guardService = useService(GuardService);
|
||||
const docService = useService(DocService);
|
||||
const docTitle = useLiveData(docService.doc.record.title$);
|
||||
|
||||
@@ -37,9 +36,7 @@ export const BlocksuiteHeaderTitle = (props: BlockSuiteHeaderTitleProps) => {
|
||||
[docService.doc.id, docsService]
|
||||
);
|
||||
|
||||
const canEdit = useLiveData(
|
||||
guardService.can$('Doc_Update', docService.doc.id)
|
||||
);
|
||||
const canEdit = useGuard('Doc_Update', docService.doc.id);
|
||||
|
||||
return (
|
||||
<InlineEdit
|
||||
|
||||
Reference in New Issue
Block a user