2025-01-28 07:23:08 +08:00
|
|
|
import React, { PropsWithChildren } from 'react';
|
|
|
|
import {
|
|
|
|
ContextSelector,
|
|
|
|
createContext,
|
|
|
|
useContextSelector
|
|
|
|
} from '@fluentui/react-context-selector';
|
|
|
|
import { useBusterChatIndividual } from '@/context/Chats';
|
|
|
|
import type { SelectedFile } from '../interfaces';
|
|
|
|
|
|
|
|
export const useChatContext = ({
|
|
|
|
chatId,
|
|
|
|
defaultSelectedFile
|
|
|
|
}: {
|
|
|
|
chatId?: string;
|
|
|
|
defaultSelectedFile?: SelectedFile;
|
|
|
|
}) => {
|
|
|
|
const selectedFileId = defaultSelectedFile?.id;
|
|
|
|
const selectedFileType = defaultSelectedFile?.type;
|
2025-02-05 03:26:42 +08:00
|
|
|
const metricId = defaultSelectedFile?.type === 'metric' ? defaultSelectedFile?.id : undefined;
|
2025-01-28 07:23:08 +08:00
|
|
|
|
|
|
|
//CHAT
|
|
|
|
const { chat } = useBusterChatIndividual({
|
2025-02-05 03:26:42 +08:00
|
|
|
chatId,
|
|
|
|
metricId
|
2025-01-28 07:23:08 +08:00
|
|
|
});
|
2025-02-05 13:04:26 +08:00
|
|
|
const hasChat = !!chatId && !!chat;
|
2025-01-28 07:23:08 +08:00
|
|
|
const chatTitle = chat?.title;
|
2025-01-28 08:08:52 +08:00
|
|
|
const chatMessages = chat?.messages ?? [];
|
2025-01-28 07:23:08 +08:00
|
|
|
|
|
|
|
//FILE
|
|
|
|
const hasFile = !!defaultSelectedFile?.id;
|
|
|
|
|
2025-02-05 13:04:26 +08:00
|
|
|
//MESSAGES
|
|
|
|
const currentMessageId = chatMessages[chatMessages.length - 1]?.id;
|
|
|
|
|
2025-01-28 07:23:08 +08:00
|
|
|
return {
|
2025-02-05 13:04:26 +08:00
|
|
|
hasChat,
|
2025-01-28 07:23:08 +08:00
|
|
|
hasFile,
|
|
|
|
selectedFileId,
|
2025-02-05 13:04:26 +08:00
|
|
|
currentMessageId,
|
2025-01-28 07:23:08 +08:00
|
|
|
chatTitle,
|
2025-01-28 08:08:52 +08:00
|
|
|
selectedFileType,
|
2025-02-05 13:04:26 +08:00
|
|
|
chatMessages,
|
|
|
|
chatId
|
2025-01-28 07:23:08 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ChatContext = createContext<ReturnType<typeof useChatContext>>(
|
|
|
|
{} as ReturnType<typeof useChatContext>
|
|
|
|
);
|
|
|
|
|
|
|
|
export const ChatContextProvider = React.memo(
|
|
|
|
({ value, children }: PropsWithChildren<{ value: ReturnType<typeof useChatContext> }>) => {
|
|
|
|
return <ChatContext.Provider value={value}>{children}</ChatContext.Provider>;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
ChatContextProvider.displayName = 'ChatContextProvider';
|
|
|
|
|
|
|
|
export const useChatContextSelector = <T,>(
|
|
|
|
selector: ContextSelector<ReturnType<typeof useChatContext>, T>
|
|
|
|
) => useContextSelector(ChatContext, selector);
|