Skip to content

Commit

Permalink
fully functional once again
Browse files Browse the repository at this point in the history
  • Loading branch information
pablonyx committed Sep 9, 2024
1 parent ba8f6fc commit 51d1714
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions web/src/app/chat/ChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,15 @@ export function ChatPage({
}
return;
}
const shouldScrollToBottom =
visibleRange.get(existingChatSessionId) === undefined ||
visibleRange.get(existingChatSessionId)?.end == 0;
if (shouldScrollToBottom) {
hasBeenInitialized.current = false;
}
console.log("this i the current most visible range");
console.log(shouldScrollToBottom);
console.log(visibleRange.get(existingChatSessionId));

clearSelectedDocuments();
setIsFetchingChatMessages(true);
Expand Down Expand Up @@ -387,10 +396,13 @@ export function ChatPage({

// go to bottom. If initial load, then do a scroll,
// otherwise just appear at the bottom
if (!hasPerformedInitialScroll) {
clientScrollToBottom();
} else if (isChatSessionSwitch) {
clientScrollToBottom(true);
if (shouldScrollToBottom) {
initializeVisibleRange();
if (!hasPerformedInitialScroll) {
clientScrollToBottom();
} else if (isChatSessionSwitch) {
clientScrollToBottom(true);
}
}
setIsFetchingChatMessages(false);

Expand Down Expand Up @@ -537,17 +549,6 @@ export function ChatPage({
new Map([[chatSessionIdRef.current, "input"]])
);

const [scrollHeight, setScrollHeight] = useState<Map<number | null, number>>(
new Map([[chatSessionIdRef.current, 0]])
);
const currentScrollHeight = () => {
return scrollHeight.get(currentSessionId());
};

const retrieveCurrentScrollHeight = (): number | null => {
return scrollHeight.get(currentSessionId()) || null;
};

const [regenerationState, setRegenerationState] = useState<
Map<number | null, RegenerationState | null>
>(new Map([[null, null]]));
Expand Down Expand Up @@ -780,6 +781,7 @@ export function ChatPage({
}
}, 100);
};

const clientScrollToBottom = (fast?: boolean) => {
setTimeout(() => {
if (!endDivRef.current || !scrollableDivRef.current) {
Expand Down Expand Up @@ -1547,7 +1549,6 @@ export function ChatPage({
});

const hasBeenInitialized = useRef(false);
// const [visibleRange, setVisibleRange] = useState({ start: 0, end: 40, mostVisibleMessageId: null as number | null });
const [visibleRange, setVisibleRange] = useState<
Map<
number | null,
Expand All @@ -1574,31 +1575,35 @@ export function ChatPage({
end: number;
mostVisibleMessageId: number | null;
}) => {
console.log("newRange", newRange);
setVisibleRange((prevState) => {
const newState = new Map(prevState);
newState.set(currentSessionId(), newRange);
return newState;
});
};

useEffect(() => {
const initializeVisibleRange = () => {
if (
messageHistory.length > 0 &&
(!visibleRange.get(currentSessionId()) ||
visibleRange.get(currentSessionId())?.end == 0)
) {
const newEnd = Math.max(messageHistory.length, 40);
const newStart = Math.max(0, newEnd - 40);
const newMostVisibleMessageId = messageHistory[newEnd - 1].messageId;
const newStart = Math.max(0, newEnd - 20);
const newMostVisibleMessageId = messageHistory[newEnd - 1]?.messageId;

updateVisibleRangeForCurrentSession({
start: newStart,
end: newEnd,
mostVisibleMessageId: newMostVisibleMessageId,
});
hasBeenInitialized.current = true;
} else {
}
};

useEffect(() => {
initializeVisibleRange();
}, [router, messageHistory, currentSessionId()]);

const currentVisibleRange = visibleRange.get(currentSessionId()) || {
Expand All @@ -1607,7 +1612,7 @@ export function ChatPage({
mostVisibleMessageId: null,
};

const updateVisibleRange = () => {
const updateVisibleRangeBasedOnScroll = () => {
if (!hasBeenInitialized.current) return;
const scrollableDiv = scrollableDivRef.current;
if (!scrollableDiv) return;
Expand All @@ -1629,7 +1634,7 @@ export function ChatPage({
});

if (mostVisibleMessageIndex !== -1) {
const bufferCount = 15;
const bufferCount = 24;
const startIndex = Math.max(0, mostVisibleMessageIndex - bufferCount);
const endIndex = Math.min(
messageHistory.length,
Expand All @@ -1649,7 +1654,7 @@ export function ChatPage({

if (scrollableDiv) {
const handleScroll = () => {
updateVisibleRange();
updateVisibleRangeBasedOnScroll();
};

scrollableDiv.addEventListener("scroll", handleScroll);
Expand Down Expand Up @@ -1954,12 +1959,12 @@ export function ChatPage({
currentVisibleRange.start,
currentVisibleRange.end
)
).map((message, i) => {
const actualIndex = i + currentVisibleRange.start;
).map((message, fauxIndex) => {
const i = fauxIndex + currentVisibleRange.start;
const messageMap = currentMessageMap(
completeMessageDetail
);
const messageReactComponentKey = `${actualIndex}-${currentSessionId()}`;
const messageReactComponentKey = `${i}-${currentSessionId()}`;
const parentMessage = message.parentMessageId
? messageMap.get(message.parentMessageId)
: null;
Expand Down

0 comments on commit 51d1714

Please sign in to comment.