diff --git a/app/client/src/pages/Editor/Explorer/Actions/ActionEntityContextMenu.tsx b/app/client/src/pages/Editor/Explorer/Actions/ActionEntityContextMenu.tsx index 456d1444bf..56ac660691 100644 --- a/app/client/src/pages/Editor/Explorer/Actions/ActionEntityContextMenu.tsx +++ b/app/client/src/pages/Editor/Explorer/Actions/ActionEntityContextMenu.tsx @@ -3,7 +3,6 @@ import { useDispatch, useSelector } from "react-redux"; import TreeDropdown from "components/editorComponents/actioncreator/TreeDropdown"; import { AppState } from "reducers"; -import { getNextEntityName } from "utils/AppsmithUtils"; import ContextMenuTrigger from "../ContextMenuTrigger"; import { @@ -15,18 +14,7 @@ import { import { initExplorerEntityNameEdit } from "actions/explorerActions"; import { ContextMenuPopoverModifiers } from "../helpers"; import { noop } from "lodash"; - -const useNewAPIName = () => { - // This takes into consideration only the current page widgets - // If we're moving to a different page, there could be a widget - // with the same name as the generated API name - // TODO: Figure out how to handle this scenario - const apiNames = useSelector((state: AppState) => - state.entities.actions.map((action) => action.config.name), - ); - return (name: string) => - apiNames.indexOf(name) > -1 ? getNextEntityName(name, apiNames) : name; -}; +import { useNewActionName } from "./helpers"; type EntityContextMenuProps = { id: string; @@ -35,7 +23,7 @@ type EntityContextMenuProps = { pageId: string; }; export const ActionEntityContextMenu = (props: EntityContextMenuProps) => { - const nextEntityName = useNewAPIName(); + const nextEntityName = useNewActionName(); const dispatch = useDispatch(); const copyActionToPage = useCallback( @@ -44,7 +32,7 @@ export const ActionEntityContextMenu = (props: EntityContextMenuProps) => { copyActionRequest({ id: actionId, destinationPageId: pageId, - name: nextEntityName(`${actionName}Copy`), + name: nextEntityName(`${actionName}Copy`, pageId), }), ), [dispatch, nextEntityName], @@ -56,7 +44,7 @@ export const ActionEntityContextMenu = (props: EntityContextMenuProps) => { id: actionId, destinationPageId, originalPageId: props.pageId, - name: nextEntityName(actionName), + name: nextEntityName(actionName, destinationPageId), }), ), [dispatch, nextEntityName, props.pageId], diff --git a/app/client/src/pages/Editor/Explorer/Actions/MoreActionsMenu.tsx b/app/client/src/pages/Editor/Explorer/Actions/MoreActionsMenu.tsx index e8a416a779..9a76bd6b83 100644 --- a/app/client/src/pages/Editor/Explorer/Actions/MoreActionsMenu.tsx +++ b/app/client/src/pages/Editor/Explorer/Actions/MoreActionsMenu.tsx @@ -2,7 +2,6 @@ import React, { useCallback } from "react"; import { useDispatch, useSelector } from "react-redux"; import { AppState } from "reducers"; -import { getNextEntityName } from "utils/AppsmithUtils"; import { moveActionRequest, @@ -13,18 +12,7 @@ import { import { ContextMenuPopoverModifiers } from "../helpers"; import { noop } from "lodash"; import TreeDropdown from "components/ads/TreeDropdown"; - -const useNewAPIName = () => { - // This takes into consideration only the current page widgets - // If we're moving to a different page, there could be a widget - // with the same name as the generated API name - // TODO: Figure out how to handle this scenario - const apiNames = useSelector((state: AppState) => - state.entities.actions.map((action) => action.config.name), - ); - return (name: string) => - apiNames.indexOf(name) > -1 ? getNextEntityName(name, apiNames) : name; -}; +import { useNewActionName } from "./helpers"; type EntityContextMenuProps = { id: string; @@ -33,7 +21,7 @@ type EntityContextMenuProps = { pageId: string; }; export const MoreActionsMenu = (props: EntityContextMenuProps) => { - const nextEntityName = useNewAPIName(); + const nextEntityName = useNewActionName(); const dispatch = useDispatch(); const copyActionToPage = useCallback( @@ -42,7 +30,7 @@ export const MoreActionsMenu = (props: EntityContextMenuProps) => { copyActionRequest({ id: actionId, destinationPageId: pageId, - name: nextEntityName(`${actionName}Copy`), + name: nextEntityName(`${actionName}Copy`, pageId), }), ), [dispatch, nextEntityName], @@ -54,7 +42,7 @@ export const MoreActionsMenu = (props: EntityContextMenuProps) => { id: actionId, destinationPageId, originalPageId: props.pageId, - name: nextEntityName(actionName), + name: nextEntityName(actionName, destinationPageId), }), ), [dispatch, nextEntityName, props.pageId], diff --git a/app/client/src/pages/Editor/Explorer/Actions/helpers.tsx b/app/client/src/pages/Editor/Explorer/Actions/helpers.tsx index 022066adea..0afb6ef6bb 100644 --- a/app/client/src/pages/Editor/Explorer/Actions/helpers.tsx +++ b/app/client/src/pages/Editor/Explorer/Actions/helpers.tsx @@ -1,4 +1,4 @@ -import React, { ReactNode } from "react"; +import React, { ReactNode, useMemo } from "react"; import { apiIcon, dbQueryIcon, MethodTag, QueryIcon } from "../ExplorerIcons"; import { PluginType } from "entities/Action"; import { generateReactKey } from "utils/generators"; @@ -15,6 +15,11 @@ import { ExplorerURLParams } from "../helpers"; import { Datasource } from "entities/Datasource"; import { Plugin } from "api/PluginApi"; import PluginGroup from "../PluginGroup/PluginGroup"; +import { useSelector } from "react-redux"; +import { AppState } from "reducers"; +import { groupBy } from "lodash"; +import { ActionData } from "reducers/entityReducers/actionsReducer"; +import { getNextEntityName } from "utils/AppsmithUtils"; export type ActionGroupConfig = { groupName: string; @@ -135,3 +140,25 @@ export const getPluginGroups = ( ); }); }; + +export const useNewActionName = () => { + // This takes into consideration only the current page widgets + // If we're moving to a different page, there could be a widget + // with the same name as the generated API name + // TODO: Figure out how to handle this scenario + const actions = useSelector((state: AppState) => state.entities.actions); + const groupedActions = useMemo(() => { + return groupBy(actions, "config.pageId"); + }, [actions]); + return (name: string, destinationPageId: string) => { + const pageActions = groupedActions[destinationPageId]; + // Get action names of the destination page only + const actionNames = pageActions + ? pageActions.map((action: ActionData) => action.config.name) + : []; + + return actionNames.indexOf(name) > -1 + ? getNextEntityName(name, actionNames) + : name; + }; +};