Skip to content

Commit c1f84e7

Browse files
Markdown/Iframe and Url Params evaluation (#391)
* v1 * Oh Darling, don't play me dirty * Fix on empty matches * Fix regex last charater and spaces * Foie gras * Added comments --------- Co-authored-by: Niels de Jong <[email protected]>
1 parent dd9d4d7 commit c1f84e7

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

src/card/view/CardView.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ const NeoCardView = ({
4747
const [lastRunTimestamp, setLastRunTimestamp] = useState(Date.now());
4848

4949
const getLocalParameters = (parse_string): any => {
50+
if (!parse_string || !globalParameters) {
51+
return {};
52+
}
53+
5054
let re = /(?:^|\W)\$(\w+)(?!\w)/g;
5155
let match;
5256

@@ -59,9 +63,6 @@ const NeoCardView = ({
5963
localQueryVariables.push(match[1]);
6064
}
6165

62-
if (!globalParameters) {
63-
return {};
64-
}
6566
return Object.fromEntries(
6667
Object.entries(globalParameters).filter(([local]) => localQueryVariables.includes(local))
6768
);
@@ -101,9 +102,8 @@ const NeoCardView = ({
101102
) : (
102103
<></>
103104
);
104-
const localParameters = getLocalParameters(
105-
`${query}//${settings.drilldownLink}` !== undefined ? settings.drilldownLink : ''
106-
);
105+
106+
const localParameters = { ...getLocalParameters(query), ...getLocalParameters(settings.drilldownLink) };
107107
const reportTypes = getReportTypes(extensions);
108108
const withoutFooter =
109109
reportTypes[type] && reportTypes[type].withoutFooter

src/chart/ChartUtils.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ export function valueIsObject(value) {
9494
return className == 'Object';
9595
}
9696

97+
export function toNumber({ low, high }) {
98+
let res = high;
99+
100+
for (let i = 0; i < 32; i++) {
101+
res *= 2;
102+
}
103+
104+
return low + res;
105+
}
106+
97107
export function getRecordType(value) {
98108
// mui data-grid native column types are: 'string' (default),
99109
// 'number', 'date', 'dateTime', 'boolean' and 'singleSelect'
@@ -122,6 +132,9 @@ export function getRecordType(value) {
122132
} else if (valueIsArray(value)) {
123133
return 'array';
124134
} else if (valueIsObject(value)) {
135+
if (!isNaN(toNumber(value))) {
136+
return 'objectNumber';
137+
}
125138
return 'object';
126139
}
127140

@@ -167,13 +180,40 @@ export function replaceDashboardParameters(str, parameters) {
167180
if (!str) {
168181
return '';
169182
}
183+
let rx = /`.([^`]*)`/g;
184+
let regexSquareBrackets = /\[(.*?)\]/g;
185+
186+
/**
187+
* Define function to access elements in an array/object type dashboard parameter.
188+
* @param _ needed for str.replace(), unused.
189+
* @param p1 - the original string.
190+
* @returns an updated markdown with injected parameters.
191+
*/
192+
const parameterElementReplacer = (_, p1) => {
193+
// Find (in the markdown) occurences of the parameter `$neodash_movie_title[index]` or `$neodash_movie_title[key]`.
194+
let matches = p1.match(regexSquareBrackets);
195+
let param = p1.split('[')[0].replace(`$`, '').trim();
196+
let val = parameters?.[param] || null;
197+
198+
// Inject the element at that index/key into the markdown as text.
199+
matches?.forEach((m) => {
200+
let i = m.replace(/[[\]']+/g, '');
201+
i = isNaN(i) ? i.replace(/['"']+/g, '') : Number(i);
202+
val = val ? val[i] : null;
203+
});
204+
205+
return RenderSubValue(val);
206+
};
207+
208+
let newString = str.replace(rx, parameterElementReplacer);
209+
170210
Object.keys(parameters).forEach((key) => {
171-
str = str.replaceAll(`$${key}`, parameters[key] !== null ? parameters[key] : '');
211+
newString = newString.replaceAll(`$${key}`, parameters[key] !== null ? parameters[key] : '');
172212
});
173-
return str;
213+
214+
return newString;
174215
}
175216

176-
// Replaces all global dashboard parameters inside a string with their values.
177217
export function replaceDashboardParametersInString(str, parameters) {
178218
Object.keys(parameters).forEach((key) => {
179219
str = str.replaceAll(`$${key}`, parameters[key]);
@@ -197,6 +237,7 @@ export const downloadComponentAsImage = (ref) => {
197237
};
198238

199239
import { QueryResult, Record as Neo4jRecord } from 'neo4j-driver';
240+
import { RenderSubValue } from '../report/ReportRecordProcessing';
200241
import { DEFAULT_NODE_LABELS } from '../config/ReportConfig';
201242

202243
/**

src/report/ReportRecordProcessing.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import React from 'react';
22
import { Chip } from '@material-ui/core';
33
import { withStyles } from '@material-ui/core/styles';
44
import Tooltip from '@material-ui/core/Tooltip';
5-
import { getRecordType, valueIsArray, valueIsNode, valueIsPath, valueIsRelationship } from '../chart/ChartUtils';
5+
import {
6+
getRecordType,
7+
toNumber,
8+
valueIsArray,
9+
valueIsNode,
10+
valueIsPath,
11+
valueIsRelationship,
12+
} from '../chart/ChartUtils';
613

714
/**
815
* Collects all node labels and node properties in a set of Neo4j records.
@@ -308,6 +315,10 @@ export const rendererForType: any = {
308315
type: 'number',
309316
renderValue: (c) => RenderNumber(c.value),
310317
},
318+
objectNumber: {
319+
type: 'number',
320+
renderValue: (c) => RenderNumber(toNumber(c.value)),
321+
},
311322
null: {
312323
type: 'string',
313324
renderValue: (c) => RenderString(c.value),

0 commit comments

Comments
 (0)