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/blocklist loading #1689

Merged
merged 1 commit into from
Jun 24, 2024
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
59 changes: 42 additions & 17 deletions src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useCallback, useRef } from 'react';
import { ReactNode, useCallback, useRef } from 'react';

import { Section } from '../../../../common/components/Section';
import { Stack } from '../../../../ui/Stack';
Expand All @@ -11,41 +11,66 @@
import { BlocksPageBlockListGrouped } from './BlocksPageBlockListGrouped';
import { BlocksPageBlockListUngrouped } from './BlocksPageBlockListUngrouped';

export function BlocksPageBlockListLayout({ children }: { children: ReactNode }) {

Check warning on line 14 in src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx#L14

Added line #L14 was not covered by tests
return <Section>{children}</Section>;
}

export function BlocksPageControlsLayout({

Check warning on line 18 in src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx#L18

Added line #L18 was not covered by tests
liveUpdates,
children,
}: {
liveUpdates?: boolean;
children: ReactNode;
}) {

Check warning on line 24 in src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx#L24

Added line #L24 was not covered by tests
return (
<Stack
marginX={-6}
px={6}
py={5}
borderBottom={liveUpdates ? '1px solid var(--stacks-colors-borderPrimary)' : 'none'}
>
{children}
</Stack>
);
}

