buster/web/src/layouts/ChatLayout/ChatContext/useAutoChangeLayout.ts

29 lines
1001 B
TypeScript
Raw Normal View History

2025-03-05 04:52:33 +08:00
'use client';
2025-02-18 07:25:31 +08:00
import { useMessageIndividual } from '@/context/Chats';
2025-02-11 11:15:32 +08:00
import type { SelectedFile } from '../interfaces';
2025-03-05 04:52:33 +08:00
import { useEffect, useRef } from 'react';
2025-02-09 13:41:08 +08:00
export const useAutoChangeLayout = ({
lastMessageId,
onSetSelectedFile
}: {
lastMessageId: string;
onSetSelectedFile: (file: SelectedFile) => void;
}) => {
2025-03-05 04:52:33 +08:00
const hasSeeningReasoningPage = useRef(false); //used when there is a delay in page load
2025-02-18 07:25:31 +08:00
const message = useMessageIndividual(lastMessageId);
2025-02-09 13:41:08 +08:00
const reasoningMessagesLength = message?.reasoning?.length;
const isCompletedStream = message?.isCompletedStream;
2025-02-11 11:15:32 +08:00
const isLoading = !isCompletedStream;
2025-02-09 13:41:08 +08:00
const hasReasoning = !!reasoningMessagesLength;
2025-03-05 04:11:49 +08:00
2025-02-12 07:46:22 +08:00
//change the page to reasoning file if we get a reasoning message
2025-02-09 13:41:08 +08:00
useEffect(() => {
2025-03-05 04:52:33 +08:00
if (isLoading && !hasSeeningReasoningPage.current && hasReasoning) {
hasSeeningReasoningPage.current = true;
2025-02-09 13:41:08 +08:00
onSetSelectedFile({ id: lastMessageId, type: 'reasoning' });
}
2025-03-05 04:52:33 +08:00
}, [isLoading, hasReasoning]);
2025-02-09 13:41:08 +08:00
};