PromucFlow_constructor/app/client/src/PluginActionEditor/PluginActionContext.tsx

63 lines
1.6 KiB
TypeScript
Raw Normal View History

chore: Plugin Action Editor Context (#36187) ## Description - Introduce the PluginActionEditor module structure - Add basic handling and states in the PluginActionContext - Update AppIDE to use the new Editor when the feature flag is active. This will later be updated and the component will be used from the route level itself Fixes #36152 ## Automation /ok-to-test tags="@tag.Datasource" ### :mag: 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/10805408539> > Commit: 1b8259b32c0df05137b01e25d7ff942b936e8110 > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=10805408539&attempt=1" target="_blank">Cypress dashboard</a>. > Tags: `@tag.Datasource` > Spec: > <hr>Wed, 11 Sep 2024 05:53:02 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a `PluginActionEditor` component for managing plugin actions. - Added `PluginActionContext` for improved state management of plugin actions. - Implemented feature flag checks to conditionally render the redesigned action editor interface in both the `ApiEditorWrapper` and `QueryEditor` components. - **Documentation** - Added centralized export files for easier access to new components and context providers. - **Bug Fixes** - Enhanced error handling for missing plugin settings and configurations in the `PluginActionEditor`. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-09-12 04:41:54 +00:00
import React, {
type ReactNode,
createContext,
useContext,
useMemo,
} from "react";
import type { Action } from "entities/Action";
import type { Plugin } from "api/PluginApi";
import type { Datasource } from "entities/Datasource";
interface PluginActionContextType {
action: Action;
editorConfig: unknown[];
settingsConfig: unknown[];
plugin: Plugin;
datasource?: Datasource;
}
// No need to export this context to use it. Use the hook defined below instead
const PluginActionContext = createContext<PluginActionContextType | null>(null);
interface ChildrenProps {
children: ReactNode[];
}
export const PluginActionContextProvider = (
props: ChildrenProps & PluginActionContextType,
) => {
const { action, children, datasource, editorConfig, plugin, settingsConfig } =
props;
// using useMemo to avoid unnecessary renders
const contextValue = useMemo(
() => ({
action,
datasource,
editorConfig,
plugin,
settingsConfig,
}),
[action, datasource, editorConfig, plugin, settingsConfig],
);
return (
<PluginActionContext.Provider value={contextValue}>
{children}
</PluginActionContext.Provider>
);
};
// By using this hook, you are guaranteed that the states are correctly
// typed and set.
// Without this, consumers of the context would need to keep doing a null check
export const usePluginActionContext = () => {
const context = useContext(PluginActionContext);
if (!context) {
throw new Error(
"usePluginActionContext must be used within usePluginActionContextProvider",
);
}
return context;
};