index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { useState } from 'react'
  2. import { Fragment } from 'react/jsx-runtime'
  3. import { format } from 'date-fns'
  4. import {
  5. IconArrowLeft,
  6. IconDotsVertical,
  7. IconEdit,
  8. IconMessages,
  9. IconPaperclip,
  10. IconPhone,
  11. IconPhotoPlus,
  12. IconPlus,
  13. IconSearch,
  14. IconSend,
  15. IconVideo,
  16. } from '@tabler/icons-react'
  17. import { cn } from '@/lib/utils'
  18. import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'
  19. import { ScrollArea } from '@/components/ui/scroll-area'
  20. import { Separator } from '@/components/ui/separator'
  21. import { Button } from '@/components/button'
  22. import { Header } from '@/components/layout/header'
  23. import { Main } from '@/components/layout/main'
  24. import { ProfileDropdown } from '@/components/profile-dropdown'
  25. import { Search } from '@/components/search'
  26. import { ThemeSwitch } from '@/components/theme-switch'
  27. // Fake Data
  28. import { conversations } from './data/convo.json'
  29. type ChatUser = (typeof conversations)[number]
  30. type Convo = ChatUser['messages'][number]
  31. export default function Chats() {
  32. const [search, setSearch] = useState('')
  33. const [selectedUser, setSelectedUser] = useState<ChatUser>(conversations[0])
  34. const [mobileSelectedUser, setMobileSelectedUser] = useState<ChatUser | null>(
  35. null
  36. )
  37. // Filtered data based on the search query
  38. const filteredChatList = conversations.filter(({ fullName }) =>
  39. fullName.toLowerCase().includes(search.trim().toLowerCase())
  40. )
  41. const currentMessage = selectedUser.messages.reduce(
  42. (acc: Record<string, Convo[]>, obj) => {
  43. const key = format(obj.timestamp, 'd MMM, yyyy')
  44. // Create an array for the category if it doesn't exist
  45. if (!acc[key]) {
  46. acc[key] = []
  47. }
  48. // Push the current object to the array
  49. acc[key].push(obj)
  50. return acc
  51. },
  52. {}
  53. )
  54. return (
  55. <>
  56. {/* ===== Top Heading ===== */}
  57. <Header>
  58. <Search />
  59. <div className='ml-auto flex items-center space-x-4'>
  60. <ThemeSwitch />
  61. <ProfileDropdown />
  62. </div>
  63. </Header>
  64. <Main fixed>
  65. <section className='flex h-full gap-6'>
  66. {/* Left Side */}
  67. <div className='flex w-full flex-col gap-2 sm:w-56 lg:w-72 2xl:w-80'>
  68. <div className='sticky top-0 z-10 -mx-4 bg-background px-4 pb-3 shadow-md sm:static sm:z-auto sm:mx-0 sm:p-0 sm:shadow-none'>
  69. <div className='flex items-center justify-between py-2'>
  70. <div className='flex gap-2'>
  71. <h1 className='text-2xl font-bold'>Inbox</h1>
  72. <IconMessages size={20} />
  73. </div>
  74. <Button size='icon' variant='ghost' className='rounded-lg'>
  75. <IconEdit size={24} className='stroke-muted-foreground' />
  76. </Button>
  77. </div>
  78. <label className='flex h-12 w-full items-center space-x-0 rounded-md border border-input pl-2 focus-within:outline-none focus-within:ring-1 focus-within:ring-ring'>
  79. <IconSearch size={15} className='mr-2 stroke-slate-500' />
  80. <span className='sr-only'>Search</span>
  81. <input
  82. type='text'
  83. className='w-full flex-1 bg-inherit text-sm focus-visible:outline-none'
  84. placeholder='Search chat...'
  85. value={search}
  86. onChange={(e) => setSearch(e.target.value)}
  87. />
  88. </label>
  89. </div>
  90. <ScrollArea className='-mx-3 h-full p-3'>
  91. {filteredChatList.map((chatUsr) => {
  92. const { id, profile, username, messages, fullName } = chatUsr
  93. const lastConvo = messages[0]
  94. const lastMsg =
  95. lastConvo.sender === 'You'
  96. ? `You: ${lastConvo.message}`
  97. : lastConvo.message
  98. return (
  99. <Fragment key={id}>
  100. <button
  101. type='button'
  102. className={cn(
  103. `-mx-1 flex w-full rounded-md px-2 py-2 text-left text-sm hover:bg-secondary/75`,
  104. selectedUser.id === id && 'sm:bg-muted'
  105. )}
  106. onClick={() => {
  107. setSelectedUser(chatUsr)
  108. setMobileSelectedUser(chatUsr)
  109. }}
  110. >
  111. <div className='flex gap-2'>
  112. <Avatar>
  113. <AvatarImage src={profile} alt={username} />
  114. <AvatarFallback>{username}</AvatarFallback>
  115. </Avatar>
  116. <div>
  117. <span className='col-start-2 row-span-2 font-medium'>
  118. {fullName}
  119. </span>
  120. <span className='col-start-2 row-span-2 row-start-2 line-clamp-2 text-ellipsis text-muted-foreground'>
  121. {lastMsg}
  122. </span>
  123. </div>
  124. </div>
  125. </button>
  126. <Separator className='my-1' />
  127. </Fragment>
  128. )
  129. })}
  130. </ScrollArea>
  131. </div>
  132. {/* Right Side */}
  133. <div
  134. className={cn(
  135. 'absolute inset-0 hidden left-full z-50 w-full flex-1 flex-col rounded-md border bg-primary-foreground shadow-sm transition-all duration-200 sm:static sm:z-auto sm:flex',
  136. mobileSelectedUser && 'left-0 flex'
  137. )}
  138. >
  139. {/* Top Part */}
  140. <div className='mb-1 flex flex-none justify-between rounded-t-md bg-secondary p-4 shadow-lg'>
  141. {/* Left */}
  142. <div className='flex gap-3'>
  143. <Button
  144. size='icon'
  145. variant='ghost'
  146. className='-ml-2 h-full sm:hidden'
  147. onClick={() => setMobileSelectedUser(null)}
  148. >
  149. <IconArrowLeft />
  150. </Button>
  151. <div className='flex items-center gap-2 lg:gap-4'>
  152. <Avatar className='size-9 lg:size-11'>
  153. <AvatarImage
  154. src={selectedUser.profile}
  155. alt={selectedUser.username}
  156. />
  157. <AvatarFallback>{selectedUser.username}</AvatarFallback>
  158. </Avatar>
  159. <div>
  160. <span className='col-start-2 row-span-2 text-sm font-medium lg:text-base'>
  161. {selectedUser.fullName}
  162. </span>
  163. <span className='col-start-2 row-span-2 row-start-2 line-clamp-1 block max-w-32 text-ellipsis text-nowrap text-xs text-muted-foreground lg:max-w-none lg:text-sm'>
  164. {selectedUser.title}
  165. </span>
  166. </div>
  167. </div>
  168. </div>
  169. {/* Right */}
  170. <div className='-mr-1 flex items-center gap-1 lg:gap-2'>
  171. <Button
  172. size='icon'
  173. variant='ghost'
  174. className='hidden size-8 rounded-full sm:inline-flex lg:size-10'
  175. >
  176. <IconVideo size={22} className='stroke-muted-foreground' />
  177. </Button>
  178. <Button
  179. size='icon'
  180. variant='ghost'
  181. className='hidden size-8 rounded-full sm:inline-flex lg:size-10'
  182. >
  183. <IconPhone size={22} className='stroke-muted-foreground' />
  184. </Button>
  185. <Button
  186. size='icon'
  187. variant='ghost'
  188. className='h-10 rounded-md sm:h-8 sm:w-4 lg:h-10 lg:w-6'
  189. >
  190. <IconDotsVertical className='stroke-muted-foreground sm:size-5' />
  191. </Button>
  192. </div>
  193. </div>
  194. {/* Conversation */}
  195. <div className='flex flex-1 flex-col gap-2 rounded-md px-4 pb-4 pt-0'>
  196. <div className='flex size-full flex-1'>
  197. <div className='chat-text-container relative -mr-4 flex flex-1 flex-col overflow-y-hidden'>
  198. <div className='chat-flex flex h-40 w-full flex-grow flex-col-reverse justify-start gap-4 overflow-y-auto py-2 pb-4 pr-4'>
  199. {currentMessage &&
  200. Object.keys(currentMessage).map((key) => (
  201. <Fragment key={key}>
  202. {currentMessage[key].map((msg, index) => (
  203. <div
  204. key={`${msg.sender}-${msg.timestamp}-${index}`}
  205. className={cn(
  206. 'chat-box max-w-72 break-words px-3 py-2 shadow-lg',
  207. msg.sender === 'You'
  208. ? 'self-end rounded-[16px_16px_0_16px] bg-primary/85 text-primary-foreground/75'
  209. : 'self-start rounded-[16px_16px_16px_0] bg-secondary'
  210. )}
  211. >
  212. {msg.message}{' '}
  213. <span
  214. className={cn(
  215. 'mt-1 block text-xs font-light italic text-muted-foreground',
  216. msg.sender === 'You' && 'text-right'
  217. )}
  218. >
  219. {format(msg.timestamp, 'h:mm a')}
  220. </span>
  221. </div>
  222. ))}
  223. <div className='text-center text-xs'>{key}</div>
  224. </Fragment>
  225. ))}
  226. </div>
  227. </div>
  228. </div>
  229. <form className='flex w-full flex-none gap-2'>
  230. <div className='flex flex-1 items-center gap-2 rounded-md border border-input px-2 py-1 focus-within:outline-none focus-within:ring-1 focus-within:ring-ring lg:gap-4'>
  231. <div className='space-x-1'>
  232. <Button
  233. size='icon'
  234. type='button'
  235. variant='ghost'
  236. className='h-8 rounded-md'
  237. >
  238. <IconPlus size={20} className='stroke-muted-foreground' />
  239. </Button>
  240. <Button
  241. size='icon'
  242. type='button'
  243. variant='ghost'
  244. className='hidden h-8 rounded-md lg:inline-flex'
  245. >
  246. <IconPhotoPlus
  247. size={20}
  248. className='stroke-muted-foreground'
  249. />
  250. </Button>
  251. <Button
  252. size='icon'
  253. type='button'
  254. variant='ghost'
  255. className='hidden h-8 rounded-md lg:inline-flex'
  256. >
  257. <IconPaperclip
  258. size={20}
  259. className='stroke-muted-foreground'
  260. />
  261. </Button>
  262. </div>
  263. <label className='flex-1'>
  264. <span className='sr-only'>Chat Text Box</span>
  265. <input
  266. type='text'
  267. placeholder='Type your messages...'
  268. className='h-8 w-full bg-inherit focus-visible:outline-none'
  269. />
  270. </label>
  271. <Button
  272. variant='ghost'
  273. size='icon'
  274. className='hidden sm:inline-flex'
  275. >
  276. <IconSend size={20} />
  277. </Button>
  278. </div>
  279. <Button
  280. className='h-full sm:hidden'
  281. rightSection={<IconSend size={18} />}
  282. >
  283. Send
  284. </Button>
  285. </form>
  286. </div>
  287. </div>
  288. </section>
  289. </Main>
  290. </>
  291. )
  292. }