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: URL Crash #1084

Merged
merged 2 commits into from
Oct 23, 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
9 changes: 5 additions & 4 deletions components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useSelect } from "@data/store/storeHelpers";
import { FlashList } from "@shopify/flash-list";
import {
backgroundColor,
itemSeparatorColor,
tertiaryBackgroundColor,
} from "@styles/colors";
import { getCleanAddress } from "@utils/evm/address";
import { FrameWithType, isFrameMessage } from "@utils/frames";
import { FrameWithType } from "@utils/frames";
import differenceInCalendarDays from "date-fns/differenceInCalendarDays";
import React, { useCallback, useEffect, useMemo, useRef } from "react";
import {
Expand Down Expand Up @@ -89,7 +90,7 @@ const useRenderItem = ({
message={{ ...item }}
colorScheme={colorScheme}
isGroup={!!conversation?.isGroup}
isFrame={isFrameMessage(item, framesStore)}
isFrame={!!framesStore[item.content.toLowerCase().trim()]}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add safety checks for message content handling

While the case-insensitive comparison and whitespace trimming improve URL matching, the current implementation might crash if item.content is undefined.

Add null checks to prevent potential crashes:

-          isFrame={!!framesStore[item.content.toLowerCase().trim()]}
+          isFrame={!!item.content && !!framesStore[item.content.toLowerCase().trim()]}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
isFrame={!!framesStore[item.content.toLowerCase().trim()]}
isFrame={!!item.content && !!framesStore[item.content.toLowerCase().trim()]}

/>
);
},
Expand Down Expand Up @@ -382,7 +383,7 @@ export function Chat() {
styles.inChatRecommendations,
]);

const framesStore = useFramesStore().frames;
const { frames: framesStore } = useFramesStore(useSelect(["frames"]));

const showPlaceholder = useIsShowingPlaceholder({
messages: listArray,
Expand Down Expand Up @@ -538,7 +539,7 @@ export function ChatPreview() {
]
);

const framesStore = useFramesStore().frames;
const { frames: framesStore } = useFramesStore(useSelect(["frames"]));

const showPlaceholder = useIsShowingPlaceholder({
messages: listArray,
Expand Down
6 changes: 5 additions & 1 deletion components/Chat/Message/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ const ChatMessage = ({
const contentType = getMessageContentType(message.contentType);

const handleUrlPress = useCallback((url: string) => {
const uri = url.toLowerCase().startsWith("http") ? url : `https://${url}`;
const cleanedUrl = url.toLowerCase().trim();

const uri = cleanedUrl.startsWith("http")
? cleanedUrl
: `https://${cleanedUrl}`;

Linking.openURL(uri);
}, []);
Expand Down
10 changes: 7 additions & 3 deletions utils/messageContent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import emojiRegex from "emoji-regex";

export const getUrlToRender = (url: string) => {
const fullUrl = new URL(url);
return fullUrl.hostname;
export const getUrlToRender = (url: string): string | undefined => {
try {
const fullUrl = new URL(url);
return fullUrl.hostname;
} catch {
return undefined;
}
};

export const isAllEmojisAndMaxThree = (str: string) => {
Expand Down
Loading