Skip to content

Commit

Permalink
Merge pull request #228 from CSCfi/CSCFC4EMSCR-588_Array-values-in-no…
Browse files Browse the repository at this point in the history
…de-info-box

CSCFC4EMSCR-588 Show node property attribute values that are arrays
  • Loading branch information
rquazi authored Nov 7, 2024
2 parents df5afaa + 46d857a commit c23d9aa
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { InfoIcon } from '@app/common/components/shared-icons';
import { useTranslation } from 'next-i18next';
import { DropdownWrapper } from '@app/common/components/schema-info/schema-info.styles';
import TypeSelector from '@app/common/components/schema-info/schema-tree/node-info/type-selector';
import { IconLinkExternal } from 'suomifi-icons';
import {
setConfirmModalState,
setSelectedRootNode,
} from '@app/common/components/actionmenu/actionmenu.slice';
import { useStoreDispatch } from '@app/store';
import processHtmlLinks from '@app/common/utils/process-html-links';
import { ConstantAttribute } from '@app/common/interfaces/node.interface';
import RenderAttribute from '@app/common/components/schema-info/schema-tree/node-info/render-attribute';

export default function NodeInfo(props: {
treeData: RenderTree[];
Expand Down Expand Up @@ -46,11 +48,6 @@ export default function NodeInfo(props: {
setSelectedNode(newSelectedNode ?? selectedNode);
};

interface ConstantAttribute {
name: string;
value: string | undefined;
}

useEffect(() => {
if (selectedNode && selectedNode.properties) {
const nodeProperties: ConstantAttribute[] = [];
Expand All @@ -59,29 +56,18 @@ export default function NodeInfo(props: {
setNodeTypeAttribute(value as string);
continue;
}
let propertyValue;
if (typeof value === 'string') propertyValue = value.toString();
if (Array.isArray(value)) propertyValue = value;
nodeProperties.push({
name: key,
value: typeof value === 'string' ? value.toString() : undefined,
value: propertyValue,
});
}
setNodeAttributes(nodeProperties);
}
}, [isLeafNode, props.isNodeEditable, selectedNode]);

function processHtmlLinks(input: string | undefined) {
if (
input &&
(input.startsWith('http://') || input.startsWith('https://'))
) {
return (
<a href={input} target="_blank" rel="noreferrer">
{input} <IconLinkExternal />
</a>
);
}
return input;
}

function setAsRootNode(node: RenderTree | undefined) {
dispatch(setSelectedRootNode(node));
if (node) {
Expand Down Expand Up @@ -178,18 +164,13 @@ export default function NodeInfo(props: {
)}

{nodeAttributes.map((attrib) => (
<div className="col-12" key={self.crypto.randomUUID()} hidden={(attrib.name === '@id' ? true : false)}>
<div className="">{processHtmlLinks(attrib.name)}:</div>
<div className="attribute-font">
{processHtmlLinks(attrib.value)}
</div>
</div>
<RenderAttribute key={self.crypto.randomUUID()} attribute={attrib} />
))}
{props.isNodeEditable &&
isLeafNode &&
nodeTypeAttribute !== '' && (
<div className="col-12" key={self.crypto.randomUUID()}>
<div className="">@type:</div>
<div className='col-12' key={self.crypto.randomUUID()}>
<div>@type:</div>
<div className="attribute-font">
{processHtmlLinks(nodeTypeAttribute)}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ConstantAttribute } from '@app/common/interfaces/node.interface';
import processHtmlLinks from '@app/common/utils/process-html-links';

export default function RenderAttribute({ attribute }: { attribute: ConstantAttribute }) {
const valueWithLinks = processHtmlLinks(attribute.value);
let valueList;
// Add line breaks between values if multiple
if (Array.isArray(valueWithLinks)) {
valueList = [valueWithLinks[0]];
for (let i = 1; i < valueWithLinks.length; i++) {
valueList.push(
<span key={self.crypto.randomUUID()}>
<br />
{valueWithLinks[i]}
</span>
);
}
} else {
valueList = valueWithLinks;
}
return (
<div
className="col-12"
hidden={attribute.name === '@id'}
>
<div>{processHtmlLinks(attribute.name)}:</div>
<div className="attribute-font">{valueList}</div>
</div>
);
}
4 changes: 4 additions & 0 deletions mscr-ui/src/common/interfaces/node.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface ConstantAttribute {
name: string;
value: string | Array<string | number | boolean> | undefined;
}
24 changes: 24 additions & 0 deletions mscr-ui/src/common/utils/process-html-links.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { IconLinkExternal } from 'suomifi-icons';

// Takes a string or an array of primitive values and swaps urls to link elements
export default function processHtmlLinks(input: string | Array<string | number | boolean> | undefined) {
const inputArray = typeof input == 'string' ? [input] : input;
if (!inputArray) return undefined;
const output : Array<JSX.Element | string | number | boolean> = inputArray.map((inputPart) => {
if (typeof inputPart == 'string' && (inputPart.startsWith('http://') || inputPart.startsWith('https://'))) {
return (
<a key={inputPart} href={inputPart} target="_blank" rel="noreferrer">
{inputPart} <IconLinkExternal />
</a>
);
} else {
return inputPart;
}
});

if (typeof input == 'string') {
return output[0];
} else {
return output;
}
}

0 comments on commit c23d9aa

Please sign in to comment.