PromucFlow_constructor/app/client/src/navigation/FocusEntity.ts
Hetu Nandu 721767d742
feat: URL Navigation for Widgets (#20202)
## Description

Widget selection is driven by URL changes. This would fix browser
navigation for users as they can use browser back/forward buttons to
travel across older contexts on Appsmith.

> Fixing browser URL navigation for widgets

Fixes #19571


Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video


## Type of change

> Please delete options that are not relevant.

- New feature (non-breaking change which adds functionality)


## How Has This Been Tested?

- Manual
- Jest
- Cypress

### Test Plan
> Add Testsmith [test
cases](https://github.com/appsmithorg/TestSmith/issues/2171) links that
relate to this PR

### Issues raised during DP testing
- [X] When a selected widget is below viewport and user refreshes the
page, then the widget property pane is open but the page does not
navigate to the selected widget
https://loom.com/share/09f1eda2f02d474981a0d48e4a6419ec

- [ ] Drop 2 widgets one at a time > Delete both the widgets > Now click
on back button of the browser > Observe the url it shows the widget id
in the URL but the canvas remains empty
https://loom.com/share/53cae28a5d224e67b783c8ccf53745f5
Dev Response: This issue is valid but not a major inconvenience. We will
try to track it and see if it needed to be addressed. Many other web
tools do not handle such cases


- [X] Canvas scrolls down when all widgets are selected.
https://loom.com/share/c8a68dadcdb040779abd3a73bde2b06c

- [X] Widget is not getting highlighted when added from the API editor
page. Please refer to the attached
video:-https://jiju8jbmwa.vmaker.com/record/IkwiAqFgafK9dVmu



## Checklist:
### Dev activity
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


### QA activity:
- [ ] Test plan has been approved by relevant developers
- [x] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test

---------

Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2023-02-21 19:08:16 +05:30

181 lines
4.9 KiB
TypeScript

import { matchPath } from "react-router";
import {
API_EDITOR_ID_PATH,
BUILDER_CUSTOM_PATH,
BUILDER_PATH,
BUILDER_PATH_DEPRECATED,
DATA_SOURCES_EDITOR_ID_PATH,
INTEGRATION_EDITOR_PATH,
JS_COLLECTION_ID_PATH,
QUERIES_EDITOR_ID_PATH,
WIDGETS_EDITOR_ID_PATH,
} from "constants/routes";
import { SAAS_EDITOR_DATASOURCE_ID_PATH } from "pages/Editor/SaaSEditor/constants";
import { SAAS_EDITOR_API_ID_PATH } from "pages/Editor/SaaSEditor/constants";
import { getQueryParamsFromString } from "utils/getQueryParamsObject";
export enum FocusEntity {
PAGE = "PAGE",
API = "API",
CANVAS = "CANVAS",
DATASOURCE = "DATASOURCE",
QUERY = "QUERY",
JS_OBJECT = "JS_OBJECT",
PROPERTY_PANE = "PROPERTY_PANE",
NONE = "NONE",
}
export const FocusStoreHierarchy: Partial<Record<FocusEntity, FocusEntity>> = {
[FocusEntity.PROPERTY_PANE]: FocusEntity.CANVAS,
};
export type FocusEntityInfo = {
entity: FocusEntity;
id: string;
pageId?: string;
};
/**
* Method to indicate if the URL is of type API, Query etc.,
* and not anything else
* @param path
* @returns
*/
export function shouldStoreURLForFocus(path: string) {
const entityTypesToStore = [
FocusEntity.QUERY,
FocusEntity.API,
FocusEntity.JS_OBJECT,
FocusEntity.DATASOURCE,
];
const entity = identifyEntityFromPath(path)?.entity;
return entityTypesToStore.indexOf(entity) >= 0;
}
/**
* parse search string and get branch
* @param searchString
* @returns
*/
const fetchGitBranch = (searchString: string | undefined) => {
const existingParams =
getQueryParamsFromString(searchString?.substring(1)) || {};
return existingParams.branch;
};
/**
* Compare if both the params are on same branch
* @param previousParamString
* @param currentParamStaring
* @returns
*/
export function isSameBranch(
previousParamString: string,
currentParamStaring: string,
) {
const previousBranch = fetchGitBranch(previousParamString);
const currentBranch = fetchGitBranch(currentParamStaring);
return previousBranch === currentBranch;
}
export function identifyEntityFromPath(path: string): FocusEntityInfo {
const match = matchPath<{
apiId?: string;
datasourceId?: string;
pluginPackageName?: string;
queryId?: string;
appId?: string;
pageId?: string;
collectionId?: string;
widgetIds?: string;
selectedTab?: string; // Datasource creation/list screen
}>(path, {
path: [
BUILDER_PATH_DEPRECATED + API_EDITOR_ID_PATH,
BUILDER_PATH + API_EDITOR_ID_PATH,
BUILDER_CUSTOM_PATH + API_EDITOR_ID_PATH,
BUILDER_PATH_DEPRECATED + QUERIES_EDITOR_ID_PATH,
BUILDER_PATH + QUERIES_EDITOR_ID_PATH,
BUILDER_CUSTOM_PATH + QUERIES_EDITOR_ID_PATH,
BUILDER_PATH_DEPRECATED + DATA_SOURCES_EDITOR_ID_PATH,
BUILDER_PATH + DATA_SOURCES_EDITOR_ID_PATH,
BUILDER_CUSTOM_PATH + DATA_SOURCES_EDITOR_ID_PATH,
BUILDER_PATH_DEPRECATED + INTEGRATION_EDITOR_PATH,
BUILDER_PATH + INTEGRATION_EDITOR_PATH,
BUILDER_CUSTOM_PATH + INTEGRATION_EDITOR_PATH,
BUILDER_PATH + SAAS_EDITOR_DATASOURCE_ID_PATH,
BUILDER_CUSTOM_PATH + SAAS_EDITOR_DATASOURCE_ID_PATH,
BUILDER_PATH_DEPRECATED + SAAS_EDITOR_API_ID_PATH,
BUILDER_PATH + SAAS_EDITOR_API_ID_PATH,
BUILDER_CUSTOM_PATH + SAAS_EDITOR_API_ID_PATH,
BUILDER_PATH_DEPRECATED + JS_COLLECTION_ID_PATH,
BUILDER_PATH + JS_COLLECTION_ID_PATH,
BUILDER_CUSTOM_PATH + JS_COLLECTION_ID_PATH,
BUILDER_PATH + WIDGETS_EDITOR_ID_PATH,
BUILDER_CUSTOM_PATH + WIDGETS_EDITOR_ID_PATH,
BUILDER_PATH_DEPRECATED + WIDGETS_EDITOR_ID_PATH,
BUILDER_PATH_DEPRECATED,
BUILDER_PATH,
BUILDER_CUSTOM_PATH,
],
exact: true,
});
if (!match) {
return { entity: FocusEntity.NONE, id: "", pageId: "" };
}
if (match.params.apiId) {
if (match.params.pluginPackageName) {
return {
entity: FocusEntity.QUERY,
id: match.params.apiId,
pageId: match.params.pageId,
};
}
return {
entity: FocusEntity.API,
id: match.params.apiId,
pageId: match.params.pageId,
};
}
if (match.params.datasourceId) {
return {
entity: FocusEntity.DATASOURCE,
id: match.params.datasourceId,
pageId: match.params.pageId,
};
}
if (match.params.selectedTab) {
return {
entity: FocusEntity.DATASOURCE,
id: match.params.selectedTab,
pageId: match.params.pageId,
};
}
if (match.params.queryId) {
return {
entity: FocusEntity.QUERY,
id: match.params.queryId,
pageId: match.params.pageId,
};
}
if (match.params.collectionId) {
return {
entity: FocusEntity.JS_OBJECT,
id: match.params.collectionId,
pageId: match.params.pageId,
};
}
if (match.params.widgetIds) {
return {
entity: FocusEntity.PROPERTY_PANE,
id: match.params.widgetIds,
pageId: match.params.pageId,
};
}
return { entity: FocusEntity.CANVAS, id: "", pageId: match.params.pageId };
}