Skip to content

Commit 25435c0

Browse files
chore: Adjust jsonata to latest version (kyma-project#4089)
* Initail changes to async/await * More adjustment added * Extensibility injections jasonata usage update * Resolved conflicts * Another part of adjusted components * Adaptation to the new version * Setting title for table in state * Handle async jsonata in sort function * Handle async evaluate function * Corrections with search functions * Resolve promises in search properties array * Copyable widget test corrected * Add hook to get async jsonata value * Promisses corrections * Handle async function in graph configuration * Corrections * Corrections * Handle async visibility and disableEdit * Handle async copy function * Correct async filter * Handle async filter for ResourceRenderer * Correction * Correct sort * Last corrections and clean ups * Corrections in useEffect array * Fix too much rerenders * PR correction * PR correction * Too many rerenders correction * Remove console.log * PR correction * Test correction added * Test correction * Test correction * Test correction * Test correction * PR correction * Fix lint * Test correction * Test correction * Correct visibility handler * Correct visibility * Tests correction * Test correction * Test correction * Test correction * Test correction * Test correction * PR review correction * Test correction
1 parent a1c396e commit 25435c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1008
-513
lines changed

package-lock.json

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"js-tiktoken": "^1.0.20",
9393
"js-yaml": "^4.1.0",
9494
"jsdom": "^26.1.0",
95-
"jsonata": "^1.8.7",
95+
"jsonata": "^2.1.0",
9696
"jsonpath": "^1.1.1",
9797
"jsonschema": "^1.5.0",
9898
"jwt-decode": "^4.0.0",

src/components/Extensibility/ExtensibilityDetails.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ export const ExtensibilityDetailsCore = ({
8383
const dataSources = resMetaData?.dataSources || {};
8484
const general = resMetaData?.general || {};
8585

86-
const prepareVisibility = (def, resource) => {
86+
const prepareVisibility = async (def, resource) => {
8787
setResourcesConditions(resource.status);
88-
const [visible, error] = jsonata(def.visibility, { resource }, true);
88+
const [visible, error] = await jsonata(def.visibility, { resource }, true);
8989
return { visible, error };
9090
};
9191

92-
const prepareDisableEdit = resource => {
92+
const prepareDisableEdit = async resource => {
9393
if (disableEdit && typeof disableEdit === 'string') {
94-
const [isDisabled] = jsonata(disableEdit, { resource });
94+
const [isDisabled] = await jsonata(disableEdit, { resource });
9595
return typeof isDisabled === 'boolean' ? isDisabled : false;
9696
}
9797
return disableEdit;

src/components/Extensibility/ExtensibilityInjections.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect, useState } from 'react';
12
import { ExtensibilityErrBoundary } from 'components/Extensibility/ExtensibilityErrBoundary';
23
import { useGetSchema } from 'hooks/useGetSchema';
34

@@ -32,27 +33,39 @@ export const ExtensibilityInjectionCore = ({ resMetaData, root }) => {
3233
skip: !resourceUrl,
3334
});
3435

36+
const [filteredItems, setFilteredItems] = useState([{}]);
3537
const jsonata = useJsonata({});
3638

37-
// there may be a moment when `resMetaData` is undefined (e.g. when switching the namespace)
38-
if (!resource && !isStatic) {
39-
return null;
40-
}
41-
4239
const dataSources = resMetaData?.dataSources || {};
4340
const general = resMetaData?.general || {};
4441
const injection = resMetaData?.injection;
4542
const injectionName = injection?.name;
4643
const filter = injection?.target.filter || injection?.filter || null;
47-
4844
const items = data?.items || [];
49-
const filteredItems = items.filter(item => {
50-
if (filter) {
51-
const [value] = jsonata(filter, { item, root });
52-
return value;
45+
46+
useEffect(() => {
47+
if (!resource && !isStatic) {
48+
return;
5349
}
54-
return true;
55-
});
50+
Promise.all(
51+
items.map(async item => {
52+
if (filter) {
53+
const [value] = await jsonata(filter, { item, root });
54+
return value ? item : false;
55+
}
56+
return item;
57+
}),
58+
).then(results => {
59+
setFilteredItems(results.filter(Boolean));
60+
});
61+
// eslint-disable-next-line react-hooks/exhaustive-deps
62+
}, [resource, isStatic, filter, JSON.stringify(items)]);
63+
64+
// there may be a moment when `resMetaData` is undefined (e.g. when switching the namespace)
65+
if (!resource && !isStatic) {
66+
return null;
67+
}
68+
5669
return (
5770
<Widget
5871
key={injectionName}

src/components/Extensibility/ExtensibilityList.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { useEffect } from 'react';
12
import pluralize from 'pluralize';
23
import { useTranslation } from 'react-i18next';
3-
import { useEffect } from 'react';
44

55
import { ResourcesList } from 'shared/components/ResourcesList/ResourcesList';
66
import { usePrepareListProps } from 'resources/helpers';
@@ -97,8 +97,8 @@ export const ExtensibilityListCore = ({
9797
typeof resMetaData?.resource?.filter === 'string' ||
9898
typeof generalFilter === 'string';
9999

100-
const filterFn = value =>
101-
applyFormula(
100+
const filterFn = async value =>
101+
await applyFormula(
102102
value,
103103
resMetaData?.resource?.filter || generalFilter,
104104
tBusola,

src/components/Extensibility/components-form/AlertRenderer.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect, useState } from 'react';
12
import { MessageStrip } from '@ui5/webcomponents-react';
23

34
import { useCreateResourceDescription } from 'components/Extensibility/helpers';
@@ -19,7 +20,7 @@ export function AlertRenderer({
1920
const { itemVars } = useVariables();
2021

2122
const rule = schema.get('schemaRule');
22-
const item = itemVars(resource, rule.itemVars, storeKeys);
23+
const item = itemVars(resource, rule?.itemVars, storeKeys);
2324

2425
const jsonata = useJsonata({
2526
resource: originalResource,
@@ -40,16 +41,23 @@ export function AlertRenderer({
4041
schemaType = 'Positive';
4142
}
4243

43-
function alertJsonata(alertFormula, item) {
44-
const [value, error] = jsonata(alertFormula, item);
45-
if (error) {
46-
console.warn('Widget::shouldBeVisible error:', error);
47-
return error.message;
48-
} else {
49-
return value;
44+
const [alertJsonata, setAlertJsonata] = useState('');
45+
46+
useEffect(() => {
47+
async function getAlertJsonata(alertFormula, item) {
48+
const [value, error] = await jsonata(alertFormula, item);
49+
if (error) {
50+
console.warn('Widget::shouldBeVisible error:', error);
51+
return error.message;
52+
} else {
53+
return value;
54+
}
5055
}
51-
}
52-
const alertLink = useCreateResourceDescription(alertJsonata(alert, item));
56+
getAlertJsonata(alert, item).then(res => setAlertJsonata(res));
57+
// eslint-disable-next-line react-hooks/exhaustive-deps
58+
}, [alert, item, originalResource, resource, embedResource, value]);
59+
60+
const alertLink = useCreateResourceDescription(alertJsonata);
5361
return (
5462
<MessageStrip
5563
design={schemaType}

src/components/Extensibility/components-form/Modules/Modules.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect, useState } from 'react';
12
import { useUIStore } from '@ui-schema/ui-schema';
23

34
import { useJsonata } from '../../hooks/useJsonata';
@@ -51,32 +52,37 @@ export function Modules({ storeKeys, resource, onChange, schema, required }) {
5152
const rule = schema.get('schemaRule');
5253

5354
const options = schema.get('options');
54-
let parsedOptions = {};
55-
56-
function makeJsonata(propObject) {
57-
if (typeof propObject === 'string') {
58-
const [newEnum] = jsonata(
59-
propObject,
60-
itemVars(resource, rule.itemVars, storeKeys),
61-
[],
62-
);
63-
return newEnum;
64-
}
65-
console.warn('Widget::Modules');
66-
return null;
67-
}
68-
69-
Object.keys(options).forEach(optionName => {
70-
if (
71-
optionName === 'name' &&
72-
!Array.isArray(makeJsonata(options[optionName]))
73-
) {
74-
let moduleName = makeJsonata(options[optionName]);
75-
parsedOptions[optionName] = [moduleName];
76-
} else {
77-
parsedOptions[optionName] = makeJsonata(options[optionName]);
55+
const [parsedOptions, setParsedOptions] = useState({});
56+
57+
useEffect(() => {
58+
async function makeJsonata(propObject) {
59+
if (typeof propObject === 'string') {
60+
const [newEnum] = await jsonata(
61+
propObject,
62+
itemVars(resource, rule?.itemVars, storeKeys),
63+
[],
64+
);
65+
return newEnum;
66+
}
67+
console.warn('Widget::Modules');
68+
return null;
7869
}
79-
});
70+
let parsedOpt = {};
71+
Promise.all(
72+
Object.keys(options).map(async optionName => {
73+
if (
74+
optionName === 'name' &&
75+
!Array.isArray(await makeJsonata(options[optionName]))
76+
) {
77+
let moduleName = await makeJsonata(options[optionName]);
78+
parsedOpt[optionName] = [moduleName];
79+
} else {
80+
parsedOpt[optionName] = await makeJsonata(options[optionName]);
81+
}
82+
}),
83+
).then(() => setParsedOptions(parsedOpt));
84+
// eslint-disable-next-line react-hooks/exhaustive-deps
85+
}, [itemVars, options, rule?.itemVars, storeKeys, resource, value]);
8086

8187
const Items = parsedOptions?.name?.map((name, index) => {
8288
if (!name)

src/components/Extensibility/components-form/MonacoRenderer.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useMemo } from 'react';
1+
import { useCallback, useEffect, useMemo, useState } from 'react';
22
import jsyaml from 'js-yaml';
33

44
import { Editor } from 'shared/components/MonacoEditorESM/Editor';
@@ -29,11 +29,11 @@ function formatValue(value, language, formatAsString) {
2929
}
3030
}
3131

32-
function getLanguage(jsonata, schema) {
32+
async function getLanguage(jsonata, schema) {
3333
const languageFormula = schema.get('language');
3434
if (!languageFormula) return 'json';
3535

36-
const [language, error] = jsonata(languageFormula);
36+
const [language, error] = await jsonata(languageFormula);
3737
return error ? 'json' : (language || '').toLowerCase();
3838
}
3939

@@ -58,7 +58,13 @@ export function MonacoRenderer({
5858
value,
5959
});
6060

61-
const language = getLanguage(jsonata, schema);
61+
const [language, setLanguage] = useState('');
62+
63+
useEffect(() => {
64+
getLanguage(jsonata, schema).then(res => setLanguage(res));
65+
// eslint-disable-next-line react-hooks/exhaustive-deps
66+
}, [schema, resource, value]);
67+
6268
const formatAsString = schema.get('formatAsString') ?? false;
6369
const formattedValue = formatValue(value, language, formatAsString);
6470
const defaultOpen = schema.get('defaultExpanded') ?? false;

0 commit comments

Comments
 (0)