Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPS-138166 - SPIKE - Frontend performance tests for the tree filter (Subtype and Mime Type) #1667

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,3 +1,5 @@
/* eslint-disable @liferay/liferay/no-abbreviations */
/* eslint-disable sort-keys */
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
Expand All @@ -13,7 +15,7 @@
*/

import PropTypes from 'prop-types';
import React from 'react';
import React, {useEffect, useState} from 'react';

import TreeFilter from './components/TreeFilter';
import {nodeTreeArrayMapper} from './utils/tree-utils';
Expand All @@ -23,12 +25,83 @@ const SelectTypeAndSubtype = ({
itemSelectorSaveEvent,
portletNamespace,
}) => {

/** ******************************
* =========================== *
TESTING WITH FAKE DATA CODE
* =========================== *
********************************/
const [mockData, setMockData] = useState([]);

const mocks = {
xs:
'http://localhost:8080/documents/20123/0/contentDashboardItemTypes_xs_25-25.json',
sm:
'http://localhost:8080/documents/20123/0/contentDashboardItemTypes_sm_25-150.json',
md:
'http://localhost:8080/documents/20123/0/contentDashboardItemTypes_md_65-95.json',
lg:
'http://localhost:8080/documents/20123/0/contentDashboardItemTypes_lg_25-500.json',
xl:
'http://localhost:8080/documents/20123/0/contentDashboardItemTypes_xl_250-125.json',
xxl:
'http://localhost:8080/documents/20123/0/contentDashboardItemTypes_xxl_550-400.json',
};

const SELECTED_MOCK = 'lg';

const fetchMockData = async () => {
// eslint-disable-next-line @liferay/portal/no-global-fetch
let response = await fetch(mocks[SELECTED_MOCK]);

response = await response.json();

const concatData = contentDashboardItemTypes.concat(

// this map aims to prevent duplicated keys errors

response.map((parent) => {
return {
...parent,
itemSubtypes: parent.itemSubtypes.map((child) => {
return {
...child,
label:
child.label +
' ' +
(Math.random() + 1).toString(36).substring(7),
};
}),
};
})
);

setMockData(concatData);
};

useEffect(() => {
fetchMockData();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

/** ******************************
* =========================== *
/END TESTING WITH FAKE DATA CODE
* =========================== *
********************************/

const nodes = nodeTreeArrayMapper({
childrenPropertyKey: 'itemSubtypes',
namePropertyKey: 'label',
nodeArray: contentDashboardItemTypes,
nodeArray: mockData,
});

// prevent the component to render during the mock data fetching

if (!nodes.length) {
return null;
}

return (
<TreeFilter
childrenPropertyKey="itemSubtypes"
Expand Down
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
Expand All @@ -17,7 +18,7 @@ import ClayLayout from '@clayui/layout';
import classNames from 'classnames';
import {Treeview} from 'frontend-js-components-web';
import PropTypes from 'prop-types';
import React, {useCallback, useMemo, useRef, useState} from 'react';
import React, {Profiler, useCallback, useMemo, useRef, useState} from 'react';

import {
filterNodes,
Expand Down Expand Up @@ -130,6 +131,18 @@ const TreeFilter = ({
setFilterQuery('');
};

const onRenderCallback = (
id, // the "id" prop of the Profiler tree that has just committed
phase, // either "mount" (if the tree just mounted) or "update" (if it re-rendered)
actualDuration, // time spent rendering the committed update
baseDuration, // estimated time to render the entire subtree without memoization
startTime, // when React began rendering this update
commitTime, // when React committed this update
interactions // the Set of interactions belonging to this update
) => {
console.log(actualDuration);
};

return (
<div className="tree-filter">
<form
Expand Down Expand Up @@ -189,14 +202,16 @@ const TreeFilter = ({
className="tree-filter-type-tree"
id={`${portletNamespace}typeContainer`}
>
<Treeview
NodeComponent={Treeview.Card}
inheritSelection
initialSelectedNodeIds={initialSelectedNodeIds}
multiSelection
nodes={computedNodes()}
onSelectedNodesChange={handleSelectionChange}
/>
<Profiler id="Treeview" onRender={onRenderCallback}>
<Treeview
NodeComponent={Treeview.Card}
inheritSelection
initialSelectedNodeIds={initialSelectedNodeIds}
multiSelection
nodes={computedNodes()}
onSelectedNodesChange={handleSelectionChange}
/>
</Profiler>

{!computedNodes().length && (
<div className="border-0 pt-0 sheet taglib-empty-result-message">
Expand Down