@plteam/chat-ui
Version:
CUI Kit is a free and open-source library for creating AI assistant chat interfaces, built with React, Material UI, and TypeScript
59 lines (58 loc) • 2.77 kB
JavaScript
import * as React from 'react';
import ThreadListMapBlockAllStyled from '../../leftContainer/listMap/ThreadListMapBlockAllStyled';
import TimeTextWrapper from '../../leftContainer/TimeTextWrapper';
import IconButton from '@mui/material/IconButton';
import Button from '@mui/material/Button';
import ListItemText from '@mui/material/ListItemText';
import MdMenuItem from '../../../ui/menu/MdMenuItem';
import Drawer from '@mui/material/Drawer';
import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import ContainerSubtitle from '../../../ui/ContainerSubtitle';
import { CHAT_LOCALE } from '../../../locale/enEN';
import { ruRU } from '../../../locale/ruRU';
import { useObserverValue } from '../../../views/hooks/useObserverValue';
import internalApi from './internalApi';
const useSlots = (slots) => {
const componentSlots = React.useMemo(() => ({
baseButton: slots?.baseButton ?? Button,
baseIconButton: slots?.baseIconButton ?? IconButton,
baseListItemText: slots?.baseListItemText ?? ListItemText,
baseMenuItem: slots?.baseMenuItem ?? MdMenuItem,
historyContainer: slots?.historyContainer ?? Box,
historyWrapper: slots?.historyWrapper ?? Stack,
threadsList: slots?.threadsList ?? ThreadListMapBlockAllStyled,
threadListItemMenuButton: slots?.threadListItemMenuButton ?? IconButton,
listDrawer: slots?.listDrawer ?? Drawer,
listSubtitle: slots?.listSubtitle ?? ContainerSubtitle,
listTimeText: slots?.listTimeText ?? Typography,
listTimeTextWrapper: slots?.listTimeTextWrapper ?? TimeTextWrapper,
listDrawerTitle: slots?.listDrawerTitle ?? Typography,
}), [slots]);
return componentSlots;
};
const Context = React.createContext(undefined);
export const HistoryProvider = ({ children, ...props }) => {
const { apiRef, loading, threadActions, slotProps } = props;
const userLocale = props?.lang === 'ru' ? ruRU : CHAT_LOCALE;
const userSlots = useSlots(props?.slots);
const internal = useObserverValue(internalApi);
const value = React.useMemo(() => ({
internal,
apiRef,
loading: !!loading,
threadActions: threadActions || [],
slots: userSlots,
locale: userLocale,
slotProps: slotProps || {},
}), [apiRef, internal, loading, props]);
return (React.createElement(Context.Provider, { value: value }, children));
};
export const useHistoryContext = () => {
const context = React.useContext(Context);
if (!context) {
throw new Error("useHistoryContext must be used within a HistoryProvider");
}
return context;
};