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

refactor: Bubble.List re-render #479

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
10 changes: 8 additions & 2 deletions components/bubble/Bubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,16 @@ const Bubble: React.ForwardRefRenderFunction<BubbleRef, BubbleProps> = (props, r
);

// ============================ Avatar ============================
const avatarNode = React.isValidElement(avatar) ? avatar : <Avatar {...avatar} />;
const avatarNode = React.useMemo(
() => (React.isValidElement(avatar) ? avatar : <Avatar {...avatar} />),
[avatar],
);

// =========================== Content ============================
const mergedContent = messageRender ? messageRender(typedContent as any) : typedContent;
const mergedContent = React.useMemo(
() => (messageRender ? messageRender(typedContent as any) : typedContent),
[typedContent, messageRender],
);

// ============================ Render ============================
let contentNode: React.ReactNode;
Expand Down
17 changes: 5 additions & 12 deletions components/bubble/BubbleList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as React from 'react';
import { useXProviderContext } from '../x-provider';
import Bubble, { BubbleContext } from './Bubble';
import type { BubbleRef } from './Bubble';
import useDisplayData from './hooks/useDisplayData';
import useListData from './hooks/useListData';
import type { BubbleProps } from './interface';
import useStyle from './style';
Expand Down Expand Up @@ -80,8 +79,6 @@ const BubbleList: React.ForwardRefRenderFunction<BubbleListRef, BubbleListProps>
// ============================= Data =============================
const mergedData = useListData(items, roles);

const [displayData, onTypingComplete] = useDisplayData(mergedData);

// ============================ Scroll ============================
// Is current scrollTop at the end. User scroll will make this false.
const [scrollReachEnd, setScrollReachEnd] = React.useState(true);
Expand All @@ -108,7 +105,7 @@ const BubbleList: React.ForwardRefRenderFunction<BubbleListRef, BubbleListProps>
React.useEffect(() => {
if (autoScroll) {
// New date come, the origin last one is the second last one
const lastItemKey = displayData[displayData.length - 2]?.key;
const lastItemKey = mergedData[mergedData.length - 2]?.key;
const bubbleInst = bubbleRefs.current[lastItemKey!];

// Auto scroll if last 2 item is visible
Expand All @@ -124,7 +121,7 @@ const BubbleList: React.ForwardRefRenderFunction<BubbleListRef, BubbleListProps>
}
}
}
}, [displayData.length]);
}, [mergedData.length]);

// ========================== Outer Ref ===========================
React.useImperativeHandle(ref, () => ({
Expand All @@ -142,8 +139,8 @@ const BubbleList: React.ForwardRefRenderFunction<BubbleListRef, BubbleListProps>

if (bubbleInst) {
// Block current auto scrolling
const index = displayData.findIndex((dataItem) => dataItem.key === key);
setScrollReachEnd(index === displayData.length - 1);
const index = mergedData.findIndex((dataItem) => dataItem.key === key);
setScrollReachEnd(index === mergedData.length - 1);

// Do native scroll
bubbleInst.nativeElement.scrollIntoView({
Expand Down Expand Up @@ -181,7 +178,7 @@ const BubbleList: React.ForwardRefRenderFunction<BubbleListRef, BubbleListProps>
ref={listRef}
onScroll={onInternalScroll}
>
{displayData.map(({ key, ...bubble }) => (
{mergedData.map(({ key, ...bubble }) => (
<Bubble
{...bubble}
key={key}
Expand All @@ -193,10 +190,6 @@ const BubbleList: React.ForwardRefRenderFunction<BubbleListRef, BubbleListProps>
}
}}
typing={initialized ? bubble.typing : false}
onTypingComplete={() => {
bubble.onTypingComplete?.();
onTypingComplete(key);
}}
/>
))}
</div>
Expand Down
42 changes: 0 additions & 42 deletions components/bubble/hooks/useDisplayData.ts

This file was deleted.

Loading