2024-01-23 04:52:50 +00:00
|
|
|
import type { FocusEntityInfo } from "navigation/FocusEntity";
|
2024-02-02 05:06:02 +00:00
|
|
|
import { FocusEntity, identifyEntityFromPath } from "navigation/FocusEntity";
|
2024-02-27 05:12:27 +00:00
|
|
|
import { all, call, put, select, takeEvery } from "redux-saga/effects";
|
2024-04-26 11:36:57 +00:00
|
|
|
import { getJSTabs, getQueryTabs } from "selectors/ideSelectors";
|
2024-02-27 05:12:27 +00:00
|
|
|
import {
|
|
|
|
|
setIdeEditorViewMode,
|
|
|
|
|
setJSTabs,
|
|
|
|
|
setQueryTabs,
|
|
|
|
|
} from "actions/ideActions";
|
2024-02-02 05:06:02 +00:00
|
|
|
import history from "../utils/history";
|
2024-04-05 12:26:17 +00:00
|
|
|
import {
|
|
|
|
|
jsCollectionAddURL,
|
|
|
|
|
jsCollectionListURL,
|
|
|
|
|
queryAddURL,
|
|
|
|
|
queryListURL,
|
|
|
|
|
} from "@appsmith/RouteBuilder";
|
2024-02-02 05:06:02 +00:00
|
|
|
import type { EntityItem } from "@appsmith/entities/IDE/constants";
|
2024-04-19 06:49:24 +00:00
|
|
|
import { getCurrentPageId } from "@appsmith/selectors/entitiesSelector";
|
2024-02-02 05:06:02 +00:00
|
|
|
import { getQueryEntityItemUrl } from "@appsmith/pages/Editor/IDE/EditorPane/Query/utils";
|
|
|
|
|
import { getJSEntityItemUrl } from "@appsmith/pages/Editor/IDE/EditorPane/JS/utils";
|
|
|
|
|
import log from "loglevel";
|
2024-02-27 05:12:27 +00:00
|
|
|
import type { ReduxAction } from "@appsmith/constants/ReduxActionConstants";
|
|
|
|
|
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
|
|
|
|
|
import type { EditorViewMode } from "@appsmith/entities/IDE/constants";
|
|
|
|
|
import { retrieveIDEViewMode, storeIDEViewMode } from "utils/storage";
|
2024-04-19 06:49:24 +00:00
|
|
|
import {
|
|
|
|
|
selectJSSegmentEditorTabs,
|
|
|
|
|
selectQuerySegmentEditorTabs,
|
|
|
|
|
} from "@appsmith/selectors/appIDESelectors";
|
2024-01-23 04:52:50 +00:00
|
|
|
|
|
|
|
|
export function* updateIDETabsOnRouteChangeSaga(entityInfo: FocusEntityInfo) {
|
|
|
|
|
const { entity, id } = entityInfo;
|
2024-02-20 05:01:57 +00:00
|
|
|
if (
|
|
|
|
|
entity === FocusEntity.JS_OBJECT ||
|
|
|
|
|
entity === FocusEntity.JS_MODULE_INSTANCE
|
|
|
|
|
) {
|
2024-01-23 04:52:50 +00:00
|
|
|
const jsTabs: string[] = yield select(getJSTabs);
|
|
|
|
|
const newTabs: string[] = yield call(getUpdatedTabs, id, jsTabs);
|
|
|
|
|
yield put(setJSTabs(newTabs));
|
|
|
|
|
}
|
2024-02-20 05:01:57 +00:00
|
|
|
if (
|
|
|
|
|
entity === FocusEntity.QUERY ||
|
|
|
|
|
entity === FocusEntity.QUERY_MODULE_INSTANCE
|
|
|
|
|
) {
|
2024-01-23 04:52:50 +00:00
|
|
|
const queryTabs: string[] = yield select(getQueryTabs);
|
|
|
|
|
const newTabs: string[] = yield call(getUpdatedTabs, id, queryTabs);
|
|
|
|
|
yield put(setQueryTabs(newTabs));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* getUpdatedTabs(newId: string, currentTabs: string[]) {
|
|
|
|
|
if (currentTabs.includes(newId)) return currentTabs;
|
2024-04-26 11:36:57 +00:00
|
|
|
const newTabs = [...currentTabs, newId];
|
2024-01-23 04:52:50 +00:00
|
|
|
return newTabs;
|
|
|
|
|
}
|
2024-02-02 05:06:02 +00:00
|
|
|
|
|
|
|
|
export function* handleJSEntityRedirect(deletedId: string) {
|
|
|
|
|
const pageId: string = yield select(getCurrentPageId);
|
2024-04-19 06:49:24 +00:00
|
|
|
const jsTabs: EntityItem[] = yield select(selectJSSegmentEditorTabs);
|
|
|
|
|
const redirectAction = getNextEntityAfterRemove(deletedId, jsTabs);
|
2024-02-02 05:06:02 +00:00
|
|
|
switch (redirectAction.action) {
|
2024-04-05 12:26:17 +00:00
|
|
|
case RedirectAction.LIST:
|
|
|
|
|
history.push(jsCollectionListURL({ pageId }));
|
2024-02-02 05:06:02 +00:00
|
|
|
break;
|
|
|
|
|
case RedirectAction.ITEM:
|
|
|
|
|
if (!redirectAction.payload) {
|
|
|
|
|
log.error("Redirect item does not have a payload");
|
2024-02-20 05:01:57 +00:00
|
|
|
history.push(jsCollectionAddURL({ pageId }));
|
2024-02-02 05:06:02 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
const { payload } = redirectAction;
|
|
|
|
|
history.push(getJSEntityItemUrl(payload, pageId));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function* handleQueryEntityRedirect(deletedId: string) {
|
|
|
|
|
const pageId: string = yield select(getCurrentPageId);
|
2024-04-19 06:49:24 +00:00
|
|
|
const queryTabs: EntityItem[] = yield select(selectQuerySegmentEditorTabs);
|
|
|
|
|
const redirectAction = getNextEntityAfterRemove(deletedId, queryTabs);
|
2024-02-02 05:06:02 +00:00
|
|
|
switch (redirectAction.action) {
|
2024-04-05 12:26:17 +00:00
|
|
|
case RedirectAction.LIST:
|
|
|
|
|
history.push(queryListURL({ pageId }));
|
2024-02-02 05:06:02 +00:00
|
|
|
break;
|
|
|
|
|
case RedirectAction.ITEM:
|
|
|
|
|
if (!redirectAction.payload) {
|
2024-02-20 05:01:57 +00:00
|
|
|
history.push(queryAddURL({ pageId }));
|
2024-02-02 05:06:02 +00:00
|
|
|
log.error("Redirect item does not have a payload");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
const { payload } = redirectAction;
|
|
|
|
|
history.push(getQueryEntityItemUrl(payload, pageId));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds custom redirect logic to redirect after an item is deleted
|
|
|
|
|
* 1. Do not navigate if the deleted item is not selected
|
2024-04-05 12:26:17 +00:00
|
|
|
* 2. If it was the only item, navigate to the list url, to show the blank state
|
2024-02-02 05:06:02 +00:00
|
|
|
* 3. If there are other items, navigate to an item close to the current one
|
|
|
|
|
* **/
|
|
|
|
|
|
2024-03-19 09:47:49 +00:00
|
|
|
export enum RedirectAction {
|
2024-02-02 05:06:02 +00:00
|
|
|
NA = "NA", // No action is needed
|
2024-04-05 12:26:17 +00:00
|
|
|
LIST = "LIST", // Navigate to a creation URL
|
2024-02-02 05:06:02 +00:00
|
|
|
ITEM = "ITEM", // Navigate to this item
|
|
|
|
|
}
|
|
|
|
|
interface RedirectActionDescription {
|
|
|
|
|
action: RedirectAction;
|
|
|
|
|
payload?: EntityItem;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 06:49:24 +00:00
|
|
|
export function getNextEntityAfterRemove(
|
|
|
|
|
removedId: string,
|
|
|
|
|
tabs: EntityItem[],
|
2024-02-02 05:06:02 +00:00
|
|
|
): RedirectActionDescription {
|
|
|
|
|
const currentSelectedEntity = identifyEntityFromPath(
|
|
|
|
|
window.location.pathname,
|
|
|
|
|
);
|
2024-04-19 06:49:24 +00:00
|
|
|
const isSelectedActionRemoved = currentSelectedEntity.id === removedId;
|
2024-02-02 05:06:02 +00:00
|
|
|
|
2024-04-19 06:49:24 +00:00
|
|
|
// If removed item is not currently selected, don't redirect
|
|
|
|
|
if (!isSelectedActionRemoved) {
|
2024-02-02 05:06:02 +00:00
|
|
|
return {
|
|
|
|
|
action: RedirectAction.NA,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 06:49:24 +00:00
|
|
|
const indexOfTab = tabs.findIndex((item) => item.key === removedId);
|
|
|
|
|
switch (indexOfTab) {
|
|
|
|
|
case -1:
|
|
|
|
|
// If no other action is remaining, navigate to the creation url
|
|
|
|
|
return {
|
|
|
|
|
action: RedirectAction.LIST,
|
|
|
|
|
};
|
|
|
|
|
case 0:
|
|
|
|
|
// if the removed item is first item, then if tabs present, tabs + 1
|
|
|
|
|
// else otherItems[0] -> TODO: consider changing this logic after discussion with
|
|
|
|
|
// design team. May be new listing UI for side by side
|
|
|
|
|
if (tabs.length > 1) {
|
|
|
|
|
return {
|
|
|
|
|
action: RedirectAction.ITEM,
|
|
|
|
|
payload: tabs[1],
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
action: RedirectAction.LIST,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return {
|
|
|
|
|
action: RedirectAction.ITEM,
|
|
|
|
|
payload: tabs[indexOfTab - 1],
|
|
|
|
|
};
|
2024-02-02 05:06:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-02-27 05:12:27 +00:00
|
|
|
|
|
|
|
|
function* storeIDEViewChangeSaga(
|
|
|
|
|
action: ReduxAction<{ view: EditorViewMode }>,
|
|
|
|
|
) {
|
|
|
|
|
yield call(storeIDEViewMode, action.payload.view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* restoreIDEViewModeSaga() {
|
|
|
|
|
const storedState: EditorViewMode = yield call(retrieveIDEViewMode);
|
|
|
|
|
if (storedState) {
|
|
|
|
|
yield put(setIdeEditorViewMode(storedState));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function* root() {
|
|
|
|
|
yield all([
|
|
|
|
|
takeEvery(
|
|
|
|
|
ReduxActionTypes.SET_IDE_EDITOR_VIEW_MODE,
|
|
|
|
|
storeIDEViewChangeSaga,
|
|
|
|
|
),
|
|
|
|
|
takeEvery(
|
|
|
|
|
ReduxActionTypes.RESTORE_IDE_EDITOR_VIEW_MODE,
|
|
|
|
|
restoreIDEViewModeSaga,
|
|
|
|
|
),
|
|
|
|
|
]);
|
|
|
|
|
}
|