// ** React Imports import { useState, useEffect, ChangeEvent, ReactNode } from 'react' // ** Next Import import { useRouter } from 'next/router' // ** MUI Imports import Box from '@mui/material/Box' import List from '@mui/material/List' import Chip from '@mui/material/Chip' import Badge from '@mui/material/Badge' import Drawer from '@mui/material/Drawer' import MuiAvatar from '@mui/material/Avatar' import ListItem from '@mui/material/ListItem' import TextField from '@mui/material/TextField' import IconButton from '@mui/material/IconButton' import Typography from '@mui/material/Typography' import ListItemText from '@mui/material/ListItemText' import ListItemAvatar from '@mui/material/ListItemAvatar' import ListItemButton from '@mui/material/ListItemButton' import InputAdornment from '@mui/material/InputAdornment' // ** Third Party Components import PerfectScrollbar from 'react-perfect-scrollbar' // ** Icon Imports import Icon from 'src/@core/components/icon' // ** Types import { ContactType, ChatSidebarLeftType, ChatsArrType } from 'src/types/apps/chatTypes' // ** Custom Components Import import CustomAvatar from 'src/@core/components/mui/avatar' // ** Chat App Components Imports import UserProfileLeft from 'src/views/apps/chat/UserProfileLeft' const ScrollWrapper = ({ children, hidden }: { children: ReactNode; hidden: boolean }) => { if (hidden) { return {children} } else { return {children} } } const SidebarLeft = (props: ChatSidebarLeftType) => { // ** Props const { store, hidden, mdAbove, dispatch, statusObj, userStatus, selectChat, getInitials, sidebarWidth, setUserStatus, leftSidebarOpen, removeSelectedChat, userProfileLeftOpen, formatDateToMonthShort, handleLeftSidebarToggle, handleUserProfileLeftSidebarToggle } = props // ** States const [query, setQuery] = useState('') const [filteredChat, setFilteredChat] = useState([]) const [filteredContacts, setFilteredContacts] = useState([]) const [active, setActive] = useState(null) // ** Hooks const router = useRouter() const handleChatClick = (type: 'chat' | 'contact', id: number) => { dispatch(selectChat(id)) setActive({ type, id }) if (!mdAbove) { handleLeftSidebarToggle() } } useEffect(() => { if (store && store.chats) { if (active !== null) { if (active.type === 'contact' && active.id === store.chats[0].id) { setActive({ type: 'chat', id: active.id }) } } } }, [store, active]) useEffect(() => { router.events.on('routeChangeComplete', () => { setActive(null) dispatch(removeSelectedChat()) }) return () => { setActive(null) dispatch(removeSelectedChat()) } // eslint-disable-next-line react-hooks/exhaustive-deps }, []) const hasActiveId = (id: number | string) => { if (store.chats !== null) { const arr = store.chats.filter(i => i.id === id) return !!arr.length } } const renderChats = () => { if (store && store.chats && store.chats.length) { if (query.length && !filteredChat.length) { return ( No Chats Found ) } else { const arrToMap = query.length && filteredChat.length ? filteredChat : store.chats return arrToMap.map((chat: ChatsArrType, index: number) => { const { lastMessage } = chat.chat const activeCondition = active !== null && active.id === chat.id && active.type === 'chat' return ( handleChatClick('chat', chat.id)} sx={{ px: 2.5, py: 2.5, width: '100%', borderRadius: 1, alignItems: 'flex-start', ...(activeCondition && { backgroundColor: theme => `${theme.palette.primary.main} !important` }) }} > `0 0 0 2px ${ !activeCondition ? theme.palette.background.paper : theme.palette.common.white }` }} /> } > {chat.avatar ? ( `2px solid ${activeCondition ? theme.palette.common.white : 'transparent'}` }} /> ) : ( `2px solid ${activeCondition ? theme.palette.common.white : 'transparent'}` }} > {getInitials(chat.fullName)} )} {chat.fullName} } secondary={ {lastMessage ? lastMessage.message : null} } /> <>{lastMessage ? formatDateToMonthShort(lastMessage.time as string, true) : new Date()} {chat.chat.unseenMsgs && chat.chat.unseenMsgs > 0 ? ( ) : null} ) }) } } } const renderContacts = () => { if (store && store.chats && store.chats.length) { if (query.length && !filteredContacts.length) { return ( No Contacts Found ) } else { const arrToMap = query.length && filteredContacts.length ? filteredContacts : store.contacts return arrToMap !== null ? arrToMap.map((contact: ContactType, index: number) => { const activeCondition = active !== null && active.id === contact.id && active.type === 'contact' && !hasActiveId(contact.id) return ( handleChatClick(hasActiveId(contact.id) ? 'chat' : 'contact', contact.id)} sx={{ px: 2.5, py: 2.5, width: '100%', borderRadius: 1, ...(activeCondition && { backgroundColor: theme => `${theme.palette.primary.main} !important` }) }} > {contact.avatar ? ( `2px solid ${activeCondition ? theme.palette.common.white : 'transparent'}` }} /> ) : ( `2px solid ${activeCondition ? theme.palette.common.white : 'transparent'}` }} > {getInitials(contact.fullName)} )} {contact.fullName} } secondary={ {contact.about} } /> ) }) : null } } } const handleFilter = (e: ChangeEvent) => { setQuery(e.target.value) if (store.chats !== null && store.contacts !== null) { const searchFilterFunction = (contact: ChatsArrType | ContactType) => contact.fullName.toLowerCase().includes(e.target.value.toLowerCase()) const filteredChatsArr = store.chats.filter(searchFilterFunction) const filteredContactsArr = store.contacts.filter(searchFilterFunction) setFilteredChat(filteredChatsArr) setFilteredContacts(filteredContactsArr) } } return (
theme.shape.borderRadius, borderBottomLeftRadius: theme => theme.shape.borderRadius }, '& > .MuiBackdrop-root': { borderRadius: 1, position: 'absolute', zIndex: theme => theme.zIndex.drawer - 1 } }} > `1px solid ${theme.palette.divider}` }} > {store && store.userProfile ? ( `0 0 0 2px ${theme.palette.background.paper}` }} /> } > ) : null} ) }} /> {!mdAbove ? ( ) : null}
) } export default SidebarLeft