Skip to content

Commit

Permalink
render txo or keyimage table row with background highlight when it ma…
Browse files Browse the repository at this point in the history
…tches search param (#30)
  • Loading branch information
holtzman authored Jul 25, 2023
1 parent 5f44d25 commit af8e32d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 12 deletions.
15 changes: 12 additions & 3 deletions src/api/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import getBurns from "api/getBurns";
import getNetworkStatus from "api/getNetworkStatus";
import searchBlock from "api/searchBlock";

const getBlockInfo = async (blockIndex) => {
const getBlockInfo = async (blockIndex, highlightItem?: string) => {
const blockContents = await getBlock(blockIndex);
const mintInfo = await getMintInfo(blockIndex);
const burns = await getBurns(blockIndex);
return {
blockContents,
mintInfo,
burns
burns,
highlightItem
};
};

Expand Down Expand Up @@ -47,8 +48,16 @@ export async function currentBlockLoader({ params }) {

export async function byTxoCurrentBlockLoader({ params }) {
const foundBlock = await searchBlock(params.pubKeyOrKeyImage.trim());
let highlightItem: string = null;
if (foundBlock) {
return getBlockInfo(foundBlock.block.index);
highlightItem =
foundBlock.resultType == "TxOut"
? foundBlock.blockContents.outputs[foundBlock.txOut.blockContentsTxOutIndex]
.publicKey
: foundBlock.blockContents.keyImages[
foundBlock.keyImage.blockContentsKeyImageIndex
];
return getBlockInfo(foundBlock.block.index, highlightItem);
} else {
throw new Error(params.pubKeyOrKeyImage);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default function Header({ networkStatus }: { networkStatus: NetworkStatus
const foundBlock = await searchBlock(trimmedQuery);
if (foundBlock) {
setQuery("");
navigate(`/blocks/${foundBlock.block.index}`);
navigate(`/txos/${trimmedQuery}`);
return;
}

Expand Down
10 changes: 7 additions & 3 deletions src/components/current-block/CurrentBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const StyledCell = styled(TableCell)(() => ({
border: "none"
}));

export default function CurrentBlock({ blockContents, mintInfo, burns }: BlockInfo) {
export default function CurrentBlock({ blockContents, mintInfo, burns, highlightItem }: BlockInfo) {
const theme = useTheme();
const matches = useMediaQuery(theme.breakpoints.down("sm"));
const networkStatus = useNetworkStatus();
Expand Down Expand Up @@ -86,8 +86,12 @@ export default function CurrentBlock({ blockContents, mintInfo, burns }: BlockIn
</Box>
<Box sx={{ marginTop: 2 }}>
<Grid container spacing={2}>
<Txos blockContents={blockContents} burns={burns} />
<KeyImages blockContents={blockContents} />
<Txos
blockContents={blockContents}
burns={burns}
highlightItem={highlightItem}
/>
<KeyImages blockContents={blockContents} highlightItem={highlightItem} />
<Mints mintInfo={mintInfo} />
<MintConfigTxs mintInfo={mintInfo} />
<Signatures blockContents={blockContents} />
Expand Down
17 changes: 15 additions & 2 deletions src/components/current-block/KeyImages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useTheme } from "@mui/material/styles";
import { StyledCard, StyledCell } from "components/current-block/CurrentBlock";
import { Block } from "api/types";
import CopyableField from "components/CopyableField";
import { highlightColor } from "theme";

// handle full-service tech debt
function removeProtoBuffFromKeyImage(keyImage: string) {
Expand All @@ -23,7 +24,13 @@ function removeProtoBuffFromKeyImage(keyImage: string) {
return keyImage;
}

export default function KeyImages({ blockContents }: { blockContents: Block }) {
export default function KeyImages({
blockContents,
highlightItem
}: {
blockContents: Block;
highlightItem: string;
}) {
const theme = useTheme();
const matches = useMediaQuery(theme.breakpoints.down("sm"));

Expand All @@ -42,7 +49,13 @@ export default function KeyImages({ blockContents }: { blockContents: Block }) {
<Table size="small" padding="none">
<TableBody>
{blockContents.keyImages.map((k) => (
<TableRow key={k}>
<TableRow
key={k}
sx={{
bgcolor:
highlightItem === k ? highlightColor : "inherit"
}}
>
<StyledCell>
<CopyableField
text={removeProtoBuffFromKeyImage(k)}
Expand Down
16 changes: 13 additions & 3 deletions src/components/current-block/Txos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@ import { StyledCard, StyledCell } from "components/current-block/CurrentBlock";
import CopyableField from "components/CopyableField";
import { Block, BurnTx, TxOut } from "api/types";
import { getTokenAmount, TOKENS } from "utils/tokens";
import { highlightColor } from "theme";

export default function Txos({ blockContents, burns }: { blockContents: Block; burns: BurnTx[] }) {
export default function Txos({
blockContents,
burns,
highlightItem
}: {
blockContents: Block;
burns: BurnTx[];
highlightItem: string;
}) {
const theme = useTheme();
const matches = useMediaQuery(theme.breakpoints.down("md"));
const [renderMemo, setRenderMemo] = useState(false);
Expand All @@ -36,6 +45,7 @@ export default function Txos({ blockContents, burns }: { blockContents: Block; b

function RenderOutput({ txout }: { txout: TxOut }) {
const matchingBurn = burns.find(({ burn }) => burn.publicKeyHex === txout.publicKey);
const rowBgColor = highlightItem === txout.publicKey ? highlightColor : "inherit";

if (matchingBurn) {
// remove null characters
Expand All @@ -44,7 +54,7 @@ export default function Txos({ blockContents, burns }: { blockContents: Block; b
""
);
return (
<TableRow>
<TableRow sx={{ bgcolor: rowBgColor }}>
<StyledCell>
<CopyableField text={txout.publicKey} abbreviate={matches} />
</StyledCell>
Expand All @@ -68,7 +78,7 @@ export default function Txos({ blockContents, burns }: { blockContents: Block; b
}

return (
<TableRow>
<TableRow sx={{ bgcolor: rowBgColor }}>
<StyledCell>
<CopyableField text={txout.publicKey} abbreviate={matches} />
</StyledCell>
Expand Down
1 change: 1 addition & 0 deletions src/pages/CurrentBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type BlockInfo = {
blockContents: Block;
mintInfo: MintInfoResponse;
burns: BurnTx[];
highlightItem?: string;
};

export default function CurrentBlockPage() {
Expand Down
2 changes: 2 additions & 0 deletions src/theme.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createTheme } from "@mui/material/styles";

export const highlightColor = "LemonChiffon";

const theme = createTheme({
palette: {
primary: {
Expand Down

0 comments on commit af8e32d

Please sign in to comment.