aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/todos/components/TodosWebview.tsx
blob: f9493d5197795ed7774e09364559e5c5144e05cb (plain) (blame)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import classnames from 'classnames';
import { observer } from 'mobx-react';
import {
  Component,
  type MouseEvent,
  type ReactElement,
  createRef,
} from 'react';
import Webview from 'react-electron-web-view';
import withStyles, { type WithStylesProps } from 'react-jss';
import { TODOS_PARTITION_ID } from '../../../config';
import type { TodoClientMessage } from '../actions';

const styles = theme => ({
  root: {
    background: theme.colorBackground,
    position: 'relative',
    borderLeft: [1, 'solid', theme.todos.todosLayer.borderLeftColor],
    zIndex: 300,

    transform: ({ isVisible, width, isTodosServiceActive }) =>
      `translateX(${isVisible || isTodosServiceActive ? 0 : width}px)`,

    '& webview': {
      height: '100%',
    },
  },
  resizeHandler: {
    position: 'absolute',
    left: 0,
    marginLeft: -5,
    width: 10,
    zIndex: 400,
    cursor: 'col-resize',
  },
  dragIndicator: {
    position: 'absolute',
    left: 0,
    width: 5,
    zIndex: 400,
    background: theme.todos.dragIndicator.background,
  },
  isTodosServiceActive: {
    width: 'calc(100% - 368px)',
    position: 'absolute',
    right: 0,
    zIndex: 0,
    borderLeftWidth: 0,
  },
  hidden: {
    borderLeftWidth: 0,
  },
});

interface IProps extends WithStylesProps<typeof styles> {
  isTodosServiceActive: boolean;
  isVisible: boolean;
  handleClientMessage: (channel: string, message: TodoClientMessage) => void;
  setTodosWebview: (webView: Webview) => void;
  resize: (newWidth: number) => void;
  width: number;
  minWidth: number;
  userAgent: string;
  todoUrl: string;
  isTodoUrlValid: boolean;
}

interface IState {
  isDragging: boolean;
  width: number;
  initialPos: number;
  delta: number;
}

@observer
class TodosWebview extends Component<IProps, IState> {
  private node = createRef<HTMLDivElement>();

  private webview: Webview;

  constructor(props: IProps) {
    super(props);

    this.state = {
      isDragging: false,
      width: 300,
      initialPos: 0,
      delta: 0,
    };
    this.resizePanel = this.resizePanel.bind(this);
    this.stopResize = this.stopResize.bind(this);
  }

  componentDidMount() {
    this.setState({
      width: this.props.width,
    });

    if (this.node.current) {
      this.node.current.addEventListener('mousemove', this.resizePanel);
      this.node.current.addEventListener('mouseup', this.stopResize);
      this.node.current.addEventListener('mouseleave', this.stopResize);
    }
  }

  startResize = (e: MouseEvent<HTMLDivElement>): void => {
    this.setState({
      isDragging: true,
      initialPos: e.clientX,
      delta: 0,
    });
  };

  resizePanel = (e: MouseEventInit): void => {
    const { minWidth } = this.props;
    const { isDragging, initialPos } = this.state;

    if (isDragging && Math.abs(e.clientX! - window.innerWidth) > minWidth) {
      const delta = e.clientX! - initialPos;

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

  stopResize = (): void => {
    const { resize, minWidth } = this.props;
    const { isDragging, delta, width } = this.state;

    if (isDragging) {
      let newWidth = width + (delta < 0 ? Math.abs(delta) : -Math.abs(delta));

      if (newWidth < minWidth) {
        newWidth = minWidth;
      }

      this.setState({
        isDragging: false,
        delta: 0,
        width: newWidth,
      });

      resize(newWidth);
    }
  };

  startListeningToIpcMessages = (): void => {
    if (!this.webview) {
      return;
    }

    const { handleClientMessage } = this.props;
    this.webview.addEventListener('ipc-message', e => {
      handleClientMessage(e.channel, e.args[0]);
    });
  };

  render(): ReactElement {
    const {
      classes,
      isTodosServiceActive,
      isVisible,
      userAgent,
      todoUrl,
      isTodoUrlValid,
    } = this.props;

    const { width, delta, isDragging } = this.state;
    let displayedWidth = isVisible ? width : 0;
    if (isTodosServiceActive) {
      displayedWidth = 0;
    }

    return (
      <div
        className={classnames({
          [classes.root]: true,
          [classes.isTodosServiceActive]: isTodosServiceActive,
          'todos__todos-panel--expanded': isTodosServiceActive,
          [classes.hidden]: !isVisible,
        })}
        style={{ width: displayedWidth }}
        onMouseUp={() => this.stopResize()}
        ref={this.node}
        id="todos-panel"
      >
        <div
          className={classes.resizeHandler}
          style={{
            left: delta,
            ...(isDragging ? { width: 600, marginLeft: -200 } : {}),
          }} // This hack is required as resizing with webviews beneath behaves quite bad
          onMouseDown={this.startResize}
        />
        {isDragging && (
          <div
            className={classes.dragIndicator}
            style={{ left: delta }} // This hack is required as resizing with webviews beneath behaves quite bad
          />
        )}
        {isTodoUrlValid && (
          <Webview
            // className={classes.webview} // TODO: [TS DEBT] style not found
            onDidAttach={() => {
              const { setTodosWebview } = this.props;
              setTodosWebview(this.webview);
              this.startListeningToIpcMessages();
            }}
            partition={TODOS_PARTITION_ID}
            preload="./features/todos/preload.js"
            ref={webview => {
              this.webview = webview ? webview.view : null;
            }}
            useragent={userAgent}
            src={todoUrl}
            allowpopups
          />
        )}
      </div>
    );
  }
}

export default withStyles(styles, { injectTheme: true })(TodosWebview);