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

fix: block list memoization #1399

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 37 additions & 42 deletions src/app/_components/BlockList/AnimatedBlockAndMicroblocksItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,41 @@ export const animationDuration = 0.8;
export const AnimatedBlockAndMicroblocksItem: FC<{
block: EnhancedBlock;
onAnimationExit?: () => void;
}> = memo(
({ block, onAnimationExit }) => {
const [show, setShow] = useState(!block.animate);
useEffect(() => {
if (block.animate) {
setTimeout(() => {
setShow(true);
}, 100);
}
}, [block.animate]);
useEffect(() => {
if (block.destroy) {
setShow(false);
}
}, [block.destroy]);
}> = ({ block, onAnimationExit }) => {
const [show, setShow] = useState(!block.animate);
useEffect(() => {
if (block.animate) {
setTimeout(() => {
setShow(true);
}, 100);
}
}, [block.animate]);
useEffect(() => {
if (block.destroy) {
setShow(false);
}
}, [block.destroy]);

return (
<>
<Box borderBottom={'1px'} _last={{ borderBottom: 'none' }}>
<Collapse
in={show}
animateOpacity
transition={{
enter: { duration: animationDuration },
exit: { duration: animationDuration },
}}
onAnimationComplete={state => {
if (state === 'exit') {
onAnimationExit?.();
}
}}
data-testid={`block-item-${block.hash}`}
>
<BlockAndMicroblocksItem block={block} />
</Collapse>
</Box>
</>
);
},
(prevProps, currentProps) =>
prevProps.block.hash === currentProps.block.hash &&
prevProps.block.destroy === currentProps.block.destroy
);
return (
<>
<Box borderBottom={'1px'} _last={{ borderBottom: 'none' }}>
<Collapse
in={show}
animateOpacity
transition={{
enter: { duration: animationDuration },
exit: { duration: animationDuration },
}}
onAnimationComplete={state => {
if (state === 'exit') {
onAnimationExit?.();
}
}}
data-testid={`block-item-${block.hash}`}
>
<BlockAndMicroblocksItem block={block} />
</Collapse>
</Box>
</>
);
};
83 changes: 40 additions & 43 deletions src/app/_components/BlockList/BlockAndMicroblocksItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo } from 'react';
import React from 'react';

import { Block } from '@stacks/stacks-blockchain-api-types';

Expand All @@ -11,45 +11,42 @@
import { BlockListItem } from './BlockListItem';
import { MicroblockListItem } from './MicroblockListItem';

export const BlockAndMicroblocksItem: React.FC<{ block: Block }> = memo(
({ block }) => {
return (
<AccordionItem
border={'none'}
borderBottom={'1px solid var(--stacks-colors-border)'}
_last={{ border: 'none' }}
>
<Flex gap={'6px'}>
<BlockListItem block={block} data-test={`block-${block.hash}`} />
<AccordionButton
flexGrow={0}
flexShrink={0}
width={'30px'}
ml={'auto'}
p={0}
justifyContent={'center'}
>
<AccordionIcon />
</AccordionButton>
</Flex>
<AccordionPanel p={'0 30px 0 0'}>
{!!block.microblocks_accepted?.length ? (
block.microblocks_accepted.map((microblockHash, microblockIndex) => (
<MicroblockListItem
blockTime={block.burn_block_time}
hash={microblockHash}
index={microblockIndex}
key={microblockHash}
/>
))
) : (
<Text fontSize={'14px'} p={'20px'} align={'center'}>
No Microblocks
</Text>
)}
</AccordionPanel>
</AccordionItem>
);
},
(prevProps, currentProps) => prevProps.block.hash === currentProps.block.hash
);
export const BlockAndMicroblocksItem: React.FC<{ block: Block }> = ({ block }) => {
return (
<AccordionItem
border={'none'}
borderBottom={'1px solid var(--stacks-colors-border)'}
_last={{ border: 'none' }}
>
<Flex gap={'6px'}>
<BlockListItem block={block} data-test={`block-${block.hash}`} />
<AccordionButton
flexGrow={0}
flexShrink={0}
width={'30px'}
ml={'auto'}
p={0}
justifyContent={'center'}
>
<AccordionIcon />
</AccordionButton>
</Flex>
<AccordionPanel p={'0 30px 0 0'}>
{!!block.microblocks_accepted?.length ? (
block.microblocks_accepted.map((microblockHash, microblockIndex) => (
<MicroblockListItem

Check warning on line 37 in src/app/_components/BlockList/BlockAndMicroblocksItem.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/BlockAndMicroblocksItem.tsx#L37

Added line #L37 was not covered by tests
blockTime={block.burn_block_time}
hash={microblockHash}
index={microblockIndex}
key={microblockHash}
/>
))
) : (
<Text fontSize={'14px'} p={'20px'} align={'center'}>
No Microblocks
</Text>
)}
</AccordionPanel>
</AccordionItem>
);
};
2 changes: 0 additions & 2 deletions src/app/sandbox/Wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ import { Grid } from '../../ui/Grid';
import { HStack } from '../../ui/HStack';
import { Icon } from '../../ui/Icon';
import { IconButton } from '../../ui/IconButton';
import { useColorMode } from '../../ui/hooks/useColorMode';
import { Caption } from '../../ui/typography';
import { PageTitle } from '../_components/PageTitle';
import { useUser } from './hooks/useUser';
import { ConnectToStacks } from './layout/ConnectToStacks';
import { RightPanelSkeleton } from './layout/RightPanelSkeleton';
import { SideNav } from './layout/SideNav';
import Loading from './loading';
import { selectShowRightPanel, setUserData, toggleRightPanel } from './sandbox-slice';

const RightPanel = dynamic(() => import('./layout/RightPanel').then(mod => mod.RightPanel), {
Expand Down
Loading