PromucFlow_constructor/app/client/src/reducers/uiReducers/debuggerReducer.ts
Hetu Nandu 7ff703c7de
chore: Migrate sub components into Plugin Action Editor (#36844)
## Description

- Moving older components, utils and constants into the Plugin Action
Editor to complete the modularisation project.
- Update Plugin Action Editor to expect an action ID instead of fetching
it from the URL.
- Add a Readme



Fixes #34324 

## Automation

/ok-to-test tags="@tag.All"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/11363303457>
> Commit: c36241f8cb19d57365c2555a02284b72aa849a67
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=11363303457&attempt=3"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.All`
> Spec:
> <hr>Wed, 16 Oct 2024 12:40:23 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
  - Introduced a new utility function `formatBytes` for byte formatting.
- Added comprehensive documentation for the Plugin Action Editor module.

- **Improvements**
  - Simplified action retrieval logic in the Plugin Action Editor.
- Enhanced import organization across various components for better
maintainability.

- **Bug Fixes**
- Resolved import path issues for several components and constants to
ensure correct functionality.

- **Chores**
- Updated multiple import paths to reflect the new directory structure
for improved modularity.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-10-16 18:29:10 +05:30

218 lines
6.0 KiB
TypeScript

import { createImmerReducer } from "utils/ReducerUtils";
import type { Log } from "entities/AppsmithConsole";
import type { ReduxAction } from "ee/constants/ReduxActionConstants";
import { ReduxActionTypes } from "ee/constants/ReduxActionConstants";
import { omit, isUndefined, isEmpty } from "lodash";
import equal from "fast-deep-equal";
import { ActionExecutionResizerHeight } from "PluginActionEditor/components/PluginActionResponse/constants";
import { klona } from "klona";
export const DefaultDebuggerContext = {
scrollPosition: 0,
selectedDebuggerTab: "",
responseTabHeight: ActionExecutionResizerHeight,
errorCount: 0,
selectedDebuggerFilter: "",
};
const initialState: DebuggerReduxState = {
logs: [],
isOpen: false,
errors: {},
expandId: "",
hideErrors: true,
context: DefaultDebuggerContext,
};
// check the last message from the current log and update the occurrence count
const removeRepeatedLogsAndMerge = (
currentLogs: Log[],
incomingLogs: Log[],
) => {
const outputArray = incomingLogs.reduce((acc: Log[], incomingLog: Log) => {
if (acc.length === 0) {
acc.push(incomingLog);
} else {
const lastLog = acc[acc.length - 1];
if (
equal(
omit(lastLog, ["occurrenceCount"]),
omit(incomingLog, ["occurrenceCount"]),
)
) {
lastLog.hasOwnProperty("occurrenceCount") && !!lastLog.occurrenceCount
? lastLog.occurrenceCount++
: (lastLog.occurrenceCount = 2);
} else {
acc.push(incomingLog);
}
}
return acc;
}, currentLogs);
return outputArray;
};
const debuggerReducer = createImmerReducer(initialState, {
[ReduxActionTypes.DEBUGGER_LOG]: (
state: DebuggerReduxState,
action: ReduxAction<Log[]>,
) => {
// state.logs = [...state.logs, ...action.payload];
state.logs = removeRepeatedLogsAndMerge(state.logs, action.payload);
},
[ReduxActionTypes.CLEAR_DEBUGGER_LOGS]: (state: DebuggerReduxState) => {
state.logs = [];
},
[ReduxActionTypes.SHOW_DEBUGGER]: (
state: DebuggerReduxState,
action: ReduxAction<boolean | undefined>,
) => {
state.isOpen = isUndefined(action.payload) ? !state.isOpen : action.payload;
},
[ReduxActionTypes.DEBUGGER_ADD_ERROR_LOGS]: (
state: DebuggerReduxState,
action: ReduxAction<Log[]>,
) => {
const { payload } = action;
// Remove Logs without IDs
const validDebuggerErrors = payload.reduce((validLogs, currentLog) => {
if (!currentLog.id) return validLogs;
return {
...validLogs,
[currentLog.id]: currentLog,
};
}, {});
if (isEmpty(validDebuggerErrors)) return state;
// Moving recent update to the top of the error list
const errors = omit(state.errors, Object.keys(validDebuggerErrors));
state.errors = {
...validDebuggerErrors,
...errors,
};
},
[ReduxActionTypes.DEBUGGER_DELETE_ERROR_LOG]: (
state: DebuggerReduxState,
action: ReduxAction<string[]>,
) => {
state.errors = omit(state.errors, action.payload);
},
[ReduxActionTypes.HIDE_DEBUGGER_ERRORS]: (
state: DebuggerReduxState,
action: ReduxAction<boolean>,
) => {
state.hideErrors = action.payload;
},
// Resetting debugger state after page switch
[ReduxActionTypes.SWITCH_CURRENT_PAGE_ID]: () => {
return {
...initialState,
};
},
[ReduxActionTypes.SET_DEBUGGER_SELECTED_TAB]: (
state: DebuggerReduxState,
action: { selectedTab: string },
) => {
state.context.selectedDebuggerTab = action.selectedTab;
},
[ReduxActionTypes.SET_DEBUGGER_SELECTED_FILTER]: (
state: DebuggerReduxState,
action: { selectedFilter: string },
) => {
state.context.selectedDebuggerFilter = action.selectedFilter;
},
[ReduxActionTypes.SET_RESPONSE_PANE_HEIGHT]: (
state: DebuggerReduxState,
action: { height: number },
) => {
state.context.responseTabHeight = action.height;
},
[ReduxActionTypes.SET_ERROR_COUNT]: (
state: DebuggerReduxState,
action: { count: number },
) => {
state.context.errorCount = action.count;
},
[ReduxActionTypes.SET_RESPONSE_PANE_SCROLL_POSITION]: (
state: DebuggerReduxState,
action: { position: number },
) => {
state.context.scrollPosition = action.position;
},
[ReduxActionTypes.TOGGLE_EXPAND_ERROR_LOG_ITEM]: (
state: DebuggerReduxState,
action: ReduxAction<{ id: string; isExpanded: boolean }>,
) => {
const { id, isExpanded } = action.payload;
const errors = JSON.parse(JSON.stringify(state.errors));
errors[id] = { ...errors[id], isExpanded };
return {
...state,
errors,
};
},
[ReduxActionTypes.SET_DEBUGGER_CONTEXT]: (
state: DebuggerReduxState,
action: { context: DebuggerContext },
) => {
state.context = action.context;
},
[ReduxActionTypes.SET_CANVAS_DEBUGGER_STATE]: (
state: DebuggerReduxState,
action: { payload: Partial<CanvasDebuggerState> },
): DebuggerReduxState => {
return {
...state,
isOpen: "open" in action.payload ? !!action.payload.open : state.isOpen,
context: {
...state.context,
responseTabHeight:
"responseTabHeight" in action.payload
? Number(action.payload.responseTabHeight)
: state.context.responseTabHeight,
selectedDebuggerTab:
"selectedTab" in action.payload
? String(action.payload.selectedTab)
: state.context.selectedDebuggerTab,
},
};
},
// Resetting debugger state after env switch
[ReduxActionTypes.SWITCH_ENVIRONMENT_SUCCESS]: () => {
return klona(initialState);
},
});
export interface DebuggerReduxState {
logs: Log[];
isOpen: boolean;
errors: Record<string, Log>;
expandId: string;
hideErrors: boolean;
context: DebuggerContext;
}
export interface DebuggerContext {
scrollPosition: number;
errorCount: number;
selectedDebuggerTab: string;
responseTabHeight: number;
selectedDebuggerFilter: string;
}
export interface CanvasDebuggerState {
open: boolean;
responseTabHeight: number;
selectedTab?: string;
}
export default debuggerReducer;