Skip to content

Commit

Permalink
Add indirect relation unknown relations support (#758)
Browse files Browse the repository at this point in the history
* Add indirect relation unknown relations support

* Fix typo, optimize filter

* indirectRelation -> IsIndirectRelation

* indirectAliases popup fix

* Add ConnectionIndirectAliasesProp to ConnectionField type

* fix for byFields after merge

* Default connection type field case - empty fields
  • Loading branch information
flops authored Mar 13, 2024
1 parent 00d52a9 commit 549aa0a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ type RowParams = {
type ConnectionByFieldsProp = string[] | string;
type ConnectionByUsedParamsProp = string[] | string;
type ConnectionByAliasesProp = string[][] | string;
type ConnectionIndirectAliasesProp = string[][];

type ConnectionField =
| ConnectionByFieldsProp
| ConnectionByUsedParamsProp
| ConnectionByAliasesProp;
type ConnectionType = 'alias' | 'field' | 'param';
| ConnectionByAliasesProp
| ConnectionIndirectAliasesProp;
type ConnectionType = 'alias' | 'field' | 'param' | 'indirect';

const getRelationDetailsText = ({
text,
Expand Down Expand Up @@ -120,8 +122,16 @@ const getFieldText = ({
let fieldText = '';
let fieldTextWithStrong = null;

if (type === 'indirect') {
return {
fieldText,
fieldTextWithStrong,
};
}

const byField = type === 'field' || type === 'param';
const {singleLabel, multiLabel} = labelsMap[type];

if (Array.isArray(field) && field.length) {
const fieldLabel = field.length === 1 ? singleLabel : multiLabel;
const fieldName = byField ? (Array.isArray(field) && field?.join(', ')) || '' : '';
Expand Down Expand Up @@ -151,17 +161,21 @@ const getConnectionByInfo = ({
byUsedParams,
byAliases,
relationType,
indirectAliases,
hasDataset,
}: {
relationType: RelationType;
byFields: ConnectionByFieldsProp;
byUsedParams: ConnectionByUsedParamsProp;
byAliases: ConnectionByAliasesProp;
indirectAliases: ConnectionIndirectAliasesProp;
hasDataset: boolean;
}) => {
const isUnknownRelation = relationType === RELATION_TYPES.unknown;
const availableLink =
relationType !== RELATION_TYPES.ignore && relationType !== RELATION_TYPES.unknown;

const hasIndirectAliases = Boolean(indirectAliases.length);
const hasUsedParams = Array.isArray(byUsedParams)
? Boolean(byUsedParams.length)
: Boolean(byUsedParams);
Expand All @@ -171,18 +185,38 @@ const getConnectionByInfo = ({
const hasAliases = Array.isArray(byAliases) ? Boolean(byAliases.length) : Boolean(byAliases);
const showByAlias = hasAliases && availableLink;

let field: ConnectionField = [];
let type: ConnectionType = 'field';

if (showByAlias) {
field = byAliases;
type = 'alias';
} else if (showByField || (hasDataset && availableLink)) {
field = byFields;
type = 'field';
} else if (showByUsedParams) {
field = byUsedParams;
type = 'param';
let field: ConnectionField;
let type: ConnectionType;

switch (true) {
case showByAlias: {
field = byAliases;
type = 'alias';
break;
}

case showByField || (hasDataset && availableLink): {
field = byFields;
type = 'field';
break;
}

case showByUsedParams: {
field = byUsedParams;
type = 'param';
break;
}

case isUnknownRelation && hasIndirectAliases: {
field = indirectAliases;
type = 'indirect';
break;
}

default: {
field = [];
type = 'field';
}
}

const {fieldText, fieldTextWithStrong} = getFieldText({
Expand All @@ -205,6 +239,7 @@ export const getTooltipInfo = ({
byFields,
byAliases,
byUsedParams,
indirectAliases,
hasDataset,
}: {
widget: DashkitMetaDataItem;
Expand All @@ -213,13 +248,15 @@ export const getTooltipInfo = ({
byFields: ConnectionByFieldsProp;
byAliases: ConnectionByAliasesProp;
byUsedParams: ConnectionByUsedParamsProp;
indirectAliases: ConnectionIndirectAliasesProp;
hasDataset: boolean;
}) => {
const {fieldText, fieldTextWithStrong, showByField} = getConnectionByInfo({
byFields,
byAliases,
relationType,
byUsedParams,
indirectAliases,
hasDataset,
});

Expand Down Expand Up @@ -325,6 +362,7 @@ export const Row = ({
const {
type: relationType,
available: availableRelations,
indirectAliases,
byFields,
byAliases,
byUsedParams,
Expand All @@ -339,6 +377,7 @@ export const Row = ({
byFields,
byAliases,
byUsedParams,
indirectAliases,
hasDataset,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ const getItemsRelations = ({
return {
byUsedParams,
byAliases: relations.byAliases,
indirectAliases: relations.indirectAliases,
isIgnoring: relations.isIgnoring,
isIgnored: relations.isIgnored,
type: relationType as RelationType,
Expand Down Expand Up @@ -396,32 +397,45 @@ export const getRelationsInfo = (args: {
const {aliases, connections, datasets, widget, row} = args;
const byUsedParams = getByUsedParams({widget, row});
let byAliases: Array<Array<string>> = [];
const indirectAliases: Array<Array<string>> = [];

const ignoreConntections = connections.filter(({kind}) => kind === CONNECTION_KIND.IGNORE);

const isIgnored = ignoreConntections.some(
({from, to}) => from === widget.widgetId && to === row.widgetId,
);

const isIgnoring = ignoreConntections.some(
({from, to}) => from === row.widgetId && to === widget.widgetId,
);

const isIndirectRelation = !isIgnored && !isIgnoring;

if (aliases[DEFAULT_ALIAS_NAMESPACE]?.length) {
byAliases = aliases[DEFAULT_ALIAS_NAMESPACE].filter((aliasArr) => {
if (!row.usedParams?.length) {
if (!row.usedParams?.length && !isIndirectRelation) {
return false;
}
const rowInAlias = intersection(row.usedParams, aliasArr);
const widgetInAlias = intersection(widget.usedParams, aliasArr);

if (rowInAlias.length || widgetInAlias.length) {
indirectAliases.push(aliasArr);
}

if (!rowInAlias.length || !widgetInAlias.length) {
return false;
}

return rowInAlias;
});
}

const isIgnored = connections
.filter(({kind}) => kind === CONNECTION_KIND.IGNORE)
.some(({from, to}) => from === widget.widgetId && to === row.widgetId);

const isIgnoring = connections
.filter(({kind}) => kind === CONNECTION_KIND.IGNORE)
.some(({from, to}) => from === row.widgetId && to === widget.widgetId);

return getItemsRelations({
relations: {
byUsedParams,
byAliases,
indirectAliases,
isIgnoring,
isIgnored,
hasDataset: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const getControlToControlRelations = ({
let hasDataset = false;
let forceAddAlias = false;
let hasRelation = false;
const isIndirectRelation = !relations.isIgnored && !relations.isIgnoring;

if (isManualControl(widget) && isManualControl(row)) {
if (relations.byAliases.length || byUsedParams.length) {
Expand All @@ -80,21 +81,24 @@ export const getControlToControlRelations = ({
(isDatasetControl(widget) && isManualControl(row)) ||
(isDatasetControl(row) && isManualControl(widget))
) {
hasDataset = true;
byFields =
getMappedConnectedControlField({
item: isDatasetControl(widget) ? widget : row,
itemDefaults: isDatasetControl(widget) ? row.defaultParams : widget.defaultParams,
datasets,
}) || byUsedParams;

if (relations.byAliases.length || byUsedParams.length) {
newRelationType = relationType || getDefaultTypeByIgnore(relations);
} else if (isIndirectRelation && relations.indirectAliases.length) {
newRelationType = RELATION_TYPES.unknown;
forceAddAlias = true;
} else {
newRelationType = RELATION_TYPES.ignore;
forceAddAlias = true;
}
availableRelations = [...FULL_RELATIONS];

hasDataset = true;
const fields =
getMappedConnectedControlField({
item: isDatasetControl(widget) ? widget : row,
datasets,
}) || [];
byFields = fields || byUsedParams;
} else if (
(isManualControl(widget) && isExternalControl(row)) ||
(isManualControl(row) && isExternalControl(widget))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ const getDatasetsListWithFlatFields = (datasets: Datasets) => {
export const getMappedConnectedControlField = ({
item,
datasets,
itemDefaults,
}: {
item: DashkitMetaDataItemNoRelations;
datasets: Datasets;
itemDefaults?: DashkitMetaDataItemNoRelations['defaultParams'];
}) => {
if (!isControl(item)) {
return null;
Expand All @@ -77,7 +79,7 @@ export const getMappedConnectedControlField = ({
// map fields with the name of the field from the dataset
if (item?.datasets?.length && !item.isQL) {
// if it is dataset selector
return Object.keys(item.defaultParams)
return Object.keys(itemDefaults || item.defaultParams)
.map((paramItem) => {
const allFields = getDatasetsFlatItems(item.datasets);
return allFields[paramItem] || '';
Expand All @@ -87,7 +89,7 @@ export const getMappedConnectedControlField = ({
// if it is dataset selector (other format)
const datasetFlatFields = getDatasetsListWithFlatFields(datasets);
if (datasetFlatFields && item?.datasetId && datasetFlatFields[item?.datasetId]) {
return Object.keys(item.defaultParams)
return Object.keys(itemDefaults || item.defaultParams)
.map(
(paramItem) =>
datasetFlatFields[item.datasetId!] &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type RelationType = keyof typeof RELATION_TYPES;
export type RelationsData = {
byUsedParams: Array<string>;
byAliases: Array<Array<string>>;
indirectAliases: Array<Array<string>>;
isIgnoring: boolean;
isIgnored: boolean;
type: RelationType;
Expand Down

0 comments on commit 549aa0a

Please sign in to comment.