특정 사용자가 1분안에 50개의 채팅을 보내면

마지막 채팅에만 시각을 보여줘야한다.

Untitled

시각을 보여주는 케이스

  1. 특정 유저의 채팅이 해당 시각에 마지막 채팅이면 보여준다.
시각 채팅 내용 시각 표시 여부 userId
09:56 안녕 x 1
09:56 이 프로젝트 x 1
09:56 좋아 x 1
09:56 맞지 x 1
09:56 ? o 1
  1. 특정 유저의 채팅이 해당 시각에 마지막 채팅이 아니지만 뒷 채팅과 유저 정보가 다르면 보여준다.
시각 채팅 내용 시각 표시 여부 userId
09:56 안녕 o 1
09:56 안녕하세요! o 2
09:56 좋아 x 1
09:56 맞지 x 1
09:56 ? o 1
  1. 시간대가 다르면 보여준다.
시각 채팅 내용 시각 표시 여부 userId
09:56 안녕 o 1
09:57 안녕하세요! o 1
09:57 좋아 x 2
09:57 맞지 x 2
09:57 ? o 2

위의 케이스를 고려하여 완성된 로직은 다음과 같다.

const checkIsLastChatFromUser = (
  targetIndex: number,
  chatLogData: Array<[string, ChatLog]>
): boolean => {
  if (targetIndex === chatLogData.length - 1) {
    return true;
  }

  const targetData = chatLogData[targetIndex][1];
  const nextData = chatLogData[targetIndex + 1][1];

  const targetTime = getTimeByDate(new Date(targetData.time));
  const nextTime = getTimeByDate(new Date(nextData.time));

  return targetTime !== nextTime || targetData.userId !== nextData.userId;
};