Skip to content

Commit

Permalink
fix: Render Frame on initial send (#1067)
Browse files Browse the repository at this point in the history
* fix: Render Frame on initial send

Fixes rendering from on initial send
Cleans up types
Updates utils

* fix ratio types
  • Loading branch information
alexrisch authored Oct 23, 2024
1 parent f8c6299 commit c296212
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 38 deletions.
68 changes: 37 additions & 31 deletions components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
tertiaryBackgroundColor,
} from "@styles/colors";
import { getCleanAddress } from "@utils/evm/address";
import { FrameWithType, isFrameMessage } from "@utils/frames";
import differenceInCalendarDays from "date-fns/differenceInCalendarDays";
import React, { useCallback, useEffect, useMemo, useRef } from "react";
import {
ColorSchemeName,
Dimensions,
FlatList,
Platform,
Expand Down Expand Up @@ -73,9 +75,11 @@ const useRenderItem = ({
colorScheme,
}: {
xmtpAddress: string;
conversation: any;
framesStore: any;
colorScheme: any;
conversation: XmtpConversationWithUpdate | undefined;
framesStore: {
[frameUrl: string]: FrameWithType;
};
colorScheme: ColorSchemeName;
}) => {
return useCallback(
({ item }: { item: MessageToDisplay }) => {
Expand All @@ -85,41 +89,43 @@ const useRenderItem = ({
message={{ ...item }}
colorScheme={colorScheme}
isGroup={!!conversation?.isGroup}
isFrame={!!framesStore[item.content.toLowerCase()]}
isFrame={isFrameMessage(item, framesStore)}
/>
);
},
[colorScheme, xmtpAddress, conversation?.isGroup, framesStore]
);
};

const getItemType = (framesStore: any) => (item: MessageToDisplay) => {
const fromMeString = item.fromMe ? "fromMe" : "notFromMe";
if (
isContentType("text", item.contentType) &&
item.converseMetadata?.frames?.[0]
) {
const frameUrl = item.converseMetadata?.frames?.[0];
const frame = framesStore[frameUrl];
// Recycle frames with the same aspect ratio
return `FRAME-${
frame?.frameInfo?.image?.aspectRatio || "1.91.1"
}-${fromMeString}`;
} else if (
(isContentType("attachment", item.contentType) ||
isContentType("remoteAttachment", item.contentType)) &&
item.converseMetadata?.attachment?.size?.height &&
item.converseMetadata?.attachment?.size?.width
) {
const aspectRatio = (
item.converseMetadata.attachment.size.width /
item.converseMetadata.attachment.size.height
).toFixed(2);
return `ATTACHMENT-${aspectRatio}-${fromMeString}`;
} else {
return `${item.contentType}-${fromMeString}`;
}
};
const getItemType =
(framesStore: { [frameUrl: string]: FrameWithType }) =>
(item: MessageToDisplay) => {
const fromMeString = item.fromMe ? "fromMe" : "notFromMe";
if (
isContentType("text", item.contentType) &&
item.converseMetadata?.frames?.[0]
) {
const frameUrl = item.converseMetadata?.frames?.[0];
const frame = framesStore[frameUrl];
// Recycle frames with the same aspect ratio
return `FRAME-${
frame?.frameInfo?.image?.aspectRatio || "1.91:1"
}-${fromMeString}`;
} else if (
(isContentType("attachment", item.contentType) ||
isContentType("remoteAttachment", item.contentType)) &&
item.converseMetadata?.attachment?.size?.height &&
item.converseMetadata?.attachment?.size?.width
) {
const aspectRatio = (
item.converseMetadata.attachment.size.width /
item.converseMetadata.attachment.size.height
).toFixed(2);
return `ATTACHMENT-${aspectRatio}-${fromMeString}`;
} else {
return `${item.contentType}-${fromMeString}`;
}
};

const getListArray = (
xmtpAddress?: string,
Expand Down
2 changes: 1 addition & 1 deletion components/Chat/Frame/FramePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export default function FramePreview({
<FrameImage
frameImage={frame.frameImage}
frameImageAspectRatio={
frame.frameInfo?.image?.aspectRatio || "1.91.1"
frame.frameInfo?.image?.aspectRatio || "1.91:1"
}
linkToOpen={initialFrame.url}
useMemoryCache={!frame.isInitialFrame || Platform.OS === "web"}
Expand Down
1 change: 1 addition & 0 deletions components/Chat/Message/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ const ChatMessage = ({
message={message}
reactions={reactions}
hideBackground={hideBackground}
isFrame={isFrame}
>
{isContentType("text", message.contentType) && (
<FramesPreviews message={message} />
Expand Down
6 changes: 3 additions & 3 deletions components/Chat/Message/MessageActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
myMessageHighlightedBubbleColor,
} from "@styles/colors";
import { useConversationContext } from "@utils/conversation";
import { isFrameMessage } from "@utils/frames";
import { navigate } from "@utils/navigation";
import * as Haptics from "expo-haptics";
import React, {
Expand Down Expand Up @@ -66,6 +65,7 @@ type Props = {
[senderAddress: string]: MessageReaction[];
};
hideBackground: boolean;
isFrame: boolean;
};

enum ContextMenuActions {
Expand All @@ -79,6 +79,7 @@ export default function ChatMessageActions({
message,
reactions,
hideBackground = false,
isFrame,
}: Props) {
const isAttachment = isAttachmentMessage(message.contentType);
const isTransaction = isTransactionMessage(message.contentType);
Expand Down Expand Up @@ -286,15 +287,14 @@ export default function ChatMessageActions({
}, [message]);

const frameURL = useMemo(() => {
const isFrame = isFrameMessage(message);
if (isFrame) {
const frames = useFramesStore
.getState()
.getFramesForURLs(message.converseMetadata?.frames || []);
return frames[0]?.url;
}
return null;
}, [message]);
}, [message, isFrame]);

const animateInStyle = useAnimatedStyle(() => {
return {
Expand Down
10 changes: 7 additions & 3 deletions utils/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,15 @@ export const handleTxAction = async (
return { buttonPostUrl, txHash };
};

export const isFrameMessage = (message: MessageToDisplay): boolean => {
const framesStore = useFramesStore.getState().frames;
export const isFrameMessage = (
message: MessageToDisplay,
framesStore: {
[frameUrl: string]: FrameWithType;
}
): boolean => {
return (
isContentType("text", message.contentType) &&
!!message.converseMetadata?.frames?.[0] &&
!!framesStore[message.converseMetadata.frames[0].toLowerCase()]
!!framesStore[message.converseMetadata.frames[0].toLowerCase().trim()]
);
};

0 comments on commit c296212

Please sign in to comment.