Skip to content

Commit

Permalink
Passes the user to the restricted function of the block settings
Browse files Browse the repository at this point in the history
The restricted function is called inside a useMemo. So we can't call
useSelector on it. To work around this, we get the user before useMemo
and pass it to the restricted function.
  • Loading branch information
wesleybl committed Aug 26, 2024
1 parent c850615 commit ef2c285
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/volto-slate/news/6264.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Passes the `user` to the `restricted` function of the block settings. @wesleybl
6 changes: 5 additions & 1 deletion packages/volto-slate/src/blocks/Text/SlashMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { filter, isEmpty } from 'lodash';
import { Menu } from 'semantic-ui-react';
import { useIntl, FormattedMessage } from 'react-intl';
import { Icon } from '@plone/volto/components';
import { useSelector } from 'react-redux';

const emptySlateBlock = () => ({
value: [
Expand Down Expand Up @@ -108,6 +109,8 @@ const PersistentSlashMenu = ({ editor }) => {
} = props;
const disableNewBlocks = data?.disableNewBlocks || detached;

const user = useSelector((state) => state.users?.user);

const [slashMenuSelected, setSlashMenuSelected] = React.useState(0);

const hasAllowedBlocks = !isEmpty(allowedBlocks);
Expand All @@ -122,7 +125,7 @@ const PersistentSlashMenu = ({ editor }) => {
hasAllowedBlocks
? allowedBlocks.includes(item.id)
: typeof item.restricted === 'function'
? !item.restricted({ properties, block: item })
? !item.restricted({ properties, block: item, user: user })
: !item.restricted,
)
.filter((block) => Boolean(block.title && block.id))
Expand Down Expand Up @@ -152,6 +155,7 @@ const PersistentSlashMenu = ({ editor }) => {
properties,
slashCommand,
hasAllowedBlocks,
user,
],
);

Expand Down

0 comments on commit ef2c285

Please sign in to comment.