Spaces:
Configuration error
Configuration error
| import { useState, useRef, useEffect } from 'react' | |
| import { useDispatch } from 'react-redux' | |
| import PropTypes from 'prop-types' | |
| import { ClickAwayListener, Paper, Popper, Button } from '@mui/material' | |
| import { useTheme } from '@mui/material/styles' | |
| import { IconMessage, IconX, IconEraser, IconArrowsMaximize } from '@tabler/icons' | |
| // project import | |
| import { StyledFab } from 'ui-component/button/StyledFab' | |
| import MainCard from 'ui-component/cards/MainCard' | |
| import Transitions from 'ui-component/extended/Transitions' | |
| import { ChatMessage } from './ChatMessage' | |
| import ChatExpandDialog from './ChatExpandDialog' | |
| // api | |
| import chatmessageApi from 'api/chatmessage' | |
| // Hooks | |
| import useConfirm from 'hooks/useConfirm' | |
| import useNotifier from 'utils/useNotifier' | |
| // Const | |
| import { enqueueSnackbar as enqueueSnackbarAction, closeSnackbar as closeSnackbarAction } from 'store/actions' | |
| export const ChatPopUp = ({ chatflowid }) => { | |
| const theme = useTheme() | |
| const { confirm } = useConfirm() | |
| const dispatch = useDispatch() | |
| useNotifier() | |
| const enqueueSnackbar = (...args) => dispatch(enqueueSnackbarAction(...args)) | |
| const closeSnackbar = (...args) => dispatch(closeSnackbarAction(...args)) | |
| const [open, setOpen] = useState(false) | |
| const [showExpandDialog, setShowExpandDialog] = useState(false) | |
| const [expandDialogProps, setExpandDialogProps] = useState({}) | |
| const anchorRef = useRef(null) | |
| const prevOpen = useRef(open) | |
| const handleClose = (event) => { | |
| if (anchorRef.current && anchorRef.current.contains(event.target)) { | |
| return | |
| } | |
| setOpen(false) | |
| } | |
| const handleToggle = () => { | |
| setOpen((prevOpen) => !prevOpen) | |
| } | |
| const expandChat = () => { | |
| const props = { | |
| open: true, | |
| chatflowid: chatflowid | |
| } | |
| setExpandDialogProps(props) | |
| setShowExpandDialog(true) | |
| } | |
| const resetChatDialog = () => { | |
| const props = { | |
| ...expandDialogProps, | |
| open: false | |
| } | |
| setExpandDialogProps(props) | |
| setTimeout(() => { | |
| const resetProps = { | |
| ...expandDialogProps, | |
| open: true | |
| } | |
| setExpandDialogProps(resetProps) | |
| }, 500) | |
| } | |
| const clearChat = async () => { | |
| const confirmPayload = { | |
| title: `Clear Chat History`, | |
| description: `Are you sure you want to clear all chat history?`, | |
| confirmButtonName: 'Clear', | |
| cancelButtonName: 'Cancel' | |
| } | |
| const isConfirmed = await confirm(confirmPayload) | |
| if (isConfirmed) { | |
| try { | |
| await chatmessageApi.deleteChatmessage(chatflowid) | |
| resetChatDialog() | |
| enqueueSnackbar({ | |
| message: 'Succesfully cleared all chat history', | |
| options: { | |
| key: new Date().getTime() + Math.random(), | |
| variant: 'success', | |
| action: (key) => ( | |
| <Button style={{ color: 'white' }} onClick={() => closeSnackbar(key)}> | |
| <IconX /> | |
| </Button> | |
| ) | |
| } | |
| }) | |
| } catch (error) { | |
| const errorData = error.response.data || `${error.response.status}: ${error.response.statusText}` | |
| enqueueSnackbar({ | |
| message: errorData, | |
| options: { | |
| key: new Date().getTime() + Math.random(), | |
| variant: 'error', | |
| persist: true, | |
| action: (key) => ( | |
| <Button style={{ color: 'white' }} onClick={() => closeSnackbar(key)}> | |
| <IconX /> | |
| </Button> | |
| ) | |
| } | |
| }) | |
| } | |
| } | |
| } | |
| useEffect(() => { | |
| if (prevOpen.current === true && open === false) { | |
| anchorRef.current.focus() | |
| } | |
| prevOpen.current = open | |
| // eslint-disable-next-line react-hooks/exhaustive-deps | |
| }, [open, chatflowid]) | |
| return ( | |
| <> | |
| <StyledFab | |
| sx={{ position: 'absolute', right: 20, top: 20 }} | |
| ref={anchorRef} | |
| size='small' | |
| color='secondary' | |
| aria-label='chat' | |
| title='Chat' | |
| onClick={handleToggle} | |
| > | |
| {open ? <IconX /> : <IconMessage />} | |
| </StyledFab> | |
| {open && ( | |
| <StyledFab | |
| sx={{ position: 'absolute', right: 80, top: 20 }} | |
| onClick={clearChat} | |
| size='small' | |
| color='error' | |
| aria-label='clear' | |
| title='Clear Chat History' | |
| > | |
| <IconEraser /> | |
| </StyledFab> | |
| )} | |
| {open && ( | |
| <StyledFab | |
| sx={{ position: 'absolute', right: 140, top: 20 }} | |
| onClick={expandChat} | |
| size='small' | |
| color='primary' | |
| aria-label='expand' | |
| title='Expand Chat' | |
| > | |
| <IconArrowsMaximize /> | |
| </StyledFab> | |
| )} | |
| <Popper | |
| placement='bottom-end' | |
| open={open} | |
| anchorEl={anchorRef.current} | |
| role={undefined} | |
| transition | |
| disablePortal | |
| popperOptions={{ | |
| modifiers: [ | |
| { | |
| name: 'offset', | |
| options: { | |
| offset: [40, 14] | |
| } | |
| } | |
| ] | |
| }} | |
| sx={{ zIndex: 1000 }} | |
| > | |
| {({ TransitionProps }) => ( | |
| <Transitions in={open} {...TransitionProps}> | |
| <Paper> | |
| <ClickAwayListener onClickAway={handleClose}> | |
| <MainCard border={false} elevation={16} content={false} boxShadow shadow={theme.shadows[16]}> | |
| <ChatMessage chatflowid={chatflowid} open={open} /> | |
| </MainCard> | |
| </ClickAwayListener> | |
| </Paper> | |
| </Transitions> | |
| )} | |
| </Popper> | |
| <ChatExpandDialog | |
| show={showExpandDialog} | |
| dialogProps={expandDialogProps} | |
| onClear={clearChat} | |
| onCancel={() => setShowExpandDialog(false)} | |
| ></ChatExpandDialog> | |
| </> | |
| ) | |
| } | |
| ChatPopUp.propTypes = { chatflowid: PropTypes.string } | |