Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 8x 8x 21x 21x 5x 5x 5x 5x 5x 5x 21x 8x 8x 8x 1x 1x 21x 5x 5x 5x 21x 8x 8x 21x 7x 7x 21x 21x 7x 14x 7x 4x 4x 12x | import { Stack, Box } from "@mui/material";
import { DateOrNew, MessageBox } from "./Giveconversation.atoms";
import {
ConversationContextTypes,
GiveConversationTabType,
MessagesChatTypes,
} from "../types";
import { useInView } from "react-intersection-observer";
import GiveButton from "@shared/Button/GiveButton";
import { useEffect, useRef, useState } from "react";
import { ArrowDownIcon } from "@phosphor-icons/react";
import { useConversationContext } from "../ConversationProvider";
import { useAppTheme } from "@theme/v2/Provider";
import GiveEmptyStateWrapper from "@shared/EmptyState/GiveEmptyStateWrapper";
import ChatConversationsSkeleton from "./ChatConversationsSkeleton";
import { checkoutScrollbarStyles } from "@sections/PayBuilder/Checkout/helpers";
interface DefaultContextProps
extends Pick<
ConversationContextTypes,
| "searchState"
| "isBackgroundFetchingMessages"
| "showMentions"
| "currentThread"
| "messages"
| "fetchNextPageMessages"
| "isFetchingNextPageMessages"
| "hasNextPageMessages"
| "isThreadMessagesLoading"
> {
tab?: GiveConversationTabType;
}
function MessageThread({
defaultContext,
}: {
defaultContext?: DefaultContextProps;
}) {
const context = useConversationContext();
const {
searchState,
isBackgroundFetchingMessages,
showMentions,
currentThread,
tab,
messages,
fetchNextPageMessages,
isFetchingNextPageMessages,
hasNextPageMessages,
isThreadMessagesLoading,
} = defaultContext || context;
const hasMentionsSwitch = tab === "team" && !currentThread?.id;
const theme = useAppTheme();
const scrollContainerRef = useRef<HTMLDivElement | null>(null);
const scrollTargetRef = useRef<HTMLDivElement | null>(null);
const hasInitiallyScrolled = useRef(false);
const [userHasScrolled, setUserHasScrolled] = useState(false);
// Observe bottom to show "Jump to Latest"
const { ref: inViewRef, inView } = useInView();
// Observe top within scrollable container
const { ref: topRef, inView: inViewFetchMore } = useInView({
root: scrollContainerRef.current,
threshold: 0,
});
const setBottomRef = (node: HTMLDivElement | null) => {
scrollTargetRef.current = node;
inViewRef(node);
};
const handleScrollToLatest = () => {
scrollContainerRef.current?.scrollTo({
top: scrollContainerRef.current.scrollHeight,
behavior: "smooth",
});
};
/* Add scroll event listener to detect user scrolling.
We need to detect if the user has scrolled to the bottom of the page to avoid showing the "Jump to Latest" button when user sends a message without scrolling.
*/
useEffect(() => {
const scrollContainer = scrollContainerRef.current;
Iif (!scrollContainer) return;
const handleScroll = () => {
const { scrollTop, scrollHeight, clientHeight } = scrollContainer;
const isAtBottom = scrollTop + clientHeight >= scrollHeight - 10; // 10px threshold
// User has scrolled if they're not at the bottom
setUserHasScrolled(!isAtBottom);
};
scrollContainer.addEventListener("scroll", handleScroll);
return () => {
scrollContainer.removeEventListener("scroll", handleScroll);
};
}, []);
// Scroll to bottom only once on initial load
useEffect(() => {
const scrollContainer = scrollContainerRef.current;
const hasMessages = Array.isArray(messages) && messages.length > 0;
// 1. Scroll once on initial load
if (hasMessages && scrollContainer && !hasInitiallyScrolled.current) {
scrollContainer.scrollTop = scrollContainer.scrollHeight;
hasInitiallyScrolled.current = true;
}
}, [messages]);
// Scroll to bottom only once on initial load
useEffect(() => {
const scrollContainer = scrollContainerRef.current;
Eif (scrollContainer) {
scrollContainer.scrollTop = scrollContainer.scrollHeight;
}
}, [isBackgroundFetchingMessages]);
// Fetch more when top is in view
useEffect(() => {
const container = scrollContainerRef.current;
Eif (!container || !inViewFetchMore || !hasNextPageMessages) return;
const previousHeight = container.scrollHeight;
Promise.resolve(fetchNextPageMessages?.()).then(() => {
requestAnimationFrame(() => {
const newHeight = container.scrollHeight;
const delta = newHeight - previousHeight;
container.scrollTop = container.scrollTop + delta;
});
});
}, [inViewFetchMore, hasNextPageMessages]);
const getEmptySection = () => {
Iif (hasMentionsSwitch && showMentions) return "no-mentions";
return tab === "team"
? "no-team-conversations"
: "no-merchant-conversations";
};
const isFetching = isFetchingNextPageMessages || isThreadMessagesLoading;
if (!messages?.length && !isFetching) {
return (
<Stack
data-testid="give-conversation-message-empty-state"
height="100%"
justifyContent="center"
alignItems="center"
>
<GiveEmptyStateWrapper section={getEmptySection()} isEmpty />
</Stack>
);
}
return (
<Stack
bgcolor={theme?.palette?.surface?.primary}
position="relative"
width="100%"
height="100%"
pt="8px"
overflow="hidden"
>
<Box
ref={scrollContainerRef}
height="100%"
overflow="auto"
p="12px 20px"
position="relative"
sx={checkoutScrollbarStyles}
>
{!isFetching && <div ref={topRef} />}
{isFetching && <ChatConversationsSkeleton />}
{(messages || [])?.map((group, idx) => (
<MessageGroup
key={group.date + idx}
{...group}
searchWord={searchState.searchValue}
/>
))}
{!isFetching && <div ref={setBottomRef} />}
</Box>
{!inView && !isFetching && userHasScrolled && (
<Box
position="absolute"
bottom={15}
display="flex"
justifyContent="center"
width="100%"
>
<GiveButton
endIcon={<ArrowDownIcon />}
label="Jump to Latest"
sx={{ borderRadius: "8px" }}
onClick={handleScrollToLatest}
/>
</Box>
)}
</Stack>
);
}
export default MessageThread;
function MessageGroup({
date,
messages,
searchWord,
}: {
date?: number;
isRead?: boolean;
messages?: MessagesChatTypes[];
searchWord?: string;
}) {
const theme = useAppTheme();
return (
<Box
data-testid="give-conversation-message-group"
bgcolor={theme?.palette?.surface?.primary}
pb="20px"
>
<DateOrNew date={date} />
{messages?.map((data) => (
<MessageBox searchWord={searchWord} {...data} key={data?.id} />
))}
</Box>
);
}
|