///
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Context, useContext, useState } from "react";
import { ActionsContext } from "@applicaster/zapp-react-native-ui-components/Contexts/ActionsContext";
import { reactHooksLogger } from "../logger";
const logger = reactHooksLogger.addSubsystem("actions");
type ActionPlugin = QuickBrickPlugin & { context: Context };
type ActionContext = Record>;
type ActionContexts = {
actions: Record>;
};
/**
* React Hook to subscribe to the custom action context
* @param {*} plugId - subscribes to the context of that action plugin
* @return {*} - global action provider context or context for specific action plugin
*/
export function useActions(plugId: string): any {
const [logged, setLogged] = useState(false);
const context = useContext>(ActionsContext);
const actionContext = context?.actions?.[plugId]?.module?.context;
if (plugId) {
if (!actionContext) {
if (!logged) {
logger.warning({
message: `useActions: Couldn't find an action for ${plugId} plugin`,
data: { actions: context?.actions },
});
setLogged(true);
}
return undefined;
}
// Disabling the rule as it's fine to have a conditional hook in this use case.
// eslint-disable-next-line react-hooks/rules-of-hooks
return useContext(actionContext);
}
return context;
}