Copy the Chatwoot (v4.14.0) frontend runnable subset into frontend/ for customization: - app/javascript/ (Vue SPA: dashboard, widget, sdk, portal, superadmin) - app/views/ (ERB templates for vite-plugin-ruby entrypoint resolution) - app/helpers/, app/assets/ (Rails view helpers, static assets) - enterprise/ (Enterprise edition frontend overlay) - config/vite.json, vite.config.ts, bin/vite (Vite-Rails toolchain) - package.json, pnpm-lock.yaml, tailwind/postcss/eslint configs - Gemfile, Gemfile.lock (vite_rails gem for bin/vite binstub) Excluded Rails backend: controllers, models, services, jobs, mailers, policies, db, lib, spec, public, node_modules. Update references to the new frontend location: - .gitignore: exclude frontend build artifacts (node_modules, tmp, packs), keep frontend/bin/ and frontend/vendor/ via negation - backend/scripts/parity_frontend_smoke.sh: CHATWOOT_DIR default reference/chatwoot -> ../frontend - backend/scripts/parity_frontend_browser_smoke.mjs: same default update - AGENTS.md: add frontend section with Rails/Vite coupling notes - README: architecture tree includes frontend/
145 lines
5.0 KiB
JavaScript
145 lines
5.0 KiB
JavaScript
import useAutomationValues from './useAutomationValues';
|
|
|
|
import {
|
|
getCustomAttributeInputType,
|
|
filterCustomAttributes,
|
|
getStandardAttributeInputType,
|
|
isCustomAttribute,
|
|
} from 'dashboard/helper/automationHelper';
|
|
|
|
export function useEditableAutomation() {
|
|
const { getConditionDropdownValues, getActionDropdownValues } =
|
|
useAutomationValues();
|
|
|
|
/**
|
|
* This function sets the conditions for automation.
|
|
* It help to format the conditions for the automation when we open the edit automation modal.
|
|
* @param {Object} automation - The automation object containing conditions to manifest.
|
|
* @param {Array} allCustomAttributes - List of all custom attributes.
|
|
* @param {Object} automationTypes - Object containing automation type definitions.
|
|
* @returns {Array} An array of manifested conditions.
|
|
*/
|
|
const manifestConditions = (
|
|
automation,
|
|
allCustomAttributes,
|
|
automationTypes
|
|
) => {
|
|
const customAttributes = filterCustomAttributes(allCustomAttributes);
|
|
return automation.conditions.map(condition => {
|
|
const customAttr = isCustomAttribute(
|
|
customAttributes,
|
|
condition.attribute_key
|
|
);
|
|
let inputType = 'plain_text';
|
|
if (customAttr) {
|
|
inputType = getCustomAttributeInputType(customAttr.type);
|
|
} else {
|
|
inputType = getStandardAttributeInputType(
|
|
automationTypes,
|
|
automation.event_name,
|
|
condition.attribute_key
|
|
);
|
|
}
|
|
if (inputType === 'plain_text' || inputType === 'date') {
|
|
return { ...condition, values: condition.values[0] };
|
|
}
|
|
if (inputType === 'comma_separated_plain_text') {
|
|
return { ...condition, values: condition.values.join(',') };
|
|
}
|
|
const dropdownValues = getConditionDropdownValues(
|
|
condition.attribute_key
|
|
);
|
|
const hasBooleanOptions =
|
|
inputType === 'search_select' &&
|
|
dropdownValues.length &&
|
|
dropdownValues.every(item => typeof item.id === 'boolean');
|
|
|
|
if (hasBooleanOptions) {
|
|
return {
|
|
...condition,
|
|
query_operator: condition.query_operator || 'and',
|
|
values: dropdownValues.find(item => item.id === condition.values[0]),
|
|
};
|
|
}
|
|
return {
|
|
...condition,
|
|
query_operator: condition.query_operator || 'and',
|
|
values: [...dropdownValues].filter(item =>
|
|
[...condition.values].includes(item.id)
|
|
),
|
|
};
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Generates an array of actions for the automation.
|
|
* @param {Object} action - The action object.
|
|
* @param {Array} automationActionTypes - List of available automation action types.
|
|
* @returns {Array|Object} Generated actions array or object based on input type.
|
|
*/
|
|
const generateActionsArray = (action, automationActionTypes) => {
|
|
const params = action.action_params;
|
|
const inputType = automationActionTypes.find(
|
|
item => item.key === action.action_name
|
|
).inputType;
|
|
if (inputType === 'multi_select' || inputType === 'search_select') {
|
|
return [...getActionDropdownValues(action.action_name)].filter(item =>
|
|
[...params].includes(item.id)
|
|
);
|
|
}
|
|
if (inputType === 'team_message') {
|
|
return {
|
|
team_ids: [...getActionDropdownValues(action.action_name)].filter(
|
|
item => [...params[0].team_ids].includes(item.id)
|
|
),
|
|
message: params[0].message,
|
|
};
|
|
}
|
|
return [...params];
|
|
};
|
|
|
|
/**
|
|
* This function sets the actions for automation.
|
|
* It help to format the actions for the automation when we open the edit automation modal.
|
|
* @param {Object} automation - The automation object containing actions.
|
|
* @param {Array} automationActionTypes - List of available automation action types.
|
|
* @returns {Array} An array of manifested actions.
|
|
*/
|
|
const manifestActions = (automation, automationActionTypes) => {
|
|
return automation.actions.map(action => ({
|
|
...action,
|
|
action_params: action.action_params.length
|
|
? generateActionsArray(action, automationActionTypes)
|
|
: [],
|
|
}));
|
|
};
|
|
|
|
/**
|
|
* Formats the automation object for use when we edit the automation.
|
|
* It help to format the conditions and actions for the automation when we open the edit automation modal.
|
|
* @param {Object} automation - The automation object to format.
|
|
* @param {Array} allCustomAttributes - List of all custom attributes.
|
|
* @param {Object} automationTypes - Object containing automation type definitions.
|
|
* @param {Array} automationActionTypes - List of available automation action types.
|
|
* @returns {Object} A new object with formatted automation data, including automation conditions and actions.
|
|
*/
|
|
const formatAutomation = (
|
|
automation,
|
|
allCustomAttributes,
|
|
automationTypes,
|
|
automationActionTypes
|
|
) => {
|
|
return {
|
|
...automation,
|
|
conditions: manifestConditions(
|
|
automation,
|
|
allCustomAttributes,
|
|
automationTypes
|
|
),
|
|
actions: manifestActions(automation, automationActionTypes),
|
|
};
|
|
};
|
|
|
|
return { formatAutomation };
|
|
}
|