Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 22x 22x 22x 22x 22x 22x 108x 108x 108x 108x 108x 27x 108x 108x 108x 17x 108x 108x 108x 27x 108x 108x 108x 108x 108x 27x 27x 27x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 4x 4x 4x 4x 4x 4x 4x 8x 8x 8x 8x 8x 8x 8x 8x 6x 6x 6x 6x 6x 6x 6x 8x 4x 4x 4x 4x 108x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 2x 108x 15x 15x 15x 15x 15x 15x 39x 39x 39x 39x 39x 39x 39x 39x 29x 29x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 25x 25x 25x 25x 25x 25x 25x 25x 25x 39x 14x 39x 39x 39x 27x 27x 27x 27x 4x 4x 4x 27x 27x 27x 27x 27x 27x 15x 15x 15x 108x 108x 108x 108x 108x 108x 108x 108x 133x 133x 133x 108x | import {
FormData,
TextFieldConfig,
AccordionLayoutConfig,
CheckboxFieldConfig
} from '../types';
import { Node, Category, Exit, Case } from '../../store/flow-definition';
import { getOperatorConfig } from '../operators';
import { generateUUID } from '../../utils';
import { categoryNamesEqual, findCategoryByName } from '../categoryUtils';
/**
* Shared result_name field configuration for router nodes.
* This provides a consistent "Save as..." optional field interface across all splits.
*
* The field is hidden by default and revealed via a "Save as..." link.
* Once revealed, it cannot be hidden again (the link disappears).
* If the field already has a value, it's shown immediately without the link.
*/
export const resultNameField: TextFieldConfig = {
type: 'text',
required: false,
maxLength: 64,
placeholder: '(optional)',
helpText: 'The name to use to reference this result in the flow'
};
/**
* Shared localization requirement fields for router nodes.
* These provide checkboxes for "Require rules to be localized" and
* "Require categories to be localized" in a collapsible accordion.
*/
export const localizeRulesField: CheckboxFieldConfig = {
type: 'checkbox',
label: 'Require rules to be localized',
helpText: 'Each language must specify its own rules for this node'
};
export const localizeCategoriesField: CheckboxFieldConfig = {
type: 'checkbox',
label: 'Require categories to be localized',
helpText: (formData: FormData) => {
const name = formData.result_name?.trim();
if (name) {
const key = name.toLowerCase().replace(/\s+/g, '_');
return `Only enable if you plan to use @results.${key}.category_localized`;
}
return 'Only enable if you plan to use category_localized in your expressions for this result';
},
conditions: {
visible: (formData: FormData) => !!formData.result_name
}
};
const resultNameSection = {
label: 'Save Result',
localizable: false,
items: ['result_name'],
collapsed: (formData: FormData) => !(formData._isNew && formData.result_name),
getValueCount: (formData: FormData) => !!formData.result_name
};
const advancedSection = {
label: 'Localization',
localizable: false,
items: ['localizeRules', 'localizeCategories'],
collapsed: true,
getValueCount: (formData: FormData) =>
!!(formData.localizeRules || formData.localizeCategories)
};
export const nodeOptionsAccordion: AccordionLayoutConfig = {
type: 'accordion',
multi: true,
sections: [resultNameSection, advancedSection]
};
export const nodeOptionsAccordionSimple: AccordionLayoutConfig = {
type: 'accordion',
multi: true,
sections: [resultNameSection]
};
/**
* Shared category localization functions for router nodes.
* These provide a consistent way to localize category names across all router types.
*/
/**
* Converts a node's categories to localization form data.
* @param node - The node containing categories to localize
* @param localization - The existing localization data for this language
* @returns Form data with category UUIDs mapped to original and localized names
*/
export function categoriesToLocalizationFormData(
node: Node,
localization: Record<string, any>
): FormData {
const categories = node.router?.categories || [];
const localizationData: Record<string, any> = {};
categories.forEach((category: any) => {
const categoryUuid = category.uuid;
const categoryLocalization = localization[categoryUuid];
localizationData[categoryUuid] = {
originalName: category.name,
localizedName:
categoryLocalization && categoryLocalization.name
? Array.isArray(categoryLocalization.name)
? categoryLocalization.name[0] || ''
: categoryLocalization.name
: ''
};
});
// Also include rule (case) argument localizations
const cases = node.router?.cases || [];
const rulesData: Record<string, any> = {};
cases.forEach((c: any) => {
if (!c.arguments?.length || !c.arguments.some((a: string) => a)) return;
const caseLocalization = localization[c.uuid];
const operatorName = getOperatorConfig(c.type)?.name || c.type;
rulesData[c.uuid] = {
operatorName,
originalArguments: [...c.arguments],
localizedArguments: caseLocalization?.arguments
? [...caseLocalization.arguments]
: c.arguments.map(() => '')
};
});
return {
categories: localizationData,
rules: rulesData
};
}
/**
* Converts localization form data back to the localization structure.
* @param formData - The form data containing category localizations
* @param _node - The original node (reserved for future validation)
* @returns Record mapping category UUIDs to their localization data
*/
export function localizationFormDataToCategories(
formData: FormData,
_node: Node
): Record<string, any> {
const localizationData: Record<string, any> = {};
if (formData.categories) {
Object.keys(formData.categories).forEach((categoryUuid) => {
const categoryData = formData.categories[categoryUuid];
const localizedName = categoryData.localizedName?.trim() || '';
const originalName = categoryData.originalName?.trim() || '';
// Only save if localized name is different from original and not empty
if (localizedName && localizedName !== originalName) {
localizationData[categoryUuid] = {
name: [localizedName]
};
}
});
}
// Also process rule localizations
if (formData.rules) {
Object.keys(formData.rules).forEach((caseUuid) => {
const ruleData = formData.rules[caseUuid];
const localized = ruleData.localizedArguments || [];
const original = ruleData.originalArguments || [];
// Save if any argument differs from original and is non-empty
const hasLocalization = localized.some(
(arg: string, i: number) =>
arg?.trim() && arg.trim() !== (original[i] || '')
);
if (hasLocalization) {
localizationData[caseUuid] = {
arguments: localized.map((a: string) => a?.trim() || '')
};
}
});
}
return localizationData;
}
/**
* Describes a category to build for a router node. When `case` is provided,
* a matching switch-router case is also built and linked to the category.
*/
export interface CategoryEntry {
name: string;
case?: {
type: string;
arguments: string[];
};
}
/**
* Builds categories, exits, and (optionally) cases for a router node,
* preserving UUIDs and exit destinations from existing data when possible.
* Categories/exits are matched by category name; cases by their first argument.
*/
export function buildCategoriesExitsCases(
entries: CategoryEntry[],
existingCategories: Category[],
existingExits: Exit[],
existingCases: Case[] = []
): { categories: Category[]; exits: Exit[]; cases: Case[] } {
const categories: Category[] = [];
const exits: Exit[] = [];
const cases: Case[] = [];
entries.forEach((entry) => {
const existingCategory = findCategoryByName(existingCategories, entry.name);
const existingExit = existingCategory
? existingExits.find((exit) => exit.uuid === existingCategory.exit_uuid)
: null;
const exitUuid = existingExit?.uuid || generateUUID();
const categoryUuid = existingCategory?.uuid || generateUUID();
categories.push({
uuid: categoryUuid,
name: entry.name,
exit_uuid: exitUuid
});
exits.push({
uuid: exitUuid,
destination_uuid: existingExit?.destination_uuid || null
});
if (entry.case) {
const matchArg = entry.case.arguments[0];
const existingCase = existingCases.find(
(c) => c.arguments?.[0] === matchArg
);
cases.push({
uuid: existingCase?.uuid || generateUUID(),
type: entry.case.type,
arguments: entry.case.arguments,
category_uuid: categoryUuid
});
}
});
return { categories, exits, cases };
}
/**
* Appends a default "Other" category and its exit to the given arrays,
* preserving the UUID/destination of an existing "Other" unless the user
* selected an item also named "Other" (in which case the existing one was
* already consumed by buildCategoriesExitsCases). Returns the Other category
* UUID for use as `default_category_uuid`.
*/
export function appendOtherCategory(
categories: Category[],
exits: Exit[],
existingCategories: Category[],
existingExits: Exit[],
userItemNames: string[]
): string {
const userHasOther = userItemNames.some((name) =>
categoryNamesEqual(name, 'Other')
);
const existingOther = userHasOther
? null
: findCategoryByName(existingCategories, 'Other');
const existingOtherExit = existingOther
? existingExits.find((exit) => exit.uuid === existingOther.exit_uuid)
: null;
const otherExitUuid = existingOtherExit?.uuid || generateUUID();
const otherCategoryUuid = existingOther?.uuid || generateUUID();
categories.push({
uuid: otherCategoryUuid,
name: 'Other',
exit_uuid: otherExitUuid
});
exits.push({
uuid: otherExitUuid,
destination_uuid: existingOtherExit?.destination_uuid || null
});
return otherCategoryUuid;
}
|