|
| 1 | +import { parseDate, parseTime } from '@/common/utils/parseDate'; |
| 2 | +import MyChat from '../../MyChat'; |
| 3 | +import OtherChat from '../../OtherChat'; |
| 4 | +import * as s from './style.css'; |
| 5 | +import PickChat from '../../PickChat'; |
| 6 | +import { useEffect, useRef } from 'react'; |
| 7 | +import React from 'react'; |
| 8 | + |
| 9 | +// [ TODO ] |
| 10 | +// 스크롤 좀 올렸을 때 맨 아래로 가는 버튼도 만들어야 하려나 |
| 11 | +// 키보드 올리면 화면 밀려 올라가게 |
| 12 | +// api 붙이기~~~ camera, send 아이콘 동작 붙이기~~~ |
| 13 | + |
| 14 | +interface Chat { |
| 15 | + isMine?: boolean; |
| 16 | + message?: string; |
| 17 | + isPick?: boolean; |
| 18 | + date: string; |
| 19 | +} |
| 20 | + |
| 21 | +const dummyData: Chat[] = [ |
| 22 | + { isMine: true, message: '응 어쩔', date: '2025-07-09T15:56:31.591185' }, |
| 23 | + { isMine: true, message: '배고파', date: '2025-07-09T15:56:31.591185' }, |
| 24 | + { isMine: true, message: '응', date: '2025-07-09T15:57:31.591185' }, |
| 25 | + { isMine: false, message: '으아아아아ㅏ 노페인 노개인 음악업슨 ㄴ세상', date: '2025-07-09T15:57:31.591185' }, |
| 26 | + { isMine: false, message: '요즘 매미가 비가 와도 안 울어', date: '2025-07-09T15:58:31.591185' }, |
| 27 | + { isMine: false, message: '어떤 데이터가 올까...?', date: '2025-07-09T15:58:31.591185' }, |
| 28 | + { isMine: true, message: 'isMine도 보내주나? 아님 내가 체크해야 하나?', date: '2025-07-09T15:59:31.591185' }, |
| 29 | + { isMine: true, message: '스크롤 체크를 해보아요', date: '2025-07-09T15:59:31.591185' }, |
| 30 | + { isMine: true, message: '그룹핑이 바로 전 거랑 내가 직접 비교하는 수밖에 없나', date: '2025-07-09T15:59:31.591185' }, |
| 31 | + { isMine: false, message: '나 내일 권예진이랑 초밥 먹는당', date: '2025-07-09T15:59:31.591185' }, |
| 32 | + { isMine: false, message: '부럽지 ㅋㅋ', date: '2025-07-09T16:00:31.591185' }, |
| 33 | + { isMine: true, message: '너무 부럽다.', date: '2025-07-09T16:00:31.591185' }, |
| 34 | + { isMine: true, message: '집 가야지 하하하 예재무 공부해야지', date: '2025-07-09T16:01:31.591185' }, |
| 35 | + { isPick: true, isMine: true, date: '2025-07-09T16:07:31.591185' }, |
| 36 | + { isPick: true, isMine: false, date: '2025-07-09T16:07:31.591185' }, |
| 37 | + { isMine: true, message: '공부는 안 하고 지금 왜 개발만', date: '2025-07-18T16:01:31.591185' }, |
| 38 | + { isMine: true, message: 'ㅠㅠㅠ', date: '2025-07-18T16:02:31.591185' }, |
| 39 | + { isMine: false, message: 'ㅠㅠㅠ', date: '2025-07-18T16:02:31.591185' }, |
| 40 | +]; |
| 41 | + |
| 42 | +export const ChatRoomContent = () => { |
| 43 | + const ref = useRef<HTMLDivElement | null>(null); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + ref.current?.scrollIntoView({ behavior: 'auto' }); |
| 47 | + }, []); |
| 48 | + |
| 49 | + return ( |
| 50 | + <div> |
| 51 | + <div className={s.Wrapper}> |
| 52 | + {dummyData.map((chat, index) => { |
| 53 | + const time = parseTime(chat.date); |
| 54 | + const date = parseDate(chat.date); |
| 55 | + |
| 56 | + const prevDate = index > 0 ? parseDate(dummyData[index - 1].date) : null; |
| 57 | + const prevIsMine = index > 0 ? dummyData[index - 1].isMine : null; |
| 58 | + |
| 59 | + const isNewDate = date !== prevDate; |
| 60 | + const isNewIsMine = chat.isMine !== prevIsMine; |
| 61 | + const isFirst = index === 0; |
| 62 | + |
| 63 | + const marginTop = isNewIsMine ? '2.25rem' : '0.75rem'; |
| 64 | + |
| 65 | + return ( |
| 66 | + // TODO: <React.Fragment key={`${chat.date}-${chat.message || 'no-message'}`}>로 변경 (key를 index 말고 고유할 수 있는 값으로,,,) |
| 67 | + <React.Fragment key={index}> |
| 68 | + {isNewDate && <div className={s.Date({ isFirst: isFirst })}>{date}</div>} |
| 69 | + |
| 70 | + {chat.isPick ? ( |
| 71 | + <PickChat marginTop={marginTop} isMine={chat.isMine} /> |
| 72 | + ) : chat.isMine ? ( |
| 73 | + <MyChat marginTop={marginTop} time={time}> |
| 74 | + {chat.message} |
| 75 | + </MyChat> |
| 76 | + ) : ( |
| 77 | + <OtherChat marginTop={marginTop} time={time}> |
| 78 | + {chat.message} |
| 79 | + </OtherChat> |
| 80 | + )} |
| 81 | + </React.Fragment> |
| 82 | + ); |
| 83 | + })} |
| 84 | + </div> |
| 85 | + <div ref={ref} /> |
| 86 | + </div> |
| 87 | + ); |
| 88 | +}; |
| 89 | + |
| 90 | +export default ChatRoomContent; |
0 commit comments