function BlocksPageBlockListBase() {
const { groupedByBtc, setGroupedByBtc, liveUpdates, setLiveUpdates } = useBlockListContext();

const lastClickTimeRef = useRef(0);
const toggleLiveUpdates = useCallback(() => {
const now = Date.now();
if (now - lastClickTimeRef.current > 2000) {
lastClickTimeRef.current = now;
setLiveUpdates(!liveUpdates);
}
}, [liveUpdates, setLiveUpdates]);
const toggleLiveUpdates = useCallback(
(immediately?: boolean) => {
const now = Date.now();

Check warning on line 43 in src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx#L41-L43

Added lines #L41 - L43 were not covered by tests
if (immediately || now - lastClickTimeRef.current > 2000) {
lastClickTimeRef.current = now;
setLiveUpdates(!liveUpdates);

Check warning on line 46 in src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx#L45-L46

Added lines #L45 - L46 were not covered by tests
}
},
[liveUpdates, setLiveUpdates]
);

return (
<Section>
<Stack
marginX={-6}
px={6}
borderBottom={liveUpdates ? '1px solid var(--stacks-colors-borderPrimary)' : 'none'}
>
<BlocksPageBlockListLayout>
<BlocksPageControlsLayout liveUpdates={liveUpdates}>
<Controls
groupByBtc={{
onChange: () => {
setGroupedByBtc(!groupedByBtc);
if (liveUpdates) {
toggleLiveUpdates(true);

Check warning on line 60 in src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx#L60

Added line #L60 was not covered by tests
}
},
isChecked: groupedByBtc,
}}
liveUpdates={{
onChange: toggleLiveUpdates,
onChange: () => toggleLiveUpdates(),

Check warning on line 66 in src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/BlocksPage/BlocksPageBlockList.tsx#L66

Added line #L66 was not covered by tests
isChecked: liveUpdates,
}}
horizontal={true}
/>
</Stack>
</BlocksPageControlsLayout>
{groupedByBtc ? <BlocksPageBlockListGrouped /> : <BlocksPageBlockListUngrouped />}
</Section>
</BlocksPageBlockListLayout>
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { Suspense } from 'react';
import { Suspense, useMemo } from 'react';

import { ListFooter } from '../../../../common/components/ListFooter';
import { Section } from '../../../../common/components/Section';
Expand All @@ -17,22 +17,26 @@
const { liveUpdates } = useBlockListContext();
const { blockList, updateBlockList, isFetchingNextPage, hasNextPage, fetchNextPage } =
useBlocksPageBlockListGrouped();
const latestBlock = useMemo(() => blockList?.[0], [blockList]);

Check warning on line 20 in src/app/_components/BlockList/BlocksPage/BlocksPageBlockListGrouped.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/BlocksPage/BlocksPageBlockListGrouped.tsx#L20

Added line #L20 was not covered by tests

return (
<>
{!liveUpdates && <UpdateBar onClick={updateBlockList} />}
{!liveUpdates && <UpdateBar onClick={updateBlockList} latestBlock={latestBlock} />}
<Flex flexDirection="column" gap={4} pt={4}>
<BlockListGrouped blockList={blockList} minimized={false} stxBlocksLimit={10} />
<BlockListGrouped
blockList={blockList}
minimized={false}
stxBlocksLimit={10}
onlyShowStxBlocksForFirstBtcBlock={true}
/>
</Flex>
<Box pt={5} pb={5}>
{!liveUpdates && (
<ListFooter
isLoading={isFetchingNextPage}
hasNextPage={hasNextPage}
fetchNextPage={fetchNextPage}
label={'blocks'}
/>
)}
<ListFooter
isLoading={isFetchingNextPage}
hasNextPage={hasNextPage}
fetchNextPage={fetchNextPage}
label={'blocks'}
/>
</Box>
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { Suspense } from 'react';
import { Suspense, useMemo } from 'react';

import { ListFooter } from '../../../../common/components/ListFooter';
import { Section } from '../../../../common/components/Section';
Expand All @@ -17,20 +17,19 @@

const { blockList, hasNextPage, fetchNextPage, isFetchingNextPage, updateBlockList } =
useBlocksPageBlockListUngrouped();
const latestBlock = useMemo(() => blockList?.[0], [blockList]);

Check warning on line 20 in src/app/_components/BlockList/BlocksPage/BlocksPageBlockListUngrouped.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/BlocksPage/BlocksPageBlockListUngrouped.tsx#L20

Added line #L20 was not covered by tests

return (
<Box pb={6}>
{!liveUpdates && <UpdateBar onClick={updateBlockList} />}
<BlockListUngrouped blockList={blockList} />
{!liveUpdates && <UpdateBar onClick={updateBlockList} latestBlock={latestBlock} />}
<BlockListUngrouped blockList={blockList} stxBlocksLimit={10} />
<Box pt={4}>
{!liveUpdates && (
<ListFooter
isLoading={isFetchingNextPage}
hasNextPage={hasNextPage}
fetchNextPage={fetchNextPage}
label={'blocks'}
/>
)}
<ListFooter
isLoading={isFetchingNextPage}
hasNextPage={hasNextPage}
fetchNextPage={fetchNextPage}
label={'blocks'}
/>
</Box>
</Box>
);
Expand Down
4 changes: 3 additions & 1 deletion src/app/_components/BlockList/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function ControlsLayout({
children: ReactNode;
} & FlexProps) {
return (
<Flex py={4} direction={horizontal ? ['column', 'row'] : 'column'} {...rest}>
<Flex direction={horizontal ? ['column', 'row'] : 'column'} gap={3} {...rest}>
{children}
</Flex>
);
Expand All @@ -35,6 +35,7 @@ export function Controls({ groupByBtc, liveUpdates, horizontal, ...rest }: Contr
<FormLabel
htmlFor="group-by-btc"
mb="0"
mr={0}
fontSize={'14px'}
lineHeight={'1.5em'}
fontWeight={400}
Expand All @@ -50,6 +51,7 @@ export function Controls({ groupByBtc, liveUpdates, horizontal, ...rest }: Contr
<FormLabel
htmlFor="live-updates"
mb="0"
mr={0}
fontSize={'14px'}
lineHeight={'1.5em'}
fontWeight={400}
Expand Down
99 changes: 68 additions & 31 deletions src/app/_components/BlockList/Grouped/BlockListGrouped.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArrowElbowLeftDown } from '@phosphor-icons/react';
import { ArrowElbowLeftDown, Clock } from '@phosphor-icons/react';
import React, { ReactNode, useMemo } from 'react';

import { BlockLink, ExplorerLink } from '../../../../common/components/ExplorerLinks';
Expand Down Expand Up @@ -80,12 +80,18 @@
<LineAndNode rowHeight={14} width={6} icon={icon} isLast={isLast} />
<BlockLink hash={stxBlock.hash}>
<Text color="text" fontWeight="medium" fontSize="sm">
#{stxBlock.height}
#{stxBlock.height} isLast: {isLast?.toString()}
</Text>
</BlockLink>
</Flex>

<HStack divider={<Caption>∙</Caption>} gap={1} whiteSpace="nowrap" gridColumn="3 / 4">
<HStack
divider={<Caption>∙</Caption>}
gap={1}
whiteSpace="nowrap"
gridColumn="3 / 4"
justifyContent="flex-end"
>
<BlockLink hash={stxBlock.hash}>
<Text color="textSubdued" fontWeight="medium" fontSize="xs" whiteSpace="nowrap">
{truncateMiddle(stxBlock.hash, 3)}
Expand Down Expand Up @@ -176,7 +182,7 @@
stxBlock={stxBlock}
minimized={minimized}
isFirst={i === 0}
isLast={i === stxBlocks.length - 1 && numStxBlocksNotDisplayed === 0}
isLast={i === stxBlocks.length - 1 && numStxBlocksNotDisplayed <= 0}
/>
{i < stxBlocks.length - 1 && (
<Box gridColumn={'1/5'} borderBottom={'1px'} borderColor="borderSecondary"></Box>
Expand Down Expand Up @@ -206,35 +212,52 @@
px={PADDING}
borderBottom={minimized ? '1px solid var(--stacks-colors-borderPrimary)' : 'none'}
flexWrap={'wrap'}
// height={5}
>
<Flex alignItems={'center'} gap={1.5} flexWrap={'nowrap'}>
<Icon as={ArrowElbowLeftDown} size={3.5} color={'textSubdued'} />
<Icon as={ArrowElbowLeftDown} size={3.5} color="textSubdued" />
<Icon as={BitcoinIcon} size={4.5} />
{isFirst ? (
<Text fontSize="sm" color={'textSubdued'}>
{btcBlock.height}
</Text>
<Flex height="full" alignItems="center">
<Text fontSize="sm" color="textSubdued">
Next Bitcoin block
</Text>
</Flex>
) : (
<ExplorerLink fontSize="sm" color={'textSubdued'} href={`/btcblock/${btcBlock.hash}`}>
#{btcBlock.height}
</ExplorerLink>
<Flex height="full" alignItems="center">
<ExplorerLink
fontSize="sm"
color="textSubdued"
href={`/btcblock/${btcBlock.hash}`}
height="full"
>
#{btcBlock.height}
</ExplorerLink>
</Flex>
)}
</Flex>
<HStack divider={<Caption>∙</Caption>} gap={1} flexWrap={'wrap'}>
<Box>
{isFirst ? (
<Text fontSize="xs" color={'textSubdued'} whiteSpace={'nowrap'}>
{truncateMiddle(btcBlock.hash, 6)}
</Text>
<Flex gap={1} alignItems="center">
<Icon as={Clock} size={4} color="iconSubdued" />
<Text color="textSubdued" fontSize="xs">
Unconfirmed
</Text>
</Flex>
) : (
<ExplorerLink
fontSize="xs"
color={'textSubdued'}
href={`/btcblock/${btcBlock.hash}`}
whiteSpace={'nowrap'}
></ExplorerLink>
<HStack divider={<Caption>∙</Caption>} gap={1} flexWrap={'wrap'}>
<ExplorerLink
fontSize="xs"
color="textSubdued"
href={`/btcblock/${btcBlock.hash}`}
whiteSpace={'nowrap'}
>
{truncateMiddle(btcBlock.hash, 6)}
</ExplorerLink>
<Timestamp ts={btcBlock.timestamp} whiteSpace={'nowrap'} />
</HStack>
)}
<Timestamp ts={btcBlock.timestamp} whiteSpace={'nowrap'} />
</HStack>
</Box>
</Flex>
);
}
Expand Down Expand Up @@ -266,12 +289,14 @@
isFirst,
stxBlocksLimit,
minimized = false,
onlyShowStxBlocksForFirstBtcBlock,
}: {
btcBlock: BlockListBtcBlock;
stxBlocks: BlockListStxBlock[];
isFirst: boolean;
stxBlocksLimit?: number;
minimized?: boolean;
onlyShowStxBlocksForFirstBtcBlock?: boolean;
}) {
const unaccountedStxBlocks = btcBlock.blockCount ? stxBlocks.length - btcBlock.blockCount : 0;
const unaccountedTxs = useMemo(
Expand All @@ -293,21 +318,30 @@
? btcBlock.blockCount + unaccountedStxBlocks
: btcBlock.blockCount
: undefined;
const numStxBlocksNotDisplayed = blocksCount ? blocksCount - (stxBlocksLimit || 0) : 0;
const numStxBlocksNotDisplayed =
onlyShowStxBlocksForFirstBtcBlock && !isFirst
? blocksCount
? blocksCount
: 0

Check warning on line 325 in src/app/_components/BlockList/Grouped/BlockListGrouped.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Grouped/BlockListGrouped.tsx#L324-L325

Added lines #L324 - L325 were not covered by tests
: blocksCount
? blocksCount - (stxBlocksLimit || 0)
: 0;

Check warning on line 328 in src/app/_components/BlockList/Grouped/BlockListGrouped.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Grouped/BlockListGrouped.tsx#L328

Added line #L328 was not covered by tests
const displayedStxBlocks = useMemo(
() => (stxBlocksLimit ? stxBlocks.slice(0, stxBlocksLimit) : stxBlocks),
[stxBlocks, stxBlocksLimit]
);
return (
<Box border={'1px'} rounded={'lg'} p={PADDING}>
<BitcoinHeader btcBlock={btcBlock} minimized={minimized} isFirst={isFirst} />
<ScrollableBox>
<BurnBlockGroupGrid
stxBlocks={displayedStxBlocks}
minimized={minimized}
numStxBlocksNotDisplayed={numStxBlocksNotDisplayed}
/>
</ScrollableBox>
{onlyShowStxBlocksForFirstBtcBlock && !isFirst ? null : (
<ScrollableBox>
<BurnBlockGroupGrid
stxBlocks={displayedStxBlocks}
minimized={minimized}
numStxBlocksNotDisplayed={numStxBlocksNotDisplayed}
/>
</ScrollableBox>
)}
{numStxBlocksNotDisplayed > 0 ? (
<BlockCount
count={numStxBlocksNotDisplayed}
Expand All @@ -334,10 +368,12 @@
blockList,
minimized,
stxBlocksLimit,
onlyShowStxBlocksForFirstBtcBlock,
}: {
blockList: BlockListData[];
minimized: boolean;
stxBlocksLimit?: number;
onlyShowStxBlocksForFirstBtcBlock?: boolean;
}) {
return (
<BlockListGroupedLayout>
Expand All @@ -349,6 +385,7 @@
minimized={minimized}
stxBlocksLimit={stxBlocksLimit}
isFirst={i === 0}
onlyShowStxBlocksForFirstBtcBlock={onlyShowStxBlocksForFirstBtcBlock}
/>
))}
</BlockListGroupedLayout>
Expand Down
Loading
Loading