File size: 4,037 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// @flow
import * as React from 'react';
import { connect } from 'react-redux';
import { closeGallery } from 'src/actions/gallery';
import type { GetMediaMessagesForThreadType } from 'shared/graphql/queries/message/getMediaMessagesForThread';
import type { Dispatch } from 'redux';
import {
  Overlay,
  ActiveImage,
  Minigallery,
  MiniImg,
  MiniContainer,
  CloseButton,
  GalleryWrapper,
} from './style';
import { ESC, ARROW_LEFT, ARROW_RIGHT } from 'src/helpers/keycodes';

type State = {
  images: Array<Object>,
  activeMessageId: string,
  index: ?number,
};

type Props = {
  dispatch: Dispatch<Object>,
  data: {
    messages?: GetMediaMessagesForThreadType,
  },
  activeMessageId: string,
};

class Browser extends React.Component<Props, State> {
  constructor(props) {
    super(props);
    // if there are no messages found
    if (!props.data.messages || props.data.messages.length === 0) {
      this.state = {
        images: [],
        activeMessageId: props.activeMessageId,
        index: null,
      };
    }

    let index;
    props.data.messages.map((message, i) => {
      if (message.id === props.activeMessageId) {
        index = i;
        return message;
      } else {
        return message;
      }
    });

    this.state = {
      images: props.data.messages,
      activeMessageId: props.activeMessageId,
      index,
    };
  }

  componentDidMount() {
    // $FlowFixMe
    document.addEventListener('keydown', this.handleKeyPress, false);
  }

  componentWillUnmount() {
    // $FlowFixMe
    document.removeEventListener('keydown', this.handleKeyPress, false);
  }

  closeGallery = () => {
    this.props.dispatch(closeGallery());
  };

  handleKeyPress = e => {
    const { images } = this.state;
    // if no media, skip on outta here
    if (!images) return;

    if (e.keyCode === ESC) {
      this.closeGallery();
    }

    if (e.keyCode === ARROW_LEFT) {
      this.previousImage();
    }

    if (e.keyCode === ARROW_RIGHT) {
      this.nextImage();
    }
  };

  previousImage = () => {
    let { index, images } = this.state;

    if (index === null) return;

    if (index === 0) {
      index = images.length - 1;
      this.setState({
        index,
      });
    } else {
      // $FlowFixMe
      index -= 1;

      this.setState({
        index,
      });
    }
  };

  nextImage = () => {
    let { index, images } = this.state;
    if (index === images.length - 1) {
      index = 0;
      this.setState({
        index,
      });
    } else {
      index += 1;

      this.setState({
        index,
      });
    }
  };

  setCount = i => {
    this.setState({
      index: i,
    });
  };

  render() {
    const { images, index } = this.state;
    const {
      data: { messages },
    } = this.props;

    if (!messages || messages.length === 0) return null;

    // when a user uploads an image, sometimes the resulting image doesn't get updated in the Apollo cache
    // if it doesn't update in the cache, then the browser component will receive a bad `activeMessageId`
    // prop. If it's the case that this happens, we just select the *last* image, assuming it's the one that the user just uploaded.
    let filteredIndex = typeof index === 'number' ? index : messages.length - 1;

    const src = `${images[filteredIndex].content.body}`;

    return (
      <GalleryWrapper>
        <CloseButton onClick={this.closeGallery}></CloseButton>
        <Overlay onClick={this.closeGallery} onKeyDown={this.handleKeyPress} />
        <ActiveImage onClick={this.nextImage} src={src} />
        <Minigallery>
          <MiniContainer>
            {images.map((image, i) => {
              return (
                <MiniImg
                  src={`${image.content.body}`}
                  key={i}
                  onClick={() => this.setCount(i)}
                  active={i === filteredIndex}
                />
              );
            })}
          </MiniContainer>
        </Minigallery>
      </GalleryWrapper>
    );
  }
}

export default connect()(Browser);