1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-10 01:16:39 +02:00

refactor: memoize heavy components in project overview (#5241)

This PR memoizes some of the heavier components in our project overview
table
This commit is contained in:
Fredrik Strand Oseberg 2023-11-02 13:12:35 +01:00 committed by GitHub
parent 5b41abff97
commit 9cfade926e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 43 deletions

View File

@ -20,3 +20,7 @@ export const FeatureEnvironmentSeenCell: VFC<IFeatureSeenCellProps> = ({
/> />
); );
}; };
export const MemoizedFeatureEnvironmentSeenCell = React.memo(
FeatureEnvironmentSeenCell,
);

View File

@ -1,4 +1,4 @@
import { useMemo } from 'react'; import React, { useMemo } from 'react';
import { styled } from '@mui/material'; import { styled } from '@mui/material';
import { flexRow } from 'themes/themeStyles'; import { flexRow } from 'themes/themeStyles';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender'; import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
@ -22,6 +22,68 @@ const StyledSwitchContainer = styled('div', {
}), }),
})); }));
interface IFeatureToggleCellProps {
projectId: string;
environmentName: string;
isChangeRequestEnabled: boolean;
refetch: () => void;
onFeatureToggleSwitch: ReturnType<UseFeatureToggleSwitchType>['onToggle'];
value: boolean;
feature: ListItemType;
}
const FeatureToggleCellComponent = ({
value,
feature,
projectId,
environmentName,
isChangeRequestEnabled,
refetch,
onFeatureToggleSwitch,
}: IFeatureToggleCellProps) => {
const environment = feature.environments[environmentName];
const hasWarning = useMemo(
() =>
feature.someEnabledEnvironmentHasVariants &&
environment.variantCount === 0 &&
environment.enabled,
[feature, environment],
);
const onToggle = (newState: boolean, onRollback: () => void) => {
onFeatureToggleSwitch(newState, {
projectId,
featureId: feature.name,
environmentName,
environmentType: environment?.type,
hasStrategies: environment?.hasStrategies,
hasEnabledStrategies: environment?.hasEnabledStrategies,
isChangeRequestEnabled,
onRollback,
onSuccess: refetch,
});
};
return (
<StyledSwitchContainer hasWarning={hasWarning}>
<FeatureToggleSwitch
projectId={projectId}
value={value}
featureId={feature.name}
environmentName={environmentName}
onToggle={onToggle}
/>
<ConditionallyRender
condition={hasWarning}
show={<VariantsWarningTooltip />}
/>
</StyledSwitchContainer>
);
};
const MemoizedFeatureToggleCell = React.memo(FeatureToggleCellComponent);
export const createFeatureToggleCell = export const createFeatureToggleCell =
( (
projectId: string, projectId: string,
@ -37,43 +99,15 @@ export const createFeatureToggleCell =
value: boolean; value: boolean;
row: { original: ListItemType }; row: { original: ListItemType };
}) => { }) => {
const environment = feature.environments[environmentName];
const hasWarning = useMemo(
() =>
feature.someEnabledEnvironmentHasVariants &&
environment.variantCount === 0 &&
environment.enabled,
[feature, environment],
);
const onToggle = (newState: boolean, onRollback: () => void) => {
onFeatureToggleSwitch(newState, {
projectId,
featureId: feature.name,
environmentName,
environmentType: environment?.type,
hasStrategies: environment?.hasStrategies,
hasEnabledStrategies: environment?.hasEnabledStrategies,
isChangeRequestEnabled,
onRollback,
onSuccess: refetch,
});
};
return ( return (
<StyledSwitchContainer hasWarning={hasWarning}> <MemoizedFeatureToggleCell
<FeatureToggleSwitch value={value}
projectId={projectId} feature={feature}
value={value} projectId={projectId}
featureId={feature.name} environmentName={environmentName}
environmentName={environmentName} isChangeRequestEnabled={isChangeRequestEnabled}
onToggle={onToggle} refetch={refetch}
/> onFeatureToggleSwitch={onFeatureToggleSwitch}
<ConditionallyRender />
condition={hasWarning}
show={<VariantsWarningTooltip />}
/>
</StyledSwitchContainer>
); );
}; };

View File

