Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,87 @@ const HistogramParameters = ({ widgetType, control, setValue }: Props) => {
// -- HANDLE FIELDS --
const [fieldOptions, setFieldOptions] = useState<GroupOption[]>([]);

// Watch the current field value to validate it when options change
const currentField = useWatch({
control,
name: mode === 'temporal' ? 'widget_config.date_attribute' : 'widget_config.field',
});

useEffect(() => {
engineSchemas(entities).then((response: { data: PropertySchemaDTO[] }) => {
const finalOptions = getEntityPropertiesListOptions(
response.data,
widgetType,
d => mode === 'temporal' ? d.schema_property_type === 'instant' : d.schema_property_type !== 'instant')
.map((o) => {
return {
...o,
label: t(o.label),
};
// If in structural mode with multiple entities, only show common fields
if (mode === 'structural' && entities.length > 1) {
// Fetch schemas for each entity separately
Promise.all(entities.map(entity => engineSchemas([entity])))
.then((responses: { data: PropertySchemaDTO[] }[]) => {
// Get field options for each entity
const optionsByEntity = responses.map(response =>
getEntityPropertiesListOptions(
response.data,
widgetType,
d => d.schema_property_type !== 'instant',
),
);

// Create Sets upfront for better performance
const entityFieldSets = optionsByEntity.map(options =>
new Set(options.map(o => o.id)),
);

// Find intersection of fields (fields that exist in ALL entities)
const commonFieldIdsSet = optionsByEntity.length > 0
? new Set(
optionsByEntity[0]
.map(o => o.id)
.filter(id => entityFieldSets.every(fieldSet => fieldSet.has(id))),
)
: new Set<string>();

// Filter to only include common fields
const finalOptions = optionsByEntity.length > 0
? optionsByEntity[0]
.filter(o => commonFieldIdsSet.has(o.id))
.map((o) => {
return {
...o,
label: t(o.label),
};
})
: [];

setFieldOptions(finalOptions);

// Clear field value if it's no longer in the available options
if (currentField && !finalOptions.some(o => o.id === currentField)) {
setValue('widget_config.field', '');
} else if (finalOptions.length === 1) {
setValue('widget_config.field', finalOptions[0].id);
}
});
setFieldOptions(finalOptions);
if (finalOptions.length === 1) {
setValue('widget_config.field', finalOptions[0].id); // If only one option is available, hide the field and set it automatically
}
});
}, [mode]);
} else {
// Single entity or temporal mode - show all fields
engineSchemas(entities).then((response: { data: PropertySchemaDTO[] }) => {
const finalOptions = getEntityPropertiesListOptions(
response.data,
widgetType,
d => mode === 'temporal' ? d.schema_property_type === 'instant' : d.schema_property_type !== 'instant')
.map((o) => {
return {
...o,
label: t(o.label),
};
});
setFieldOptions(finalOptions);

// Clear field value if it's no longer in the available options
const fieldName = mode === 'temporal' ? 'widget_config.date_attribute' : 'widget_config.field';
if (currentField && !finalOptions.some(o => o.id === currentField)) {
setValue(fieldName, '');
} else if (finalOptions.length === 1) {
setValue(fieldName, finalOptions[0].id); // If only one option is available, hide the field and set it automatically
}
});
}
}, [mode, entities.length, currentField, widgetType, setValue, t]);

return (
<>
Expand Down