Skip to content

Commit

Permalink
[FIX] Fixed the performance issues caused by re-rendering (#39)
Browse files Browse the repository at this point in the history
* Memoized `ResultCard` component

* Cached `updateDownload` function

* Added `react-virtualized`

* Implemented a virtualization solution for rendering ResultCards

* Refactored `ResultCard`

* Updated Cypress config to include `esbuild-plugin-react-virtualized`

* Added `esbuild-plugin-react-virtualized` as a dev dependency

* Reverted changes related to virtualization
  • Loading branch information
rmanaem authored Mar 1, 2024
1 parent f1321b2 commit dce0493
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/components/ResultCard.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { memo } from 'react'
import Button from '@mui/material/Button';
import Card from '@mui/material/Card';
import CardContent from '@mui/material/CardContent';
Expand All @@ -6,7 +7,7 @@ import ButtonGroup from '@mui/material/ButtonGroup';
import Typography from '@mui/material/Typography';
import { modalities } from '../utils/constants';

function ResultCard({
const ResultCard = memo(({
nodeName,
datasetUUID,
datasetName,
Expand All @@ -24,8 +25,7 @@ function ResultCard({
imageModals: string[];
checked: boolean;
onCheckboxChange: (id: string) => void;
}) {
return (
}) => (
<Card data-cy={`card-${datasetUUID}`}>
<CardContent>
<div className="grid grid-cols-12 items-center gap-2">
Expand Down Expand Up @@ -59,7 +59,6 @@ function ResultCard({
</div>
</CardContent>
</Card>
);
}
));

export default ResultCard;
9 changes: 5 additions & 4 deletions src/components/ResultContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from 'react';
import { useState, useEffect, useCallback } from 'react';
import { Button, FormControlLabel, Checkbox, Typography } from '@mui/material';
import ResultCard from './ResultCard';
import { Result } from '../utils/types';
Expand Down Expand Up @@ -31,14 +31,14 @@ function ResultContainer({ result }: { result: Result[] | null }) {
* @param id - The uuid of the dataset to be added or removed from the download list
* @returns void
*/
function updateDownload(id: string) {
const updateDownload = useCallback((id: string) => {
setDownload((currDownload) => {
const newDownload = !currDownload.includes(id)
? [...currDownload, id]
: currDownload.filter((downloadID) => downloadID !== id);
return newDownload;
});
}
}, []);

function handleSelectAll(checked: boolean) {
if (result) {
Expand Down Expand Up @@ -170,6 +170,7 @@ function ResultContainer({ result }: { result: Result[] | null }) {
</Typography>
);
}

return (
<>
<div>
Expand Down Expand Up @@ -200,7 +201,7 @@ function ResultContainer({ result }: { result: Result[] | null }) {
numMatchingSubjects={item.num_matching_subjects}
imageModals={item.image_modals}
checked={download.includes(item.dataset_uuid)}
onCheckboxChange={(id) => updateDownload(id)}
onCheckboxChange={updateDownload}
/>
))}
</div>
Expand Down

0 comments on commit dce0493

Please sign in to comment.