File size: 2,267 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
// @flow
import React from 'react';
import compose from 'recompose/compose';
import { withRouter, type History } from 'react-router';
import type { UserInfoType } from 'shared/graphql/fragments/user/userInfo';
import { withCurrentUser } from 'src/components/withCurrentUser';
import { isViewingMarketingPage } from 'src/helpers/is-viewing-marketing-page';
import { StyledAppViewWrapper } from './style';

type Props = {
  isModal: boolean,
  currentUser: ?UserInfoType,
  history: History,
  location: Object,
};

class AppViewWrapper extends React.Component<Props> {
  ref: ?HTMLElement;
  prevScrollOffset: number;

  constructor(props: Props) {
    super(props);
    this.ref = null;
    this.prevScrollOffset = 0;
  }

  getSnapshotBeforeUpdate(prevProps) {
    const { isModal: currModal } = this.props;
    const { isModal: prevModal } = prevProps;

    /*
      If the user is going to open a modal, grab the current scroll
      offset of the main view the user is on and save it for now; we'll use
      the value to restore the scroll position after the user closes the modal
    */
    if (!prevModal && currModal && this.ref) {
      const offset = this.ref.scrollTop;
      this.prevScrollOffset = offset;
      return null;
    }

    if (prevModal && !currModal) {
      // the user is closing the modal, return the previous view's scroll offset
      return this.prevScrollOffset;
    }

    return null;
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    /*
      If we have a snapshot value, the user has closed a modal and we need
      to return the user to where they were previously scrolled in the primary
      view
    */
    if (snapshot !== null && this.ref) {
      this.ref.scrollTop = snapshot;
    }
  }

  render() {
    const { currentUser, history, location } = this.props;

    const isMarketingPage = isViewingMarketingPage(history, currentUser);
    const isViewingExplore = location && location.pathname === '/explore';
    const isTwoColumn = isViewingExplore || !isMarketingPage;

    return (
      <StyledAppViewWrapper
        ref={el => (this.ref = el)}
        isTwoColumn={isTwoColumn}
        {...this.props}
      />
    );
  }
}

export default compose(
  withRouter,
  withCurrentUser
)(AppViewWrapper);