File size: 3,316 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import format from 'date-fns/format';
import React from 'react';
import { useEffect, useRef } from 'react';
import styled, { css } from 'styled-components';
import { useInfiniteScroll } from '../../hooks/use-infinite-scroll';
import { useAdjustedScroll } from '../../hooks/use-adjusted-scroll';

const Container = styled.div`
  position: relative;
  display: block;
  flex: 2;
  overflow-y: overlay;
  padding: 0 15px;
`;

const LoadingMore = styled.div`
  height: 30px;
  line-height: 30px;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  text-align: center;
`;

type StyledProp = {
  isMine: any;
};

const MessageItem = styled.div`
  display: inline-block;
  position: relative;
  max-width: 100%;
  border-radius: 7px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
  margin-top: 10px;
  margin-bottom: 10px;
  clear: both;

  &::after {
    content: '';
    display: table;
    clear: both;
  }

  &::before {
    content: '';
    position: absolute;
    bottom: 3px;
    width: 12px;
    height: 19px;
    background-position: 50% 50%;
    background-repeat: no-repeat;
    background-size: contain;
  }

  ${(props: StyledProp) =>
    props.isMine
      ? css`
          float: right;
          background-color: #dcf8c6;

          &::before {
            right: -11px;
            background-image: url(/assets/message-mine.png);
          }
        `
      : css`
          float: left;
          background-color: #fff;

          &::before {
            left: -11px;
            background-image: url(/assets/message-other.png);
          }
        `}
`;

const Contents = styled.div`
  padding: 5px 7px;
  word-wrap: break-word;

  &::after {
    content: ' \\00a0\\00a0\\00a0\\00a0\\00a0\\00a0\\00a0\\00a0\\00a0\\00a0\\00a0\\00a0\\00a0\\00a0\\00a0\\00a0\\00a0\\00a0\\00a0';
    display: inline;
  }
`;

const Timestamp = styled.div`
  position: absolute;
  bottom: 2px;
  right: 7px;
  color: gray;
  font-size: 12px;
`;

interface Message {
  id: string | null;
  content: string | null;
  createdAt: string | null;
}
interface MessagesListProps {
  messages: Array<Message>;
  loadMore: Function;
  hasMore: boolean;
}

const MessagesList: React.FC<MessagesListProps> = ({
  messages,
  loadMore,
  hasMore,
}) => {
  const selfRef = useRef<HTMLDivElement>(null);
  const [fetching, stopFetching] = useInfiniteScroll({
    onLoadMore: loadMore,
    hasMore,
    ref: selfRef!,
  });
  const adjustScroll = useAdjustedScroll(selfRef);

  useEffect(() => {
    if (!selfRef.current) return;

    if (fetching) {
      stopFetching();
      adjustScroll();
    } else {
      // scroll to the most recent message
      adjustScroll(true);
    }
  }, [messages.length, selfRef, fetching, stopFetching, adjustScroll]);

  return (
    <Container ref={selfRef}>
      {fetching && <LoadingMore>{'Loading more messages...'}</LoadingMore>}
      {messages.map((message: any) => (
        <MessageItem
          data-testid="message-item"
          isMine={message.isMine}
          key={message.id}>
          <Contents data-testid="message-content">{message.content}</Contents>
          <Timestamp data-testid="message-date">
            {format(message.createdAt, 'HH:mm')}
          </Timestamp>
        </MessageItem>
      ))}
    </Container>
  );
};

export default MessagesList;