Skip to content

Commit

Permalink
refactoring usage of editorType to ideType to remove code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitakinger committed Jan 21, 2025
1 parent 2ca5993 commit dfe8f5c
Show file tree
Hide file tree
Showing 60 changed files with 303 additions and 354 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { datasourcesEditorIdURL } from "ee/RouteBuilder";
import { omit } from "lodash";
import { getQueryParams } from "utils/URLUtils";
import history from "utils/history";
import { useEditorType } from "ee/hooks";
import { useParentEntityInfo } from "ee/hooks/datasourceEditorHooks";
import { useParentEntityInfo } from "ee/IDE/hooks/useParentEntityInfo";
import { getIDETypeByUrl } from "ee/entities/IDE/utils";
import type { Plugin } from "entities/Plugin";

interface Props {
Expand All @@ -25,8 +25,8 @@ const DatasourceInfo = ({
plugin,
showEditButton,
}: Props) => {
const editorType = useEditorType(location.pathname);
const { parentEntityId } = useParentEntityInfo(editorType);
const ideType = getIDETypeByUrl(location.pathname);
const { parentEntityId } = useParentEntityInfo(ideType);

// eslint-disable-next-line react-perf/jsx-no-new-function-as-prop
const editDatasource = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import { isEmpty, omit } from "lodash";
import { getQueryParams } from "utils/URLUtils";
import { TableColumns } from "./TableColumns";
import { BOTTOMBAR_HEIGHT } from "./constants";
import { useEditorType } from "ee/hooks";
import { useParentEntityInfo } from "ee/hooks/datasourceEditorHooks";
import { useParentEntityInfo } from "ee/IDE/hooks/useParentEntityInfo";
import DatasourceInfo from "./DatasourceInfo";
import { getPlugin } from "ee/selectors/entitiesSelector";
import {
Expand All @@ -34,6 +33,7 @@ import {
} from "ee/utils/BusinessFeatures/permissionPageHelpers";
import { useFeatureFlag } from "utils/hooks/useFeatureFlag";
import { FEATURE_FLAG } from "ee/entities/FeatureFlag";
import { getIDETypeByUrl } from "ee/entities/IDE/utils";

interface Props {
datasourceId: string;
Expand All @@ -58,8 +58,8 @@ const DatasourceTab = (props: Props) => {

const plugin = useSelector((state) => getPlugin(state, pluginId || ""));

const editorType = useEditorType(location.pathname);
const { parentEntityId } = useParentEntityInfo(editorType);
const ideType = getIDETypeByUrl(location.pathname);
const { parentEntityId } = useParentEntityInfo(ideType);

const [selectedTable, setSelectedTable] = useState<string>();

Expand Down
19 changes: 19 additions & 0 deletions app/client/src/ce/IDE/hooks/useParentEntityInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ActionParentEntityType } from "ee/entities/Engine/actionHelpers";
import type { IDEType } from "ee/entities/IDE/constants";
import { useSelector } from "react-redux";
import {
getCurrentApplicationId,
getCurrentBasePageId,
} from "selectors/editorSelectors";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const useParentEntityInfo = (ideType: IDEType) => {
const appId = useSelector(getCurrentApplicationId);
const basePageId = useSelector(getCurrentBasePageId);

return {
editorId: appId || "",
parentEntityId: basePageId || "",
parentEntityType: ActionParentEntityType.PAGE,
};
};
13 changes: 7 additions & 6 deletions app/client/src/ce/actions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
saveActionName,
} from "actions/pluginActionActions";
import { saveJSObjectName } from "actions/jsActionActions";
import { IDE_TYPE, type IDEType } from "ee/entities/IDE/constants";

export const createNewQueryBasedOnParentEntity = (
entityId: string,
Expand Down Expand Up @@ -43,29 +44,29 @@ export const createNewJSCollectionBasedOnParentEntity = (
return createNewJSCollection(entityId, from);
};

export const saveActionNameBasedOnParentEntity = (
export const saveActionNameBasedOnIdeType = (
id: string,
name: string,
// Used in EE
// eslint-disable-next-line @typescript-eslint/no-unused-vars
parentEntityType: ActionParentEntityTypeInterface = ActionParentEntityType.PAGE,
ideType: IDEType = IDE_TYPE.App,
) => {
return saveActionName({ id, name });
};

export const saveJSObjectNameBasedOnParentEntity = (
export const saveJSObjectNameBasedOnIdeType = (
id: string,
name: string,
// Used in EE
// eslint-disable-next-line @typescript-eslint/no-unused-vars
parentEntityType: ActionParentEntityTypeInterface = ActionParentEntityType.PAGE,
ideType: IDEType = IDE_TYPE.App,
) => {
return saveJSObjectName({ id, name });
};

export const createNewApiActionBasedOnEditorType = (
export const createNewApiActionBasedOnIdeType = (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
editorType: string,
ideType: IDEType,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
editorId: string,
parentEntityId: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useFeatureFlag } from "utils/hooks/useFeatureFlag";
import { IDE_TYPE, type IDEType } from "ee/entities/IDE/constants";
import { FEATURE_FLAG } from "ee/entities/FeatureFlag";
import { useSelector } from "react-redux";
import { getPagePermissions } from "selectors/editorSelectors";
import { getHasCreateActionPermission } from "ee/utils/BusinessFeatures/permissionPageHelpers";

export const useCreateActionsPermissions = (ideType: IDEType) => {
const isFeatureEnabled = useFeatureFlag(FEATURE_FLAG.license_gac_enabled);
const pagePermissions = useSelector(getPagePermissions);

switch (ideType) {
case IDE_TYPE.App: {
return getHasCreateActionPermission(isFeatureEnabled, pagePermissions);
}
default: {
return true;
}
}
};
25 changes: 4 additions & 21 deletions app/client/src/ce/hooks/datasourceEditorHooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
GENERATE_NEW_PAGE_BUTTON_TEXT,
createMessage,
} from "ee/constants/messages";
import { ActionParentEntityType } from "ee/entities/Engine/actionHelpers";
import { FEATURE_FLAG } from "ee/entities/FeatureFlag";
import type { AppState } from "ee/reducers";
import { getPlugin } from "ee/selectors/entitiesSelector";
Expand All @@ -18,17 +17,13 @@ import NewActionButton from "pages/Editor/DataSourceEditor/NewActionButton";
import { useShowPageGenerationOnHeader } from "pages/Editor/DataSourceEditor/hooks";
import React from "react";
import { useDispatch, useSelector } from "react-redux";
import {
getCurrentApplicationId,
getCurrentBasePageId,
getPagePermissions,
} from "selectors/editorSelectors";
import { getPagePermissions } from "selectors/editorSelectors";
import { getIsAnvilEnabledInCurrentApplication } from "layoutSystems/anvil/integrations/selectors";
import { isEnabledForPreviewData } from "utils/editorContextUtils";
import { useFeatureFlag } from "utils/hooks/useFeatureFlag";
import { EditorNames } from "./";
import { getCurrentApplication } from "ee/selectors/applicationSelectors";
import { openGeneratePageModal } from "pages/Editor/GeneratePage/store/generatePageActions";
import { IDE_TYPE, type IDEType } from "ee/entities/IDE/constants";

export interface HeaderActionProps {
datasource: Datasource | ApiDatasourceForm | undefined;
Expand All @@ -38,7 +33,7 @@ export interface HeaderActionProps {
}

export const useHeaderActions = (
editorType: string,
ideType: IDEType,
{
datasource,
isPluginAuthorized,
Expand Down Expand Up @@ -77,7 +72,7 @@ export const useHeaderActions = (
? false
: !!isPluginAllowedToPreviewData;

if (editorType === EditorNames.APPLICATION) {
if (ideType === IDE_TYPE.App) {
const canCreateDatasourceActions = hasCreateDSActionPermissionInApp({
isEnabled: isFeatureEnabled,
dsPermissions: datasource?.userPermissions ?? [],
Expand Down Expand Up @@ -141,15 +136,3 @@ export const useHeaderActions = (

return {};
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const useParentEntityInfo = (editorType: string) => {
const appId = useSelector(getCurrentApplicationId);
const basePageId = useSelector(getCurrentBasePageId);

return {
editorId: appId || "",
parentEntityId: basePageId || "",
parentEntityType: ActionParentEntityType.PAGE,
};
};
25 changes: 0 additions & 25 deletions app/client/src/ce/hooks/hooks.test.ts

This file was deleted.

35 changes: 0 additions & 35 deletions app/client/src/ce/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,4 @@
import {
BUILDER_BASE_PATH_DEPRECATED,
BUILDER_VIEWER_PATH_PREFIX,
} from "constants/routes";
import { useEffect, type RefObject } from "react";
import { matchPath } from "react-router";

export const EditorNames = {
APPLICATION: "app",
};

export interface EditorType {
[key: string]: string;
}

export const editorType: EditorType = {
[BUILDER_VIEWER_PATH_PREFIX]: EditorNames.APPLICATION,
[BUILDER_BASE_PATH_DEPRECATED]: EditorNames.APPLICATION,
};

// Utility function to get editor type based on path
// to be used in non react functions
export const getEditorType = (path: string) => {
const basePath = matchPath(path, {
path: [BUILDER_VIEWER_PATH_PREFIX, BUILDER_BASE_PATH_DEPRECATED],
});

return basePath
? editorType[basePath.path]
: editorType[BUILDER_VIEWER_PATH_PREFIX];
};

// custom hook to get editor type based on path
export const useEditorType = (path: string) => {
return getEditorType(path);
};

export function useOutsideClick<T extends HTMLElement>(
ref: RefObject<T>,
Expand Down
2 changes: 1 addition & 1 deletion app/client/src/ce/navigation/FocusSetters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { PluginType } from "entities/Plugin";
import type { FocusEntityInfo } from "navigation/FocusEntity";
import { FocusEntity } from "navigation/FocusEntity";
import { getQueryEntityItemUrl } from "../pages/Editor/IDE/EditorPane/Query/utils";
import { getQueryEntityItemUrl } from "ee/pages/Editor/IDE/EditorPane/Query/utils/getQueryEntityItemUrl";

export function setSelectedDatasource(id?: string) {
if (id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import React from "react";
import ExplorerJSCollectionEntity from "pages/Editor/Explorer/JSActions/JSActionEntity";
import type { ActionParentEntityTypeInterface } from "ee/entities/Engine/actionHelpers";
import { Flex } from "@appsmith/ads";
import type { EntityItem } from "ee/entities/IDE/constants";

export interface JSListItemProps {
item: EntityItem;
isActive: boolean;
parentEntityId: string;
parentEntityType: ActionParentEntityTypeInterface;
}

export const JSListItem = (props: JSListItemProps) => {
const { isActive, item, parentEntityId, parentEntityType } = props;
const { isActive, item, parentEntityId } = props;

return (
<Flex data-testid="t--ide-list-item" flexDirection={"column"}>
Expand All @@ -21,7 +19,6 @@ export const JSListItem = (props: JSListItemProps) => {
isActive={isActive}
key={item.key}
parentEntityId={parentEntityId}
parentEntityType={parentEntityType}
searchKeyword={""}
step={1}
/>
Expand Down
2 changes: 1 addition & 1 deletion app/client/src/ce/pages/Editor/IDE/EditorPane/JS/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ADD_PATH } from "ee/constants/routes/appRoutes";
import history from "utils/history";
import { FocusEntity, identifyEntityFromPath } from "navigation/FocusEntity";
import { useModuleOptions } from "ee/utils/moduleInstanceHelpers";
import { getJSUrl } from "ee/pages/Editor/IDE/EditorPane/JS/utils";
import { getJSUrl } from "ee/pages/Editor/IDE/EditorPane/JS/utils/getJSUrl";
import { getIDEViewMode } from "selectors/ideSelectors";
import { EditorViewMode } from "ee/entities/IDE/constants";
import { setListViewActiveState } from "actions/ideActions";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getJSEntityItemUrl } from "./getJSEntityItemUrl";
import urlBuilder from "ee/entities/URLRedirect/URLAssembly";
import { PluginType } from "entities/Plugin";

describe("getJSEntityItemUrl", () => {
urlBuilder.setCurrentBasePageId("0123456789abcdef00000000");
it("returns a JS url", () => {
const url = getJSEntityItemUrl(
{
title: "TestTitle",
key: "abc",
type: PluginType.JS,
},
"0123456789abcdef00000000",
);

expect(url).toEqual(
"/app/application/page-0123456789abcdef00000000/edit/jsObjects/abc",
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { EntityItem } from "ee/entities/IDE/constants";
import { jsCollectionIdURL } from "ee/RouteBuilder";

export const getJSEntityItemUrl = (
item: EntityItem,
basePageId: string,
): string => {
return jsCollectionIdURL({
baseCollectionId: item.key,
basePageId,
});
};
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
import { getJSEntityItemUrl, getJSUrl } from "./utils";
import { getJSUrl } from "./getJSUrl";
import urlBuilder from "ee/entities/URLRedirect/URLAssembly";
import { PluginType } from "entities/Plugin";
import type { FocusEntityInfo } from "navigation/FocusEntity";
import { FocusEntity } from "navigation/FocusEntity";
import { EditorState } from "ee/entities/IDE/constants";

describe("getJSEntityItemUrl", () => {
urlBuilder.setCurrentBasePageId("0123456789abcdef00000000");
it("returns a JS url", () => {
const url = getJSEntityItemUrl(
{
title: "TestTitle",
key: "abc",
type: PluginType.JS,
},
"0123456789abcdef00000000",
);

expect(url).toEqual(
"/app/application/page-0123456789abcdef00000000/edit/jsObjects/abc",
);
});
});

describe("getJSUrl", () => {
urlBuilder.setCurrentBasePageId("0123456789abcdef00000000");
it("returns a JS collection url", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import type { EntityItem } from "ee/entities/IDE/constants";
import {
jsCollectionAddURL,
jsCollectionIdURL,
jsCollectionListURL,
} from "ee/RouteBuilder";
import { FocusEntity, type FocusEntityInfo } from "navigation/FocusEntity";

export const getJSEntityItemUrl = (
item: EntityItem,
basePageId: string,
): string => {
return jsCollectionIdURL({
baseCollectionId: item.key,
basePageId,
});
};

export const getJSUrl = (
item: FocusEntityInfo,
add: boolean = true,
Expand Down
Loading

0 comments on commit dfe8f5c

Please sign in to comment.