From b3770d18c4caf2744f45d6d4c569488755f86efa Mon Sep 17 00:00:00 2001 From: Leonardo Giacone Date: Fri, 10 Jan 2025 11:12:11 +0100 Subject: [PATCH] refactor: remove useFolders --- .../src/components/FolderTree/List/index.tsx | 10 ++++++---- .../src/components/FolderTree/List/utils.ts | 6 +++--- .../app-aco/src/components/FolderTree/index.tsx | 9 ++++++--- packages/app-aco/src/contexts/acoList.tsx | 10 ++++------ packages/app-aco/src/dialogs/useCreateDialog.tsx | 4 ++-- packages/app-aco/src/dialogs/useDeleteDialog.tsx | 4 ++-- packages/app-aco/src/dialogs/useEditDialog.tsx | 4 ++-- .../src/dialogs/useSetPermissionsDialog.tsx | 4 ++-- .../folder/deleteFolder/DeleteFolder.test.ts | 9 ++++++++- .../folder/deleteFolder/IDeleteFolderUseCase.ts | 7 +++++++ .../folder/deleteFolder/useDeleteFolder.ts | 2 +- packages/app-aco/src/features/index.ts | 1 + packages/app-aco/src/hooks/useFolders.ts | 7 ++++++- packages/app-aco/src/index.ts | 1 + .../FileManagerViewContext.tsx | 4 ++-- .../FileManagerViewProvider/useListFiles.ts | 4 ++-- .../src/admin/views/contentEntries/Table/Main.tsx | 15 +++++++++------ .../components/BulkActions/SecureActionDelete.tsx | 11 +++++------ .../BulkActions/SecureActionDuplicate.tsx | 9 +++++---- .../components/BulkActions/SecureActionMove.tsx | 9 +++++---- .../BulkActions/SecureActionPublish.tsx | 9 +++++---- .../BulkActions/SecureActionUnpublish.tsx | 9 +++++---- .../Table/Actions/SecureChangePageStatus.tsx | 7 ++++--- .../Table/Table/Actions/SecureDeletePage.tsx | 9 +++++---- .../Table/Table/Actions/SecureDuplicatePage.tsx | 9 +++++---- .../Table/Table/Actions/SecureEditPage.tsx | 9 +++++---- .../Table/Table/Actions/SecureMovePage.tsx | 10 +++++----- .../pageDetails/header/deletePage/DeletePage.tsx | 9 +++++---- .../header/editRevision/EditRevision.tsx | 9 +++++---- .../DuplicatePage/SecureDuplicatePage.tsx | 10 +++++----- .../header/pageOptionsMenu/PageOptionsMenu.tsx | 7 ++++--- .../header/publishRevision/PublishRevision.tsx | 7 ++++--- .../SecureDeleteRevisionMenuOption.tsx | 10 +++++----- .../MenuOptions/SecureEditRevisionMenuOption.tsx | 10 +++++----- .../MenuOptions/SecureNewRevisionFromCurrent.tsx | 9 +++++---- .../MenuOptions/SecurePublishPageMenuOption.tsx | 9 +++++---- .../MenuOptions/SecureUnpublishPageMenuOption.tsx | 9 +++++---- .../src/admin/views/Pages/Table/Main.tsx | 15 +++++++++------ 38 files changed, 170 insertions(+), 126 deletions(-) create mode 100644 packages/app-aco/src/features/index.ts diff --git a/packages/app-aco/src/components/FolderTree/List/index.tsx b/packages/app-aco/src/components/FolderTree/List/index.tsx index 11fa30881cb..97dd3d9c5df 100644 --- a/packages/app-aco/src/components/FolderTree/List/index.tsx +++ b/packages/app-aco/src/components/FolderTree/List/index.tsx @@ -13,7 +13,7 @@ import { Node } from "../Node"; import { NodePreview } from "../NodePreview"; import { Placeholder } from "../Placeholder"; import { createInitialOpenList, createTreeData } from "./utils"; -import { useFolders } from "~/hooks"; +import { useGetFolderLevelPermission, useUpdateFolder } from "~/features"; import { ROOT_FOLDER } from "~/constants"; import { DndFolderItemData, FolderItem } from "~/types"; import { FolderProvider } from "~/contexts/folder"; @@ -33,7 +33,9 @@ export const List = ({ hiddenFolderIds, enableActions }: ListProps) => { - const { updateFolder, folderLevelPermissions: flp } = useFolders(); + const { updateFolder } = useUpdateFolder(); + const { getFolderLevelPermission: canManageStructure } = + useGetFolderLevelPermission("canManageStructure"); const { showSnackbar } = useSnackbar(); const [treeData, setTreeData] = useState[]>([]); const [initialOpenList, setInitialOpenList] = useState(); @@ -95,9 +97,9 @@ export const List = ({ const canDrag = useCallback( (folderId: string) => { const isRootFolder = folderId === ROOT_FOLDER; - return !isRootFolder && flp.canManageStructure(folderId); + return !isRootFolder && canManageStructure(folderId); }, - [flp.canManageStructure] + [canManageStructure] ); return ( diff --git a/packages/app-aco/src/components/FolderTree/List/utils.ts b/packages/app-aco/src/components/FolderTree/List/utils.ts index 4659211a1e5..d3b7010315c 100644 --- a/packages/app-aco/src/components/FolderTree/List/utils.ts +++ b/packages/app-aco/src/components/FolderTree/List/utils.ts @@ -3,9 +3,9 @@ import { DndFolderItemData, FolderItem } from "~/types"; import { ROOT_FOLDER } from "~/constants"; /** - * Transform an array of folders returned by useFolders hook into an array of elements for the tree component. + * Transform an array of folders returned by folders cache into an array of elements for the tree component. * - * @param folders list of folders returned by useFolders hook. + * @param folders list of folders returned by folders cache. * @param focusedNodeId id of the current folder selected/focused. * @param hiddenFolderIds list ids of the folder you don't want to show within the list. * @return array of elements to render the tree component. @@ -37,7 +37,7 @@ export const createTreeData = ( * Return an array of ids of open folders, based on the current focused folder id, its parent folders and the folders * opened by user interaction. * - * @param folders list of folders returned by useFolders hook. + * @param folders list of folders returned by folders cache. * @param openIds list of open folders ids. * @param focusedId id of the current folder selected/focused. * @return array of ids of open folders. diff --git a/packages/app-aco/src/components/FolderTree/index.tsx b/packages/app-aco/src/components/FolderTree/index.tsx index 2bbbbc73a36..b874066ae59 100644 --- a/packages/app-aco/src/components/FolderTree/index.tsx +++ b/packages/app-aco/src/components/FolderTree/index.tsx @@ -1,6 +1,6 @@ import React, { useMemo } from "react"; import { Tooltip } from "@webiny/ui/Tooltip"; -import { useFolders } from "~/hooks/useFolders"; +import { useGetFolderLevelPermission, useListFolders } from "~/features"; import { CreateButton } from "./ButtonCreate"; import { Empty } from "./Empty"; import { Loader } from "./Loader"; @@ -29,7 +29,10 @@ export const FolderTree = ({ onFolderClick, rootFolderLabel }: FolderTreeProps) => { - const { folders, folderLevelPermissions: flp, loading } = useFolders(); + const { loading, folders } = useListFolders(); + const { getFolderLevelPermission: canManageStructure } = + useGetFolderLevelPermission("canManageStructure"); + const localFolders = useMemo(() => { if (!folders) { return []; @@ -50,7 +53,7 @@ export const FolderTree = ({ let createButton = null; if (enableCreate) { - const canCreate = flp.canManageStructure(focusedFolderId!); + const canCreate = canManageStructure(focusedFolderId!); createButton = ; diff --git a/packages/app-aco/src/contexts/acoList.tsx b/packages/app-aco/src/contexts/acoList.tsx index 2695295319a..53f4709a91b 100644 --- a/packages/app-aco/src/contexts/acoList.tsx +++ b/packages/app-aco/src/contexts/acoList.tsx @@ -11,7 +11,8 @@ import { ListSearchRecordsSort, SearchRecordItem } from "~/types"; -import { useAcoApp, useFolders, useNavigateFolder } from "~/hooks"; +import { useAcoApp, useNavigateFolder } from "~/hooks"; +import { useGetDescendantFolders, useListFolders } from "~/features"; import { FoldersContext } from "~/contexts/folders"; import { SearchRecordsContext } from "~/contexts/records"; import { sortTableItems, validateOrGetDefaultDbSort } from "~/sorting"; @@ -120,11 +121,8 @@ export const AcoListProvider = ({ children, ...props }: AcoListProviderProps) => const { identity } = useSecurity(); const { currentFolderId } = useNavigateFolder(); const { folderIdPath, folderIdInPath } = useAcoApp(); - const { - folders: originalFolders, - loading: foldersLoading, - getDescendantFolders - } = useFolders(); + const { folders: originalFolders, loading: foldersLoading } = useListFolders(); + const { getDescendantFolders } = useGetDescendantFolders(); const folderContext = useContext(FoldersContext); const searchContext = useContext(SearchRecordsContext); diff --git a/packages/app-aco/src/dialogs/useCreateDialog.tsx b/packages/app-aco/src/dialogs/useCreateDialog.tsx index 1920abe3593..c3194deabb2 100644 --- a/packages/app-aco/src/dialogs/useCreateDialog.tsx +++ b/packages/app-aco/src/dialogs/useCreateDialog.tsx @@ -10,7 +10,7 @@ import { validation } from "@webiny/validation"; import { FolderTree } from "~/components"; import { useDialogs } from "@webiny/app-admin"; import { DialogFoldersContainer } from "~/dialogs/styled"; -import { useFolders } from "~/hooks"; +import { useCreateFolder } from "~/features"; import { ROOT_FOLDER } from "~/constants"; import { FolderItem } from "~/types"; @@ -81,7 +81,7 @@ const FormComponent = ({ currentParentId = null }: FormComponentProps) => { export const useCreateDialog = (): UseCreateDialogResponse => { const dialogs = useDialogs(); - const { createFolder } = useFolders(); + const { createFolder } = useCreateFolder(); const { showSnackbar } = useSnackbar(); const onAccept = useCallback(async (data: FolderItem) => { diff --git a/packages/app-aco/src/dialogs/useDeleteDialog.tsx b/packages/app-aco/src/dialogs/useDeleteDialog.tsx index a8dd8e65bf5..54f8f643f6e 100644 --- a/packages/app-aco/src/dialogs/useDeleteDialog.tsx +++ b/packages/app-aco/src/dialogs/useDeleteDialog.tsx @@ -1,7 +1,7 @@ import { useSnackbar } from "@webiny/app-admin"; import { useDialogs } from "@webiny/app-admin"; -import { useFolders } from "~/hooks"; +import { useDeleteFolder } from "~/features"; import { FolderItem } from "~/types"; import { useCallback } from "react"; @@ -15,7 +15,7 @@ interface UseDeleteDialogResponse { export const useDeleteDialog = (): UseDeleteDialogResponse => { const dialogs = useDialogs(); - const { deleteFolder } = useFolders(); + const { deleteFolder } = useDeleteFolder(); const { showSnackbar } = useSnackbar(); const onAccept = useCallback(async (folder: FolderItem) => { diff --git a/packages/app-aco/src/dialogs/useEditDialog.tsx b/packages/app-aco/src/dialogs/useEditDialog.tsx index da03f9575ae..e9027d28832 100644 --- a/packages/app-aco/src/dialogs/useEditDialog.tsx +++ b/packages/app-aco/src/dialogs/useEditDialog.tsx @@ -10,7 +10,7 @@ import { FolderTree } from "~/components"; import { ROOT_FOLDER } from "~/constants"; import { useDialogs } from "@webiny/app-admin"; import { DialogFoldersContainer } from "~/dialogs/styled"; -import { useFolders } from "~/hooks"; +import { useUpdateFolder } from "~/features"; import { FolderItem } from "~/types"; interface ShowDialogParams { @@ -72,7 +72,7 @@ const FormComponent = ({ folder }: FormComponentProps) => { export const useEditDialog = (): UseEditDialogResponse => { const dialog = useDialogs(); - const { updateFolder } = useFolders(); + const { updateFolder } = useUpdateFolder(); const { showSnackbar } = useSnackbar(); const onAccept = useCallback(async (folder: FolderItem, data: GenericFormData) => { diff --git a/packages/app-aco/src/dialogs/useSetPermissionsDialog.tsx b/packages/app-aco/src/dialogs/useSetPermissionsDialog.tsx index 8758d00bc74..9505cdd4139 100644 --- a/packages/app-aco/src/dialogs/useSetPermissionsDialog.tsx +++ b/packages/app-aco/src/dialogs/useSetPermissionsDialog.tsx @@ -9,7 +9,7 @@ import { UsersTeamsSelection } from "./DialogSetPermissions/UsersTeamsSelection" import { LIST_FOLDER_LEVEL_PERMISSIONS_TARGETS } from "./DialogSetPermissions/graphql"; import { useDialogs } from "@webiny/app-admin"; -import { useFolders } from "~/hooks"; +import { useUpdateFolder } from "~/features"; import { FolderItem, FolderLevelPermissionsTarget, FolderPermission } from "~/types"; interface ShowDialogParams { @@ -115,7 +115,7 @@ const FormComponent = ({ folder }: FormComponentProps) => { export const useSetPermissionsDialog = (): UseSetPermissionsDialogResponse => { const dialogs = useDialogs(); - const { updateFolder } = useFolders(); + const { updateFolder } = useUpdateFolder(); const { showSnackbar } = useSnackbar(); const onAccept = useCallback(async (folder: FolderItem, data: Partial) => { diff --git a/packages/app-aco/src/features/folder/deleteFolder/DeleteFolder.test.ts b/packages/app-aco/src/features/folder/deleteFolder/DeleteFolder.test.ts index fe7c3db797b..d587c352579 100644 --- a/packages/app-aco/src/features/folder/deleteFolder/DeleteFolder.test.ts +++ b/packages/app-aco/src/features/folder/deleteFolder/DeleteFolder.test.ts @@ -30,7 +30,14 @@ describe("DeleteFolder", () => { const item = foldersCache.getItem(folder => folder.id === "any-folder-id"); expect(item?.id).toEqual("any-folder-id"); - await deleteFolder.execute({ id: "any-folder-id" }); + await deleteFolder.execute({ + id: "any-folder-id", + title: "New Folder", + slug: "new-folder", + parentId: null, + permissions: [], + type + }); expect(gateway.execute).toHaveBeenCalledTimes(1); expect(foldersCache.hasItems()).toBeFalse(); diff --git a/packages/app-aco/src/features/folder/deleteFolder/IDeleteFolderUseCase.ts b/packages/app-aco/src/features/folder/deleteFolder/IDeleteFolderUseCase.ts index c4963f82922..a257dd5b0e0 100644 --- a/packages/app-aco/src/features/folder/deleteFolder/IDeleteFolderUseCase.ts +++ b/packages/app-aco/src/features/folder/deleteFolder/IDeleteFolderUseCase.ts @@ -1,5 +1,12 @@ +import { FolderPermission } from "~/types"; + export interface DeleteFolderParams { id: string; + title: string; + slug: string; + type: string; + parentId: string | null; + permissions: FolderPermission[]; } export interface IDeleteFolderUseCase { diff --git a/packages/app-aco/src/features/folder/deleteFolder/useDeleteFolder.ts b/packages/app-aco/src/features/folder/deleteFolder/useDeleteFolder.ts index 5f36f3fb893..e0344e7be1f 100644 --- a/packages/app-aco/src/features/folder/deleteFolder/useDeleteFolder.ts +++ b/packages/app-aco/src/features/folder/deleteFolder/useDeleteFolder.ts @@ -12,7 +12,7 @@ export const useDeleteFolder = () => { const foldersContext = useContext(FoldersContext); if (!foldersContext) { - throw new Error("useFolders must be used within a FoldersProvider"); + throw new Error("useDeleteFolder must be used within a FoldersProvider"); } const { type } = foldersContext; diff --git a/packages/app-aco/src/features/index.ts b/packages/app-aco/src/features/index.ts new file mode 100644 index 00000000000..40d34a8d6d6 --- /dev/null +++ b/packages/app-aco/src/features/index.ts @@ -0,0 +1 @@ +export * from "./folder"; diff --git a/packages/app-aco/src/hooks/useFolders.ts b/packages/app-aco/src/hooks/useFolders.ts index 1a0a6c7e9ee..a493b1c546e 100644 --- a/packages/app-aco/src/hooks/useFolders.ts +++ b/packages/app-aco/src/hooks/useFolders.ts @@ -6,8 +6,13 @@ import { useGetFolderLevelPermission, useListFolders, useUpdateFolder -} from "~/features/folder"; +} from "~/features"; +/** + * Custom hook to manage folder operations. + * + * @deprecated This hook is deprecated. Use the individual hooks directly from "~/features" instead. + */ export const useFolders = () => { const { createFolder } = useCreateFolder(); const { deleteFolder } = useDeleteFolder(); diff --git a/packages/app-aco/src/index.ts b/packages/app-aco/src/index.ts index 1c69c067f52..34b2023afd9 100644 --- a/packages/app-aco/src/index.ts +++ b/packages/app-aco/src/index.ts @@ -2,5 +2,6 @@ export * from "./components"; export * from "./config"; export * from "./contexts"; export * from "./hooks"; +export * from "./features"; export * from "./dialogs"; export * from "./sorting"; diff --git a/packages/app-file-manager/src/modules/FileManagerRenderer/FileManagerViewProvider/FileManagerViewContext.tsx b/packages/app-file-manager/src/modules/FileManagerRenderer/FileManagerViewProvider/FileManagerViewContext.tsx index dc034cefd22..89d876e4245 100644 --- a/packages/app-file-manager/src/modules/FileManagerRenderer/FileManagerViewProvider/FileManagerViewContext.tsx +++ b/packages/app-file-manager/src/modules/FileManagerRenderer/FileManagerViewProvider/FileManagerViewContext.tsx @@ -8,7 +8,7 @@ import { initializeState, State } from "./state"; import { FolderItem, ListMeta, ListSearchRecordsSort } from "@webiny/app-aco/types"; import { UploadOptions } from "@webiny/app/types"; import { sortTableItems } from "@webiny/app-aco/sorting"; -import { useFolders, useNavigateFolder } from "@webiny/app-aco"; +import { useListFolders, useNavigateFolder } from "@webiny/app-aco"; import { ListFilesQueryVariables } from "~/modules/FileManagerApiProvider/graphql"; import { useListFiles } from "./useListFiles"; import { useTags } from "./useTags"; @@ -106,7 +106,7 @@ export const FileManagerViewProvider = ({ children, ...props }: FileManagerViewP const shiftKeyPressed = useShiftKey(); const modifiers = { scope: props.scope, own: props.own, accept: props.accept }; const fileManager = useFileManagerApi(); - const { folders: originalFolders, loading: foldersLoading } = useFolders(); + const { folders: originalFolders, loading: foldersLoading } = useListFolders(); const { currentFolderId = ROOT_FOLDER, navigateToFolder } = useNavigateFolder(); const tags = useTags(modifiers); const [state, setState] = useStateIfMounted(initializeState()); diff --git a/packages/app-file-manager/src/modules/FileManagerRenderer/FileManagerViewProvider/useListFiles.ts b/packages/app-file-manager/src/modules/FileManagerRenderer/FileManagerViewProvider/useListFiles.ts index a28a45b6629..3d6811e76c4 100644 --- a/packages/app-file-manager/src/modules/FileManagerRenderer/FileManagerViewProvider/useListFiles.ts +++ b/packages/app-file-manager/src/modules/FileManagerRenderer/FileManagerViewProvider/useListFiles.ts @@ -1,6 +1,6 @@ import isEqual from "lodash/isEqual"; import { validateOrGetDefaultDbSort } from "@webiny/app-aco/sorting"; -import { useFolders } from "@webiny/app-aco"; +import { useGetDescendantFolders } from "@webiny/app-aco"; import { ListMeta } from "@webiny/app-aco/types"; import { useSecurity } from "@webiny/app-security"; import { FileItem } from "@webiny/app-admin/types"; @@ -41,7 +41,7 @@ interface UseListFilesParams { export function useListFiles({ modifiers, folderId, state }: UseListFilesParams) { const { identity } = useSecurity(); const fileManager = useFileManagerApi(); - const { getDescendantFolders } = useFolders(); + const { getDescendantFolders } = useGetDescendantFolders(); const [meta, setMeta] = useStateIfMounted(undefined); const [files, setFiles] = useStateIfMounted([]); const [loading, setLoading] = useStateIfMounted>({}); diff --git a/packages/app-headless-cms/src/admin/views/contentEntries/Table/Main.tsx b/packages/app-headless-cms/src/admin/views/contentEntries/Table/Main.tsx index af6797906a2..6e6488ea02d 100644 --- a/packages/app-headless-cms/src/admin/views/contentEntries/Table/Main.tsx +++ b/packages/app-headless-cms/src/admin/views/contentEntries/Table/Main.tsx @@ -1,6 +1,6 @@ import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; import debounce from "lodash/debounce"; -import { useCreateDialog, useFolders } from "@webiny/app-aco"; +import { useCreateDialog, useGetFolderLevelPermission } from "@webiny/app-aco"; import { Scrollbar } from "@webiny/ui/Scrollbar"; import { Empty } from "~/admin/components/ContentEntries/Empty"; import { Filters } from "~/admin/components/ContentEntries/Filters"; @@ -29,15 +29,18 @@ export const Main = ({ folderId: initialFolderId }: MainProps) => { // We check permissions on two layers - security and folder level permissions. const { canCreate, contentModel } = useContentEntry(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); + const { getFolderLevelPermission: canManageStructure } = + useGetFolderLevelPermission("canManageStructure"); const canCreateFolder = useMemo(() => { - return flp.canManageStructure(folderId); - }, [flp, folderId]); + return canManageStructure(folderId); + }, [canManageStructure, folderId]); const canCreateContent = useMemo(() => { - return canCreate && flp.canManageContent(folderId); - }, [flp, folderId]); + return canCreate && canManageContent(folderId); + }, [canManageContent, folderId]); const createEntry = useCallback(() => { const folder = folderId ? `&folderId=${encodeURIComponent(folderId)}` : ""; diff --git a/packages/app-page-builder/src/admin/components/BulkActions/SecureActionDelete.tsx b/packages/app-page-builder/src/admin/components/BulkActions/SecureActionDelete.tsx index 8af05ee53f3..225683e4aba 100644 --- a/packages/app-page-builder/src/admin/components/BulkActions/SecureActionDelete.tsx +++ b/packages/app-page-builder/src/admin/components/BulkActions/SecureActionDelete.tsx @@ -1,5 +1,5 @@ import React, { useMemo } from "react"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { observer } from "mobx-react-lite"; import { PageListConfig } from "~/admin/config/pages"; import { usePagesPermissions } from "~/hooks/permissions"; @@ -7,18 +7,17 @@ import { ActionDelete as ActionDeleteBase } from "~/admin/components/BulkActions export const SecureActionDelete = observer(() => { const { canDelete } = usePagesPermissions(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const { useWorker } = PageListConfig.Browser.BulkAction; const worker = useWorker(); const canDeleteAll = useMemo(() => { return worker.items.every(item => { - return ( - canDelete(item.data.createdBy.id) && flp.canManageContent(item.location?.folderId) - ); + return canDelete(item.data.createdBy.id) && canManageContent(item.location?.folderId); }); - }, [worker.items]); + }, [worker.items, canManageContent]); if (!canDeleteAll) { console.log("You don't have permissions to delete pages."); diff --git a/packages/app-page-builder/src/admin/components/BulkActions/SecureActionDuplicate.tsx b/packages/app-page-builder/src/admin/components/BulkActions/SecureActionDuplicate.tsx index 527d7f2903d..2e333ddac8d 100644 --- a/packages/app-page-builder/src/admin/components/BulkActions/SecureActionDuplicate.tsx +++ b/packages/app-page-builder/src/admin/components/BulkActions/SecureActionDuplicate.tsx @@ -1,5 +1,5 @@ import React, { useMemo } from "react"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { observer } from "mobx-react-lite"; import { PageListConfig } from "~/admin/config/pages"; import { usePagesPermissions } from "~/hooks/permissions"; @@ -8,7 +8,8 @@ import { ActionDuplicate } from "~/admin/components/BulkActions/ActionDuplicate" export const SecureActionDuplicate = ActionDuplicate.createDecorator(Original => { return observer(() => { const { canWrite: pagesCanWrite } = usePagesPermissions(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const { useWorker } = PageListConfig.Browser.BulkAction; const worker = useWorker(); @@ -17,10 +18,10 @@ export const SecureActionDuplicate = ActionDuplicate.createDecorator(Original => return worker.items.every(item => { return ( pagesCanWrite(item.data.createdBy.id) && - flp.canManageContent(item.location?.folderId) + canManageContent(item.location?.folderId) ); }); - }, [worker.items]); + }, [worker.items, canManageContent]); if (!canDuplicateAll) { console.log("You don't have permissions to duplicate pages."); diff --git a/packages/app-page-builder/src/admin/components/BulkActions/SecureActionMove.tsx b/packages/app-page-builder/src/admin/components/BulkActions/SecureActionMove.tsx index 733d0ca65e1..0e12c532ed8 100644 --- a/packages/app-page-builder/src/admin/components/BulkActions/SecureActionMove.tsx +++ b/packages/app-page-builder/src/admin/components/BulkActions/SecureActionMove.tsx @@ -1,5 +1,5 @@ import React, { useMemo } from "react"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { observer } from "mobx-react-lite"; import { PageListConfig } from "~/admin/config/pages"; import { ActionMove as ActionMoveBase } from "~/admin/components/BulkActions"; @@ -7,13 +7,14 @@ import { ActionMove as ActionMoveBase } from "~/admin/components/BulkActions"; export const SecureActionMove = observer(() => { const { useWorker } = PageListConfig.Browser.BulkAction; const worker = useWorker(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const canMoveAll = useMemo(() => { return worker.items.every(item => { - return flp.canManageContent(item.location?.folderId); + return canManageContent(item.location?.folderId); }); - }, [worker.items]); + }, [worker.items, canManageContent]); if (!canMoveAll) { return null; diff --git a/packages/app-page-builder/src/admin/components/BulkActions/SecureActionPublish.tsx b/packages/app-page-builder/src/admin/components/BulkActions/SecureActionPublish.tsx index 051d25e6d8d..80dc43dbd92 100644 --- a/packages/app-page-builder/src/admin/components/BulkActions/SecureActionPublish.tsx +++ b/packages/app-page-builder/src/admin/components/BulkActions/SecureActionPublish.tsx @@ -1,5 +1,5 @@ import React, { useMemo } from "react"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { observer } from "mobx-react-lite"; import { PageListConfig } from "~/admin/config/pages"; import { usePagesPermissions } from "~/hooks/permissions"; @@ -8,16 +8,17 @@ import { ActionPublish as ActionPublishBase } from "~/admin/components/BulkActio export const SecureActionPublish = observer(() => { const { canPublish } = usePagesPermissions(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const { useWorker } = PageListConfig.Browser.BulkAction; const worker = useWorker(); const canPublishAll = useMemo(() => { return worker.items.every(item => { - return canPublish() && flp.canManageContent(item.location?.folderId); + return canPublish() && canManageContent(item.location?.folderId); }); - }, [worker.items]); + }, [worker.items, canManageContent]); if (!canPublishAll) { console.log("You don't have permissions to publish pages."); diff --git a/packages/app-page-builder/src/admin/components/BulkActions/SecureActionUnpublish.tsx b/packages/app-page-builder/src/admin/components/BulkActions/SecureActionUnpublish.tsx index 9683f12a73f..6025b05b730 100644 --- a/packages/app-page-builder/src/admin/components/BulkActions/SecureActionUnpublish.tsx +++ b/packages/app-page-builder/src/admin/components/BulkActions/SecureActionUnpublish.tsx @@ -1,13 +1,14 @@ import React, { useMemo } from "react"; import { observer } from "mobx-react-lite"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { PageListConfig } from "~/admin/config/pages"; import { usePagesPermissions } from "~/hooks/permissions"; import { ActionUnpublish as ActionUnpublishBase } from "~/admin/components/BulkActions"; export const SecureActionUnpublish = observer(() => { const { canUnpublish } = usePagesPermissions(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const { useWorker } = PageListConfig.Browser.BulkAction; @@ -15,9 +16,9 @@ export const SecureActionUnpublish = observer(() => { const canUnpublishAll = useMemo(() => { return worker.items.every(item => { - return canUnpublish() && flp.canManageContent(item.location?.folderId); + return canUnpublish() && canManageContent(item.location?.folderId); }); - }, [worker.items]); + }, [worker.items, canManageContent]); if (!canUnpublishAll) { console.log("You don't have permissions to unpublish pages."); diff --git a/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureChangePageStatus.tsx b/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureChangePageStatus.tsx index 13a2ba0618e..46cc3d4e423 100644 --- a/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureChangePageStatus.tsx +++ b/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureChangePageStatus.tsx @@ -1,12 +1,13 @@ import React from "react"; import { usePage } from "~/admin/views/Pages/hooks/usePage"; import { usePagesPermissions } from "~/hooks/permissions"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { ChangePageStatus } from "./ChangePageStatus"; export const SecureChangePageStatus = () => { const { page } = usePage(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const { hasPermissions, canPublish, canUnpublish } = usePagesPermissions(); if (!hasPermissions()) { @@ -14,7 +15,7 @@ export const SecureChangePageStatus = () => { } const { folderId } = page.location; - if (!flp.canManageContent(folderId)) { + if (!canManageContent(folderId)) { return null; } diff --git a/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureDeletePage.tsx b/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureDeletePage.tsx index e06a04da33e..fc1672a286f 100644 --- a/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureDeletePage.tsx +++ b/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureDeletePage.tsx @@ -1,18 +1,19 @@ import React, { useMemo } from "react"; import { usePage } from "~/admin/views/Pages/hooks/usePage"; import { usePagesPermissions } from "~/hooks/permissions"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { DeletePage } from "./DeletePage"; export const SecureDeletePage = () => { const { page } = usePage(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const { canDelete: pagesCanDelete } = usePagesPermissions(); const { folderId } = page.location; const canDelete = useMemo(() => { - return pagesCanDelete(page.data.createdBy.id) && flp.canManageContent(folderId); - }, [flp, folderId]); + return pagesCanDelete(page.data.createdBy.id) && canManageContent(folderId); + }, [canManageContent, folderId]); if (!canDelete) { return null; diff --git a/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureDuplicatePage.tsx b/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureDuplicatePage.tsx index 9af2fdbcbe0..c894783d806 100644 --- a/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureDuplicatePage.tsx +++ b/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureDuplicatePage.tsx @@ -1,5 +1,5 @@ import React, { useMemo } from "react"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { usePagesPermissions } from "~/hooks/permissions"; import { usePage } from "~/admin/views/Pages/hooks/usePage"; import { DuplicatePage } from "./DuplicatePage"; @@ -7,14 +7,15 @@ import { DuplicatePage } from "./DuplicatePage"; export const SecureDuplicatePage = DuplicatePage.createDecorator(Original => { return function SecureDuplicatePageRenderer() { const { page } = usePage(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const { canWrite: pagesCanWrite } = usePagesPermissions(); const { folderId } = page.location; const canDuplicate = useMemo(() => { - return pagesCanWrite(page.data.createdBy.id) && flp.canManageContent(folderId); - }, [flp, folderId]); + return pagesCanWrite(page.data.createdBy.id) && canManageContent(folderId); + }, [canManageContent, folderId]); if (!canDuplicate) { return null; diff --git a/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureEditPage.tsx b/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureEditPage.tsx index e6a342f059e..79a4a7127d4 100644 --- a/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureEditPage.tsx +++ b/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureEditPage.tsx @@ -1,18 +1,19 @@ import React, { useMemo } from "react"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { usePage } from "~/admin/views/Pages/hooks/usePage"; import { usePagesPermissions } from "~/hooks/permissions"; import { EditPage } from "./EditPage"; export const SecureEditPage = () => { const { page } = usePage(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const { canUpdate: pagesCanUpdate } = usePagesPermissions(); const { folderId } = page.location; const canEdit = useMemo(() => { - return pagesCanUpdate(page.data.createdBy.id) && flp.canManageContent(folderId); - }, [flp, folderId]); + return pagesCanUpdate(page.data.createdBy.id) && canManageContent(folderId); + }, [canManageContent, folderId]); if (!canEdit) { return null; diff --git a/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureMovePage.tsx b/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureMovePage.tsx index eece9aa980a..515a1c3bfeb 100644 --- a/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureMovePage.tsx +++ b/packages/app-page-builder/src/admin/components/Table/Table/Actions/SecureMovePage.tsx @@ -1,17 +1,17 @@ import React, { useMemo } from "react"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { usePage } from "~/admin/views/Pages/hooks/usePage"; import { MovePage } from "./MovePage"; export const SecureMovePage = () => { const { page } = usePage(); - - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const { folderId } = page.location; const canMove = useMemo(() => { - return flp.canManageContent(folderId); - }, [flp, folderId]); + return canManageContent(folderId); + }, [canManageContent, folderId]); if (!canMove) { return null; diff --git a/packages/app-page-builder/src/admin/plugins/pageDetails/header/deletePage/DeletePage.tsx b/packages/app-page-builder/src/admin/plugins/pageDetails/header/deletePage/DeletePage.tsx index 089fa129c45..7c7578f3b41 100644 --- a/packages/app-page-builder/src/admin/plugins/pageDetails/header/deletePage/DeletePage.tsx +++ b/packages/app-page-builder/src/admin/plugins/pageDetails/header/deletePage/DeletePage.tsx @@ -4,7 +4,7 @@ import { Tooltip } from "@webiny/ui/Tooltip"; import { ReactComponent as DeleteIcon } from "~/admin/assets/delete.svg"; import { usePagesPermissions } from "~/hooks/permissions"; import { useDeletePage } from "~/admin/views/Pages/hooks/useDeletePage"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { makeDecoratable } from "@webiny/react-composition"; import { usePage } from "~/admin/views/Pages/PageDetails"; @@ -15,14 +15,15 @@ export interface DeletePageProps { const DeletePage = makeDecoratable("DeletePage", (props: DeletePageProps) => { const { onDelete } = props; const { page } = usePage(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const { canDelete: pagesCanDelete } = usePagesPermissions(); const { openDialogDeletePage } = useDeletePage({ page, onDelete }); const folderId = page.wbyAco_location?.folderId; const canDelete = useMemo(() => { - return pagesCanDelete(page.createdBy?.id) && flp.canManageContent(folderId); - }, [flp, folderId]); + return pagesCanDelete(page.createdBy?.id) && canManageContent(folderId); + }, [canManageContent, folderId]); if (!canDelete) { return null; diff --git a/packages/app-page-builder/src/admin/plugins/pageDetails/header/editRevision/EditRevision.tsx b/packages/app-page-builder/src/admin/plugins/pageDetails/header/editRevision/EditRevision.tsx index 889ada66ccf..f88a42b88d8 100644 --- a/packages/app-page-builder/src/admin/plugins/pageDetails/header/editRevision/EditRevision.tsx +++ b/packages/app-page-builder/src/admin/plugins/pageDetails/header/editRevision/EditRevision.tsx @@ -9,7 +9,7 @@ import { i18n } from "@webiny/app/i18n"; import { useMutation } from "@apollo/react-hooks"; import { usePagesPermissions } from "~/hooks/permissions"; import { useNavigatePage } from "~/admin/hooks/useNavigatePage"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { usePage } from "~/admin/views/Pages/PageDetails"; import { makeDecoratable } from "@webiny/react-composition"; @@ -17,7 +17,8 @@ const t = i18n.ns("app-headless-cms/app-page-builder/page-details/header/edit"); const EditRevision = makeDecoratable("EditRevision", () => { const { canUpdate: pagesCanUpdate } = usePagesPermissions(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const [inProgress, setInProgress] = useState(); const { showSnackbar } = useSnackbar(); const [createPageFrom] = useMutation(CREATE_PAGE); @@ -46,8 +47,8 @@ const EditRevision = makeDecoratable("EditRevision", () => { const folderId = page.wbyAco_location?.folderId; const canEdit = useMemo(() => { - return pagesCanUpdate(page.createdBy?.id) && flp.canManageContent(folderId); - }, [flp, folderId]); + return pagesCanUpdate(page.createdBy?.id) && canManageContent(folderId); + }, [canManageContent, folderId]); if (!canEdit) { return null; diff --git a/packages/app-page-builder/src/admin/plugins/pageDetails/header/pageOptionsMenu/DuplicatePage/SecureDuplicatePage.tsx b/packages/app-page-builder/src/admin/plugins/pageDetails/header/pageOptionsMenu/DuplicatePage/SecureDuplicatePage.tsx index 855a1d4b5ab..77245349ea4 100644 --- a/packages/app-page-builder/src/admin/plugins/pageDetails/header/pageOptionsMenu/DuplicatePage/SecureDuplicatePage.tsx +++ b/packages/app-page-builder/src/admin/plugins/pageDetails/header/pageOptionsMenu/DuplicatePage/SecureDuplicatePage.tsx @@ -1,5 +1,5 @@ import React, { useMemo } from "react"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { usePage } from "~/admin/views/Pages/PageDetails"; import { usePagesPermissions } from "~/hooks/permissions"; import { DuplicatePage } from "./DuplicatePage"; @@ -7,7 +7,8 @@ import { DuplicatePage } from "./DuplicatePage"; export const SecureDuplicatePage = DuplicatePage.createDecorator(Original => { return function SecurePageDetailsDuplicatePageRenderer() { const { page } = usePage(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const { canWrite: pagesCanWrite } = usePagesPermissions(); const canDuplicate = useMemo(() => { @@ -17,10 +18,9 @@ export const SecureDuplicatePage = DuplicatePage.createDecorator(Original => { } return ( - pagesCanWrite(page.createdBy.id) && - flp.canManageContent(page.wbyAco_location.folderId) + pagesCanWrite(page.createdBy.id) && canManageContent(page.wbyAco_location.folderId) ); - }, [flp, page]); + }, [canManageContent, page]); if (!canDuplicate) { return null; diff --git a/packages/app-page-builder/src/admin/plugins/pageDetails/header/pageOptionsMenu/PageOptionsMenu.tsx b/packages/app-page-builder/src/admin/plugins/pageDetails/header/pageOptionsMenu/PageOptionsMenu.tsx index 7fddcdaab67..0a55b4db39a 100644 --- a/packages/app-page-builder/src/admin/plugins/pageDetails/header/pageOptionsMenu/PageOptionsMenu.tsx +++ b/packages/app-page-builder/src/admin/plugins/pageDetails/header/pageOptionsMenu/PageOptionsMenu.tsx @@ -21,7 +21,7 @@ import { plugins } from "@webiny/plugins"; import { PbPageData, PbPageDetailsHeaderRightOptionsMenuItemPlugin, PbPageTemplate } from "~/types"; import { SecureView } from "@webiny/app-security"; import { useAdminPageBuilder } from "~/admin/hooks/useAdminPageBuilder"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { useTemplatesPermissions } from "~/hooks/permissions"; import { PreviewPage } from "./PreviewPage"; import { DuplicatePage } from "./DuplicatePage"; @@ -84,13 +84,14 @@ const PageOptionsMenu = (props: PageOptionsMenuProps) => { [page] ); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const { canCreate: templatesCanCreate } = useTemplatesPermissions(); const canCreateTemplate = templatesCanCreate(); const folderId = page.wbyAco_location?.folderId; - const flpCanManageContent = flp.canManageContent(folderId); + const flpCanManageContent = canManageContent(folderId); const isTemplatePage = page.content?.data?.template; return ( diff --git a/packages/app-page-builder/src/admin/plugins/pageDetails/header/publishRevision/PublishRevision.tsx b/packages/app-page-builder/src/admin/plugins/pageDetails/header/publishRevision/PublishRevision.tsx index 6c1b1c70926..73f7988a011 100644 --- a/packages/app-page-builder/src/admin/plugins/pageDetails/header/publishRevision/PublishRevision.tsx +++ b/packages/app-page-builder/src/admin/plugins/pageDetails/header/publishRevision/PublishRevision.tsx @@ -7,7 +7,7 @@ import { ReactComponent as PublishIcon } from "@material-design-icons/svg/round/ import { ReactComponent as UnpublishIcon } from "@material-design-icons/svg/round/settings_backup_restore.svg"; import { makeDecoratable } from "@webiny/app-admin"; import { usePagesPermissions } from "~/hooks/permissions"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { usePage } from "~/admin/views/Pages/PageDetails"; import { usePublishRevisionHandler } from "../../pageRevisions/usePublishRevisionHandler"; @@ -15,7 +15,8 @@ const t = i18n.ns("app-headless-cms/app-page-builder/page-details/header/publish const PublishRevision = () => { const { canPublish, canUnpublish, hasPermissions } = usePagesPermissions(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const { page } = usePage(); const { publishRevision, unpublishRevision } = usePublishRevisionHandler(); @@ -45,7 +46,7 @@ const PublishRevision = () => { }); const folderId = page.wbyAco_location?.folderId; - if (!hasPermissions() || !flp.canManageContent(folderId)) { + if (!hasPermissions() || !canManageContent(folderId)) { return null; } diff --git a/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureDeleteRevisionMenuOption.tsx b/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureDeleteRevisionMenuOption.tsx index cf50324561c..646a7e4fb44 100644 --- a/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureDeleteRevisionMenuOption.tsx +++ b/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureDeleteRevisionMenuOption.tsx @@ -1,6 +1,6 @@ import React, { useMemo } from "react"; import { usePagesPermissions } from "~/hooks/permissions"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { DeleteRevisionMenuOption, @@ -10,14 +10,14 @@ import { export const SecureDeleteRevisionMenuOption = (props: DeleteRevisionMenuOptionProps) => { const { page } = props; const { canDelete: pagesCanDelete } = usePagesPermissions(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const hasAccess = useMemo(() => { return ( - pagesCanDelete(page?.createdBy?.id) && - flp.canManageContent(page.wbyAco_location?.folderId) + pagesCanDelete(page?.createdBy?.id) && canManageContent(page.wbyAco_location?.folderId) ); - }, [page]); + }, [page, canManageContent]); if (!hasAccess) { return null; diff --git a/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureEditRevisionMenuOption.tsx b/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureEditRevisionMenuOption.tsx index 0e6bccc1f44..193e4de88a5 100644 --- a/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureEditRevisionMenuOption.tsx +++ b/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureEditRevisionMenuOption.tsx @@ -1,20 +1,20 @@ import React, { useMemo } from "react"; import { usePagesPermissions } from "~/hooks/permissions"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { EditRevisionMenuOption, EditRevisionMenuOptionProps } from "./EditRevisionMenuOption"; export const SecureEditRevisionMenuOption = (props: EditRevisionMenuOptionProps) => { const { page } = props; const { canUpdate: pagesCanUpdate } = usePagesPermissions(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const hasAccess = useMemo(() => { return ( - pagesCanUpdate(page?.createdBy?.id) && - flp.canManageContent(page.wbyAco_location?.folderId) + pagesCanUpdate(page?.createdBy?.id) && canManageContent(page.wbyAco_location?.folderId) ); - }, [page]); + }, [page, canManageContent]); if (!hasAccess) { return null; diff --git a/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureNewRevisionFromCurrent.tsx b/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureNewRevisionFromCurrent.tsx index 409fa88e58b..b0b02da0fb9 100644 --- a/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureNewRevisionFromCurrent.tsx +++ b/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureNewRevisionFromCurrent.tsx @@ -1,17 +1,18 @@ import React, { useMemo } from "react"; import { usePagesPermissions } from "~/hooks/permissions"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { NewRevisionFromCurrent, NewRevisionFromCurrentProps } from "./NewRevisionFromCurrent"; export const SecureNewRevisionFromCurrent = (props: NewRevisionFromCurrentProps) => { const { page } = props; const { canCreate: pagesCanCreate } = usePagesPermissions(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const hasAccess = useMemo(() => { - return pagesCanCreate() && flp.canManageContent(page.wbyAco_location?.folderId); - }, [page]); + return pagesCanCreate() && canManageContent(page.wbyAco_location?.folderId); + }, [page, canManageContent]); if (!hasAccess) { return null; diff --git a/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecurePublishPageMenuOption.tsx b/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecurePublishPageMenuOption.tsx index 90646e2cb29..9310ee05826 100644 --- a/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecurePublishPageMenuOption.tsx +++ b/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecurePublishPageMenuOption.tsx @@ -1,6 +1,6 @@ import React, { useMemo } from "react"; import { usePagesPermissions } from "~/hooks/permissions"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { PublishPageMenuOptionProps, PublishPageMenuOption } from "./PublishPageMenuOption"; import { usePage } from "~/admin/views/Pages/PageDetails"; @@ -8,11 +8,12 @@ import { usePage } from "~/admin/views/Pages/PageDetails"; export const SecurePublishPageMenuOption = (props: PublishPageMenuOptionProps) => { const { page } = usePage(); const { canPublish: pagesCanPublish } = usePagesPermissions(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const hasAccess = useMemo(() => { - return pagesCanPublish() && flp.canManageContent(page.wbyAco_location?.folderId); - }, [page]); + return pagesCanPublish() && canManageContent(page.wbyAco_location?.folderId); + }, [page, canManageContent]); if (!hasAccess) { return null; diff --git a/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureUnpublishPageMenuOption.tsx b/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureUnpublishPageMenuOption.tsx index e5bd121e5a3..6e130ca81ce 100644 --- a/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureUnpublishPageMenuOption.tsx +++ b/packages/app-page-builder/src/admin/plugins/pageDetails/pageRevisions/MenuOptions/SecureUnpublishPageMenuOption.tsx @@ -1,17 +1,18 @@ import React, { useMemo } from "react"; import { usePagesPermissions } from "~/hooks/permissions"; -import { useFolders } from "@webiny/app-aco"; +import { useGetFolderLevelPermission } from "@webiny/app-aco"; import { UnpublishPageMenuOption, UnpublishPageMenuOptionProps } from "./UnpublishPageMenuOption"; export const SecureUnpublishPageMenuOption = (props: UnpublishPageMenuOptionProps) => { const { page } = props; const { canUnpublish: pagesCanUnpublish } = usePagesPermissions(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const hasAccess = useMemo(() => { - return pagesCanUnpublish() && flp.canManageContent(page.wbyAco_location?.folderId); - }, [page]); + return pagesCanUnpublish() && canManageContent(page.wbyAco_location?.folderId); + }, [page, canManageContent]); if (!hasAccess) { return null; diff --git a/packages/app-page-builder/src/admin/views/Pages/Table/Main.tsx b/packages/app-page-builder/src/admin/views/Pages/Table/Main.tsx index 4b87fa7c569..064b859e4fd 100644 --- a/packages/app-page-builder/src/admin/views/Pages/Table/Main.tsx +++ b/packages/app-page-builder/src/admin/views/Pages/Table/Main.tsx @@ -1,7 +1,7 @@ import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; import debounce from "lodash/debounce"; import { i18n } from "@webiny/app/i18n"; -import { useCreateDialog, useFolders } from "@webiny/app-aco"; +import { useCreateDialog, useGetFolderLevelPermission } from "@webiny/app-aco"; import { CircularProgress } from "@webiny/ui/Progress"; import { Scrollbar } from "@webiny/ui/Scrollbar"; import CategoriesDialog from "~/admin/views/Categories/CategoriesDialog"; @@ -45,15 +45,18 @@ export const Main = ({ folderId: initialFolderId }: Props) => { // We check permissions on two layers - security and folder level permissions. const { canCreate } = usePagesPermissions(); - const { folderLevelPermissions: flp } = useFolders(); + const { getFolderLevelPermission: canManageStructure } = + useGetFolderLevelPermission("canManageStructure"); + const { getFolderLevelPermission: canManageContent } = + useGetFolderLevelPermission("canManageContent"); const canCreateFolder = useMemo(() => { - return flp.canManageStructure(folderId); - }, [flp, folderId]); + return canManageStructure(folderId); + }, [canManageStructure, folderId]); const canCreateContent = useMemo(() => { - return canCreate() && flp.canManageContent(folderId); - }, [flp, folderId]); + return canCreate() && canManageContent(folderId); + }, [canManageContent, folderId]); const { innerHeight: windowHeight } = window; const [tableHeight, setTableHeight] = useState(0);