buster/web/src/app/app/_layouts/ChatLayout/ChatContext/ChatContext.tsx

63 lines
1.6 KiB
TypeScript
Raw Normal View History

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;
const metricId = defaultSelectedFile?.type === 'metric' ? defaultSelectedFile?.id : undefined;
//CHAT
const { chat } = useBusterChatIndividual({
chatId,
metricId
});
2025-02-05 13:04:26 +08:00
const hasChat = !!chatId && !!chat;
const chatTitle = chat?.title;
2025-01-28 08:08:52 +08:00
const chatMessages = chat?.messages ?? [];
//FILE
const hasFile = !!defaultSelectedFile?.id;
2025-02-05 13:04:26 +08:00
//MESSAGES
const currentMessageId = chatMessages[chatMessages.length - 1]?.id;
return {
2025-02-05 13:04:26 +08:00
hasChat,
hasFile,
selectedFileId,
2025-02-05 13:04:26 +08:00
currentMessageId,
chatTitle,
2025-01-28 08:08:52 +08:00
selectedFileType,
2025-02-05 13:04:26 +08:00
chatMessages,
chatId
};
};
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);