Skip to content

Commit

Permalink
Implement more hooks in Chat and ChatPreview components
Browse files Browse the repository at this point in the history
  • Loading branch information
lourou committed Oct 2, 2024
1 parent 6def7b2 commit 0744094
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,31 @@ const getListArray = (
return reverseArray;
};

const useAnimatedListView = (
conversation: XmtpConversationWithUpdate | undefined
) => {
// The first message was really buggy on iOS & Android and this is due to FlashList
// so we keep FlatList for new convos and switch to FlashList for bigger convos
// that need great perf.
return useMemo(() => {
const isConversationNotPending = conversation && !conversation.pending;
return isConversationNotPending && Platform.OS !== "web"
? ReanimatedFlashList
: ReanimatedFlatList;
}, [conversation]);
};

const useIsShowingPlaceholder = (
listArray: MessageToDisplay[],
isBlockedPeer: boolean,
conversation: XmtpConversationWithUpdate | undefined
): boolean => {
return listArray.length === 0 || isBlockedPeer || !conversation;
};

export function Chat() {
const conversation = useConversationContext("conversation");
const AnimatedListView = useAnimatedListView(conversation);
const isBlockedPeer = useConversationContext("isBlockedPeer");
const onReadyToFocus = useConversationContext("onReadyToFocus");
const transactionMode = useConversationContext("transactionMode");
Expand Down Expand Up @@ -342,8 +365,11 @@ export function Chat() {
]);

const framesStore = useFramesStore().frames;
const showPlaceholder =
listArray.length === 0 || isBlockedPeer || !conversation;
const showPlaceholder = useIsShowingPlaceholder(
listArray,
isBlockedPeer,
conversation
);
const renderItem = useRenderItem(
xmtpAddress,
conversation,
Expand All @@ -352,17 +378,6 @@ export function Chat() {
);
const keyExtractor = useCallback((item: MessageToDisplay) => item.id, []);

// The first message was really buggy on iOS & Android and this is due to FlashList
// so we keep FlatList for new convos and switch to FlashList for bigger convos
// that need great perf.
const conversationNotPendingRef = useRef(
conversation && !conversation.pending
);
const AnimatedListView =
conversationNotPendingRef.current && Platform.OS !== "web"
? ReanimatedFlashList
: ReanimatedFlatList;

const messageListRef = useRef<
FlatList<MessageToDisplay> | FlashList<MessageToDisplay> | undefined
>();
Expand Down Expand Up @@ -391,8 +406,6 @@ export function Chat() {
};
}, [scrollToMessage]);

const itemType = getItemType(framesStore);

const handleOnLayout = useCallback(() => {
setTimeout(() => {
onReadyToFocus();
Expand Down Expand Up @@ -433,7 +446,7 @@ export function Chat() {
}
inverted
keyExtractor={keyExtractor}
getItemType={itemType}
getItemType={getItemType(framesStore)}
keyboardShouldPersistTaps="handled"
estimatedItemSize={80}
// Size glitch on Android
Expand Down Expand Up @@ -481,6 +494,7 @@ export function Chat() {
// Lightweight chat preview component used for longpress on chat
export function ChatPreview() {
const conversation = useConversationContext("conversation");
const AnimatedListView = useAnimatedListView(conversation);
const isBlockedPeer = useConversationContext("isBlockedPeer");
const onReadyToFocus = useConversationContext("onReadyToFocus");

Expand All @@ -506,8 +520,11 @@ export function ChatPreview() {
);

const framesStore = useFramesStore().frames;
const showPlaceholder =
listArray.length === 0 || isBlockedPeer || !conversation;
const showPlaceholder = useIsShowingPlaceholder(
listArray,
isBlockedPeer,
conversation
);
const renderItem = useRenderItem(
xmtpAddress,
conversation,
Expand All @@ -516,23 +533,10 @@ export function ChatPreview() {
);
const keyExtractor = useCallback((item: MessageToDisplay) => item.id, []);

// The first message was really buggy on iOS & Android and this is due to FlashList
// so we keep FlatList for new convos and switch to FlashList for bigger convos
// that need great perf.
const conversationNotPendingRef = useRef(
conversation && !conversation.pending
);
const AnimatedListView =
conversationNotPendingRef.current && Platform.OS !== "web"
? ReanimatedFlashList
: ReanimatedFlatList;

const messageListRef = useRef<
FlatList<MessageToDisplay> | FlashList<MessageToDisplay> | undefined
>();

const itemType = getItemType(framesStore);

const handleOnLayout = useCallback(() => {
setTimeout(() => {
onReadyToFocus();
Expand Down Expand Up @@ -566,7 +570,7 @@ export function ChatPreview() {
estimatedListSize={Dimensions.get("screen")}
inverted
keyExtractor={keyExtractor}
getItemType={itemType}
getItemType={getItemType(framesStore)}
keyboardShouldPersistTaps="handled"
estimatedItemSize={80}
showsVerticalScrollIndicator={false}
Expand Down

0 comments on commit 0744094

Please sign in to comment.