File size: 2,218 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
// @flow
import React, { useEffect, useRef } from 'react';
import { connect } from 'react-redux';
import type { Location, History, Match } from 'react-router';
import Icon from 'src/components/icon';
import { ThreadView } from 'src/views/thread';
import { ErrorBoundary } from 'src/components/error';
import { ESC } from 'src/helpers/keycodes';
import { setTitlebarProps } from 'src/actions/titlebar';
import type { TitlebarPayloadProps } from 'src/views/globalTitlebar';
import { Container, Overlay, ThreadContainer, CloseButton } from './style';

type Props = {
  previousLocation: Location,
  history: History,
  match: Match,
  titlebar: TitlebarPayloadProps,
  dispatch: Dispatch<Object>,
};

const ThreadSlider = (props: Props) => {
  const { previousLocation, history, match, titlebar, dispatch } = props;
  const prevTitlebarProps = useRef(titlebar);
  const { params } = match;
  const { threadId } = params;

  const closeSlider = (e: any) => {
    e && e.stopPropagation();
    history.push({ ...previousLocation, state: { modal: false } });
  };

  useEffect(() => {
    const handleKeyPress = (e: any) => {
      if (e.keyCode === ESC) {
        e.stopPropagation();
        closeSlider();
      }
    };

    document.addEventListener('keydown', handleKeyPress, false);
    return () => {
      const prev = prevTitlebarProps.current;
      dispatch(setTitlebarProps({ ...prev }));
      document.removeEventListener('keydown', handleKeyPress, false);
    };
  }, []);

  return (
    <ErrorBoundary>
      <Container data-cy="modal-container">
        <ThreadContainer>
          <ThreadView
            isModal
            css={{
              width: '100%',
              position: 'relative',
              zIndex: '1',
            }}
            threadId={threadId}
          >
            <Overlay onClick={closeSlider} data-cy="overlay" />
          </ThreadView>
        </ThreadContainer>

        <CloseButton data-cy="thread-slider-close" onClick={closeSlider}>
          <Icon glyph="view-close" size={32} />
        </CloseButton>
      </Container>
    </ErrorBoundary>
  );
};

const map = state => ({ titlebar: state.titlebar });

// $FlowIssue
export default connect(map)(ThreadSlider);