aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/layout/AppLayout.tsx
blob: 97795212a43dd38d96e9cb545ac553872aea7ae5 (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
226
227
228
229
import React, { Component, PropsWithChildren } from 'react';
import { observer } from 'mobx-react';
import { defineMessages, injectIntl, WrappedComponentProps } from 'react-intl';
import { TitleBar } from 'electron-react-titlebar/renderer';
import injectSheet, { WithStylesProps } from 'react-jss';
import { ipcRenderer } from 'electron';

import { mdiFlash, mdiPowerPlug } from '@mdi/js';
import { Outlet } from 'react-router-dom';
import InfoBar from '../ui/InfoBar';
import { Component as BasicAuth } from '../../features/basicAuth';
import { Component as QuickSwitch } from '../../features/quickSwitch';
import { Component as PublishDebugInfo } from '../../features/publishDebugInfo';
import ErrorBoundary from '../util/ErrorBoundary';
import { updateVersionParse } from '../../helpers/update-helpers';

import { isMac, isWindows } from '../../environment';
import WorkspaceSwitchingIndicator from '../../features/workspaces/components/WorkspaceSwitchingIndicator';
import { workspaceStore } from '../../features/workspaces';
import AppUpdateInfoBar from '../AppUpdateInfoBar';
import Todos from '../../features/todos/containers/TodosScreen';
import Icon from '../ui/icon';

import LockedScreen from '../../containers/auth/LockedScreen';
import SettingsStore from '../../stores/SettingsStore';

const messages = defineMessages({
  servicesUpdated: {
    id: 'infobar.servicesUpdated',
    defaultMessage: 'Your services have been updated.',
  },
  buttonReloadServices: {
    id: 'infobar.buttonReloadServices',
    defaultMessage: 'Reload services',
  },
  requiredRequestsFailed: {
    id: 'infobar.requiredRequestsFailed',
    defaultMessage: 'Could not load services and user information',
  },
  authRequestFailed: {
    id: 'infobar.authRequestFailed',
    defaultMessage:
      'There were errors while trying to perform an authenticated request. Please try logging out and back in if this error persists.',
  },
});

const transition = window?.matchMedia('(prefers-reduced-motion: no-preference)')
  ? 'transform 0.5s ease'
  : 'none';

const styles = (theme: { workspaces: { drawer: { width: any } } }) => ({
  appContent: {
    // width: `calc(100% + ${theme.workspaces.drawer.width}px)`,
    width: '100%',
    transition,
    transform() {
      return workspaceStore.isWorkspaceDrawerOpen
        ? 'translateX(0)'
        : `translateX(-${theme.workspaces.drawer.width}px)`;
    },
  },
  titleBar: {
    display: 'block',
    zIndex: 1,
    width: '100%',
    height: '10px',
    position: 'absolute',
    top: 0,
  },
});

const toggleFullScreen = () => {
  ipcRenderer.send('window.toolbar-double-clicked');
};

interface IProps extends WrappedComponentProps, WithStylesProps<typeof styles> {
  settings: SettingsStore;
  updateVersion: string;
  isFullScreen: boolean;
  sidebar: React.ReactElement;
  workspacesDrawer: React.ReactElement;
  services: React.ReactElement;
  showServicesUpdatedInfoBar: boolean;
  appUpdateIsDownloaded: boolean;
  authRequestFailed: boolean;
  installAppUpdate: () => void;
  showRequiredRequestsError: boolean;
  areRequiredRequestsSuccessful: boolean;
  retryRequiredRequests: () => void;
  areRequiredRequestsLoading: boolean;
}

interface IState {
  shouldShowAppUpdateInfoBar: boolean;
  shouldShowServicesUpdatedInfoBar: boolean;
}

@observer
class AppLayout extends Component<PropsWithChildren<IProps>, IState> {
  constructor(props) {
    super(props);

    this.state = {
      shouldShowAppUpdateInfoBar: true,
      shouldShowServicesUpdatedInfoBar: true,
    };
  }

  render() {
    const {
      classes,
      isFullScreen,
      workspacesDrawer,
      sidebar,
      services,
      showServicesUpdatedInfoBar,
      appUpdateIsDownloaded,
      authRequestFailed,
      installAppUpdate,
      settings,
      showRequiredRequestsError,
      areRequiredRequestsSuccessful,
      retryRequiredRequests,
      areRequiredRequestsLoading,
      updateVersion,
    } = this.props;

    const { intl } = this.props;

    const { locked, automaticUpdates } = settings.app;
    if (locked) {
      return <LockedScreen />;
    }

    return (
      <>
        {isMac && !isFullScreen && <div className="window-draggable" />}
        <ErrorBoundary>
          <div className="app">
            {isWindows && !isFullScreen && (
              <TitleBar
                menu={window['ferdium'].menu.template}
                icon="assets/images/logo.svg"
              />
            )}
            {isMac && !isFullScreen && (
              <span
                onDoubleClick={toggleFullScreen}
                className={classes.titleBar}
              />
            )}
            <div className={`app__content ${classes.appContent}`}>
              {workspacesDrawer}
              {sidebar}
              <div className="app__service">
                <WorkspaceSwitchingIndicator />
                {!areRequiredRequestsSuccessful &&
                  showRequiredRequestsError && (
                    <InfoBar
                      type="danger"
                      ctaLabel="Try again"
                      ctaLoading={areRequiredRequestsLoading}
                      sticky
                      onClick={retryRequiredRequests}
                    >
                      <Icon icon={mdiFlash} />
                      {intl.formatMessage(messages.requiredRequestsFailed)}
                    </InfoBar>
                  )}
                {authRequestFailed && (
                  <InfoBar
                    type="danger"
                    ctaLabel="Try again"
                    ctaLoading={areRequiredRequestsLoading}
                    sticky
                    onClick={retryRequiredRequests}
                  >
                    <Icon icon={mdiFlash} />
                    {intl.formatMessage(messages.authRequestFailed)}
                  </InfoBar>
                )}
                {automaticUpdates &&
                  showServicesUpdatedInfoBar &&
                  this.state.shouldShowServicesUpdatedInfoBar && (
                    <InfoBar
                      type="primary"
                      ctaLabel={intl.formatMessage(
                        messages.buttonReloadServices,
                      )}
                      onClick={() => window.location.reload()}
                      onHide={() => {
                        this.setState({
                          shouldShowServicesUpdatedInfoBar: false,
                        });
                      }}
                    >
                      <Icon icon={mdiPowerPlug} />
                      {intl.formatMessage(messages.servicesUpdated)}
                    </InfoBar>
                  )}
                {automaticUpdates &&
                  appUpdateIsDownloaded &&
                  this.state.shouldShowAppUpdateInfoBar && (
                    <AppUpdateInfoBar
                      onInstallUpdate={installAppUpdate}
                      updateVersionParsed={updateVersionParse(updateVersion)}
                      onHide={() => {
                        this.setState({ shouldShowAppUpdateInfoBar: false });
                      }}
                    />
                  )}
                <BasicAuth />
                <QuickSwitch />
                <PublishDebugInfo />
                {services}
                <Outlet />
              </div>
              <Todos />
            </div>
          </div>
        </ErrorBoundary>
      </>
    );
  }
}

export default injectIntl(
  injectSheet(styles, { injectTheme: true })(AppLayout),
);