index.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import List from '@mui/material/List';
  2. import ListItemButton from '@mui/material/ListItemButton';
  3. import Avatar from '@mui/material/Avatar';
  4. import ListItemText from '@mui/material/ListItemText';
  5. import ListItemIcon from '@mui/material/ListItemIcon';
  6. import { makeStyles } from '@material-ui/core'
  7. import { useState } from 'react';
  8. import shortid from 'shortid';
  9. import { contacts } from './contacts'
  10. const useStyles = makeStyles({
  11. list: {
  12. width: '100%',
  13. maxHeight: '890px',
  14. overflowY: 'scroll',
  15. '&::-webkit-scrollbar': {
  16. width: '0.4em'
  17. },
  18. '&::-webkit-scrollbar-track': {
  19. boxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  20. webkitBoxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
  21. backgroundColor: '#eceeec',
  22. },
  23. '&::-webkit-scrollbar-thumb': {
  24. backgroundColor: '#ccc8c8',
  25. },
  26. "&::-webkit-scrollbar-thumb:focus": {
  27. backgroundColor: "#959595",
  28. },
  29. "&::-webkit-scrollbar-thumb:active": {
  30. backgroundColor: "#959595",
  31. },
  32. },
  33. listItem_iconAvatar: {
  34. marginRight:10
  35. },
  36. })
  37. const ChatsList = () => {
  38. const [selectedIndex, setSelectedIndex] = useState<number>(1);
  39. const classes = useStyles()
  40. const handleListItemClick = (e: React.SyntheticEvent<Element, Event>, i: number) => {
  41. console.log(i,'index','selected chat in chat bar',e)
  42. setSelectedIndex(i);
  43. }
  44. const data = new Date(2022, 3, 8).toLocaleString("en-US", {
  45. year:'numeric',
  46. month: 'short',
  47. day: 'numeric'
  48. });
  49. const firstLetter = (word: string) => word.slice(0, 1).toUpperCase()
  50. const slicedWord = (word: string, max: number, from: number = 0) =>
  51. word.length < max ? word.slice(from) : word.slice(from, max)
  52. return (
  53. <List
  54. className={classes.list} component="nav"
  55. aria-label="main mailbox folders">
  56. {contacts && contacts.map(({name,lastName,avatarUrl}: any,i:number) =>
  57. <ListItemButton
  58. key={shortid.generate()}
  59. selected={selectedIndex === i}
  60. onClick={(e) => handleListItemClick(e, i)}
  61. >
  62. <ListItemIcon className={classes.listItem_iconAvatar}>
  63. <Avatar alt={name} src={avatarUrl?avatarUrl:undefined}
  64. sx={{ background: '#f0c712', width: 54, height: 54 }}>
  65. {!avatarUrl&&`${firstLetter(name)}${firstLetter(lastName)}`}
  66. </Avatar>
  67. </ListItemIcon>
  68. <ListItemText primary={`${firstLetter(name)}${slicedWord(name, 15, 1)}
  69. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`} secondary={`last seen ${data}`} />
  70. </ListItemButton>)}
  71. </List>
  72. );
  73. }
  74. export default ChatsList