Fix stream view crashing when failing to parse zap request

This commit is contained in:
hzrd149
2023-07-07 14:49:50 -05:00
parent dd4cb0bea3
commit 5d4a6801f4
2 changed files with 18 additions and 8 deletions

View File

@@ -0,0 +1,5 @@
---
"nostrudel": patch
---
Fix stream view crashing when failing to parse zap request

View File

@@ -1,4 +1,4 @@
import React, { useRef } from "react"; import React, { useMemo, useRef } from "react";
import { Box, Flex, Text } from "@chakra-ui/react"; import { Box, Flex, Text } from "@chakra-ui/react";
import { ParsedStream } from "../../../../helpers/nostr/stream"; import { ParsedStream } from "../../../../helpers/nostr/stream";
import { UserAvatar } from "../../../../components/user-avatar"; import { UserAvatar } from "../../../../components/user-avatar";
@@ -15,20 +15,25 @@ function ZapMessage({ zap, stream }: { zap: NostrEvent; stream: ParsedStream })
const ref = useRef<HTMLDivElement | null>(null); const ref = useRef<HTMLDivElement | null>(null);
useRegisterIntersectionEntity(ref, zap.id); useRegisterIntersectionEntity(ref, zap.id);
const { request, payment } = parseZapEvent(zap); const parsed = useMemo(() => {
if (!payment.amount) return null; try {
return parseZapEvent(zap);
} catch (e) {}
}, [zap]);
if (!parsed || !parsed.payment.amount) return null;
return ( return (
<TrustProvider event={request}> <TrustProvider event={parsed.request}>
<Flex direction="column" borderRadius="md" borderColor="yellow.400" borderWidth="1px" p="2" ref={ref}> <Flex direction="column" borderRadius="md" borderColor="yellow.400" borderWidth="1px" p="2" ref={ref}>
<Flex gap="2"> <Flex gap="2">
<LightningIcon color="yellow.400" /> <LightningIcon color="yellow.400" />
<UserAvatar pubkey={request.pubkey} size="xs" /> <UserAvatar pubkey={parsed.request.pubkey} size="xs" />
<UserLink pubkey={request.pubkey} fontWeight="bold" color="yellow.400" /> <UserLink pubkey={parsed.request.pubkey} fontWeight="bold" color="yellow.400" />
<Text>zapped {readablizeSats(payment.amount / 1000)} sats</Text> <Text>zapped {readablizeSats(parsed.payment.amount / 1000)} sats</Text>
</Flex> </Flex>
<Box> <Box>
<ChatMessageContent event={request} /> <ChatMessageContent event={parsed.request} />
</Box> </Box>
</Flex> </Flex>
</TrustProvider> </TrustProvider>