aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/editor/ConnectButton.tsx
blob: d08fbb4d41b7973de572e12d489949459b98970e (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
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import CloudIcon from '@mui/icons-material/Cloud';
import CloudOffIcon from '@mui/icons-material/CloudOff';
import SyncIcon from '@mui/icons-material/Sync';
import SyncProblemIcon from '@mui/icons-material/SyncProblem';
import IconButton from '@mui/material/IconButton';
import Tooltip from '@mui/material/Tooltip';
import { keyframes, styled } from '@mui/material/styles';
import { observer } from 'mobx-react-lite';

import type EditorStore from './EditorStore';

const rotateKeyframe = keyframes`
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(-360deg);
  }
`;

const AnimatedSyncIcon = styled(SyncIcon)`
  animation: ${rotateKeyframe} 1.4s linear infinite;
`;

export default observer(function ConnectButton({
  editorStore,
}: {
  editorStore: EditorStore | undefined;
}): JSX.Element {
  if (
    editorStore !== undefined &&
    (editorStore.opening || editorStore.opened)
  ) {
    return (
      <Tooltip
        title={
          editorStore.opening
            ? 'Connecting (click to cancel)'
            : 'Connected (click to disconnect)'
        }
      >
        <IconButton
          onClick={() => editorStore.disconnect()}
          aria-label="Disconnect"
          color="inherit"
        >
          {editorStore.opening ? (
            <AnimatedSyncIcon fontSize="small" />
          ) : (
            <CloudIcon fontSize="small" />
          )}
        </IconButton>
      </Tooltip>
    );
  }

  let title: string;
  let disconnectedIcon: JSX.Element;
  if (editorStore === undefined) {
    title = 'Connecting';
    disconnectedIcon = <SyncIcon fontSize="small" />;
  } else if (editorStore.connectionErrors.length > 0) {
    title = 'Connection error (click to retry)';
    disconnectedIcon = <SyncProblemIcon fontSize="small" />;
  } else {
    title = 'Disconnected (click to connect)';
    disconnectedIcon = <CloudOffIcon fontSize="small" />;
  }

  return (
    <Tooltip title={title}>
      <IconButton
        disabled={editorStore === undefined}
        onClick={() => editorStore?.connect()}
        aria-label="Connect"
        color="inherit"
      >
        {disconnectedIcon}
      </IconButton>
    </Tooltip>
  );
});