@ -159,6 +159,7 @@ export const useFeatureToggleSwitch: UseFeatureToggleSwitchType = (
config.environmentName, config.environmentName,
shouldActivateDisabledStrategies, shouldActivateDisabledStrategies,
); );
setToastData({ setToastData({
type: 'success', type: 'success',
title: `Enabled in ${config.environmentName}`, title: `Enabled in ${config.environmentName}`,

View File

@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo, useState } from 'react'; import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { import {
Checkbox, Checkbox,
IconButton, IconButton,
@ -55,10 +55,10 @@ import { useGlobalLocalStorage } from 'hooks/useGlobalLocalStorage';
import FileDownload from '@mui/icons-material/FileDownload'; import FileDownload from '@mui/icons-material/FileDownload';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig'; import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import { ExportDialog } from 'component/feature/FeatureToggleList/ExportDialog'; import { ExportDialog } from 'component/feature/FeatureToggleList/ExportDialog';
import { RowSelectCell } from './RowSelectCell/RowSelectCell'; import { MemoizedRowSelectCell } from './RowSelectCell/RowSelectCell';
import { BatchSelectionActionsBar } from '../../../common/BatchSelectionActionsBar/BatchSelectionActionsBar'; import { BatchSelectionActionsBar } from '../../../common/BatchSelectionActionsBar/BatchSelectionActionsBar';
import { ProjectFeaturesBatchActions } from './ProjectFeaturesBatchActions/ProjectFeaturesBatchActions'; import { ProjectFeaturesBatchActions } from './ProjectFeaturesBatchActions/ProjectFeaturesBatchActions';
import { FeatureEnvironmentSeenCell } from '../../../common/Table/cells/FeatureSeenCell/FeatureEnvironmentSeenCell'; import { MemoizedFeatureEnvironmentSeenCell } from '../../../common/Table/cells/FeatureSeenCell/FeatureEnvironmentSeenCell';
import { useChangeRequestsEnabled } from 'hooks/useChangeRequestsEnabled'; import { useChangeRequestsEnabled } from 'hooks/useChangeRequestsEnabled';
import { ListItemType } from './ProjectFeatureToggles.types'; import { ListItemType } from './ProjectFeatureToggles.types';
import { createFeatureToggleCell } from './FeatureToggleSwitch/createFeatureToggleCell'; import { createFeatureToggleCell } from './FeatureToggleSwitch/createFeatureToggleCell';
@ -161,7 +161,9 @@ export const ProjectFeatureToggles = ({
<Checkbox {...getToggleAllRowsSelectedProps()} /> <Checkbox {...getToggleAllRowsSelectedProps()} />
), ),
Cell: ({ row }: any) => ( Cell: ({ row }: any) => (
<RowSelectCell {...row?.getToggleRowSelectedProps?.()} /> <MemoizedRowSelectCell
{...row?.getToggleRowSelectedProps?.()}
/>
), ),
maxWidth: 50, maxWidth: 50,
disableSortBy: true, disableSortBy: true,
@ -191,7 +193,7 @@ export const ProjectFeatureToggles = ({
accessor: 'lastSeenAt', accessor: 'lastSeenAt',
Cell: ({ value, row: { original: feature } }: any) => { Cell: ({ value, row: { original: feature } }: any) => {
return showEnvironmentLastSeen ? ( return showEnvironmentLastSeen ? (
<FeatureEnvironmentSeenCell feature={feature} /> <MemoizedFeatureEnvironmentSeenCell feature={feature} />
) : ( ) : (
<FeatureSeenCell value={value} /> <FeatureSeenCell value={value} />
); );

View File

@ -1,3 +1,4 @@
import React from 'react';
import { Box, Checkbox, styled } from '@mui/material'; import { Box, Checkbox, styled } from '@mui/material';
import { FC } from 'react'; import { FC } from 'react';
import { BATCH_SELECT } from 'utils/testIds'; import { BATCH_SELECT } from 'utils/testIds';
@ -23,3 +24,5 @@ export const RowSelectCell: FC<IRowSelectCellProps> = ({
<Checkbox onChange={onChange} title={title} checked={checked} /> <Checkbox onChange={onChange} title={title} checked={checked} />
</StyledBoxCell> </StyledBoxCell>
); );
export const MemoizedRowSelectCell = React.memo(RowSelectCell);