You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CalendarTab.tsx 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import Spinner from '@atlaskit/spinner';
  2. import { Theme } from '@mui/material';
  3. import { withStyles } from '@mui/styles';
  4. import React, { Component } from 'react';
  5. import { WithTranslation } from 'react-i18next';
  6. import { connect } from 'react-redux';
  7. import { IReduxState } from '../../../app/types';
  8. import { translate } from '../../../base/i18n/functions';
  9. import { withPixelLineHeight } from '../../../base/styles/functions.web';
  10. import Button from '../../../base/ui/components/web/Button';
  11. import {
  12. CALENDAR_TYPE,
  13. MicrosoftSignInButton,
  14. bootstrapCalendarIntegration,
  15. clearCalendarIntegration,
  16. isCalendarEnabled,
  17. signIn
  18. // @ts-ignore
  19. } from '../../../calendar-sync';
  20. // eslint-disable-next-line lines-around-comment
  21. // @ts-ignore
  22. import { GoogleSignInButton } from '../../../google-api';
  23. import logger from '../../logger';
  24. /**
  25. * The type of the React {@code Component} props of {@link CalendarTab}.
  26. */
  27. interface IProps extends WithTranslation {
  28. /**
  29. * The name given to this Jitsi Application.
  30. */
  31. _appName: string;
  32. /**
  33. * Whether or not to display a button to sign in to Google.
  34. */
  35. _enableGoogleIntegration: boolean;
  36. /**
  37. * Whether or not to display a button to sign in to Microsoft.
  38. */
  39. _enableMicrosoftIntegration: boolean;
  40. /**
  41. * The current calendar integration in use, if any.
  42. */
  43. _isConnectedToCalendar: boolean;
  44. /**
  45. * The email address associated with the calendar integration in use.
  46. */
  47. _profileEmail?: string;
  48. /**
  49. * CSS classes object.
  50. */
  51. classes: any;
  52. /**
  53. * Invoked to change the configured calendar integration.
  54. */
  55. dispatch: Function;
  56. }
  57. /**
  58. * The type of the React {@code Component} state of {@link CalendarTab}.
  59. */
  60. interface IState {
  61. /**
  62. * Whether or not any third party APIs are being loaded.
  63. */
  64. loading: boolean;
  65. }
  66. const styles = (theme: Theme) => {
  67. return {
  68. container: {
  69. width: '100%',
  70. display: 'flex',
  71. flexDirection: 'column' as const,
  72. alignItems: 'center',
  73. justifyContent: 'center',
  74. textAlign: 'center',
  75. minHeight: '100px',
  76. color: theme.palette.text01,
  77. ...withPixelLineHeight(theme.typography.bodyShortRegular)
  78. },
  79. button: {
  80. marginTop: theme.spacing(4)
  81. }
  82. };
  83. };
  84. /**
  85. * React {@code Component} for modifying calendar integration.
  86. *
  87. * @augments Component
  88. */
  89. class CalendarTab extends Component<IProps, IState> {
  90. /**
  91. * Initializes a new {@code CalendarTab} instance.
  92. *
  93. * @inheritdoc
  94. */
  95. constructor(props: IProps) {
  96. super(props);
  97. this.state = {
  98. loading: true
  99. };
  100. // Bind event handlers so they are only bound once for every instance.
  101. this._onClickDisconnect = this._onClickDisconnect.bind(this);
  102. this._onClickGoogle = this._onClickGoogle.bind(this);
  103. this._onClickMicrosoft = this._onClickMicrosoft.bind(this);
  104. }
  105. /**
  106. * Loads third party APIs as needed and bootstraps the initial calendar
  107. * state if not already set.
  108. *
  109. * @inheritdoc
  110. */
  111. componentDidMount() {
  112. this.props.dispatch(bootstrapCalendarIntegration())
  113. .catch((err: any) => logger.error('CalendarTab bootstrap failed', err))
  114. .then(() => this.setState({ loading: false }));
  115. }
  116. /**
  117. * Implements React's {@link Component#render()}.
  118. *
  119. * @inheritdoc
  120. * @returns {ReactElement}
  121. */
  122. render() {
  123. const { classes } = this.props;
  124. let view;
  125. if (this.state.loading) {
  126. view = this._renderLoadingState();
  127. } else if (this.props._isConnectedToCalendar) {
  128. view = this._renderSignOutState();
  129. } else {
  130. view = this._renderSignInState();
  131. }
  132. return (
  133. <div className = { classes.container }>
  134. { view }
  135. </div>
  136. );
  137. }
  138. /**
  139. * Dispatches the action to start the sign in flow for a given calendar
  140. * integration type.
  141. *
  142. * @param {string} type - The calendar type to try integrating with.
  143. * @private
  144. * @returns {void}
  145. */
  146. _attemptSignIn(type: string) {
  147. this.props.dispatch(signIn(type));
  148. }
  149. /**
  150. * Dispatches an action to sign out of the currently connected third party
  151. * used for calendar integration.
  152. *
  153. * @private
  154. * @returns {void}
  155. */
  156. _onClickDisconnect() {
  157. // We clear the integration state instead of actually signing out. This
  158. // is for two primary reasons. Microsoft does not support a sign out and
  159. // instead relies on clearing of local auth data. Google signout can
  160. // also sign the user out of YouTube. So for now we've decided not to
  161. // do an actual sign out.
  162. this.props.dispatch(clearCalendarIntegration());
  163. }
  164. /**
  165. * Starts the sign in flow for Google calendar integration.
  166. *
  167. * @private
  168. * @returns {void}
  169. */
  170. _onClickGoogle() {
  171. this._attemptSignIn(CALENDAR_TYPE.GOOGLE);
  172. }
  173. /**
  174. * Starts the sign in flow for Microsoft calendar integration.
  175. *
  176. * @private
  177. * @returns {void}
  178. */
  179. _onClickMicrosoft() {
  180. this._attemptSignIn(CALENDAR_TYPE.MICROSOFT);
  181. }
  182. /**
  183. * Render a React Element to indicate third party APIs are being loaded.
  184. *
  185. * @private
  186. * @returns {ReactElement}
  187. */
  188. _renderLoadingState() {
  189. return (
  190. <Spinner
  191. // @ts-ignore
  192. isCompleting = { false }
  193. size = 'medium' />
  194. );
  195. }
  196. /**
  197. * Render a React Element to sign into a third party for calendar
  198. * integration.
  199. *
  200. * @private
  201. * @returns {ReactElement}
  202. */
  203. _renderSignInState() {
  204. const {
  205. _appName,
  206. _enableGoogleIntegration,
  207. _enableMicrosoftIntegration,
  208. classes,
  209. t
  210. } = this.props;
  211. return (
  212. <>
  213. <p>
  214. { t('settings.calendar.about',
  215. { appName: _appName || '' }) }
  216. </p>
  217. { _enableGoogleIntegration
  218. && <div className = { classes.button }>
  219. <GoogleSignInButton
  220. onClick = { this._onClickGoogle }
  221. text = { t('liveStreaming.signIn') } />
  222. </div> }
  223. { _enableMicrosoftIntegration
  224. && <div className = { classes.button }>
  225. <MicrosoftSignInButton
  226. onClick = { this._onClickMicrosoft }
  227. text = { t('settings.calendar.microsoftSignIn') } />
  228. </div> }
  229. </>
  230. );
  231. }
  232. /**
  233. * Render a React Element to sign out of the currently connected third
  234. * party used for calendar integration.
  235. *
  236. * @private
  237. * @returns {ReactElement}
  238. */
  239. _renderSignOutState() {
  240. const { _profileEmail, classes, t } = this.props;
  241. return (
  242. <>
  243. { t('settings.calendar.signedIn',
  244. { email: _profileEmail }) }
  245. <Button
  246. className = { classes.button }
  247. id = 'calendar_logout'
  248. label = { t('settings.calendar.disconnect') }
  249. onClick = { this._onClickDisconnect } />
  250. </>
  251. );
  252. }
  253. }
  254. /**
  255. * Maps (parts of) the Redux state to the associated props for the
  256. * {@code CalendarTab} component.
  257. *
  258. * @param {Object} state - The Redux state.
  259. * @private
  260. * @returns {{
  261. * _appName: string,
  262. * _enableGoogleIntegration: boolean,
  263. * _enableMicrosoftIntegration: boolean,
  264. * _isConnectedToCalendar: boolean,
  265. * _profileEmail: string
  266. * }}
  267. */
  268. function _mapStateToProps(state: IReduxState) {
  269. const calendarState = state['features/calendar-sync'] || {};
  270. const {
  271. googleApiApplicationClientID,
  272. microsoftApiApplicationClientID
  273. } = state['features/base/config'];
  274. const calendarEnabled = isCalendarEnabled(state);
  275. return {
  276. _appName: interfaceConfig.APP_NAME,
  277. _enableGoogleIntegration: Boolean(
  278. calendarEnabled && googleApiApplicationClientID),
  279. _enableMicrosoftIntegration: Boolean(
  280. calendarEnabled && microsoftApiApplicationClientID),
  281. _isConnectedToCalendar: calendarState.integrationReady,
  282. _profileEmail: calendarState.profileEmail
  283. };
  284. }
  285. export default withStyles(styles)(translate(connect(_mapStateToProps)(CalendarTab)));