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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import { Theme } from '@mui/material';
  2. import { withStyles } from '@mui/styles';
  3. import React, { Component } from 'react';
  4. import { WithTranslation } from 'react-i18next';
  5. import { connect } from 'react-redux';
  6. import { IReduxState } from '../../../app/types';
  7. import { translate } from '../../../base/i18n/functions';
  8. import { withPixelLineHeight } from '../../../base/styles/functions.web';
  9. import Button from '../../../base/ui/components/web/Button';
  10. import Spinner from '../../../base/ui/components/web/Spinner';
  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. );
  192. }
  193. /**
  194. * Render a React Element to sign into a third party for calendar
  195. * integration.
  196. *
  197. * @private
  198. * @returns {ReactElement}
  199. */
  200. _renderSignInState() {
  201. const {
  202. _appName,
  203. _enableGoogleIntegration,
  204. _enableMicrosoftIntegration,
  205. classes,
  206. t
  207. } = this.props;
  208. return (
  209. <>
  210. <p>
  211. { t('settings.calendar.about',
  212. { appName: _appName || '' }) }
  213. </p>
  214. { _enableGoogleIntegration
  215. && <div className = { classes.button }>
  216. <GoogleSignInButton
  217. onClick = { this._onClickGoogle }
  218. text = { t('liveStreaming.signIn') } />
  219. </div> }
  220. { _enableMicrosoftIntegration
  221. && <div className = { classes.button }>
  222. <MicrosoftSignInButton
  223. onClick = { this._onClickMicrosoft }
  224. text = { t('settings.calendar.microsoftSignIn') } />
  225. </div> }
  226. </>
  227. );
  228. }
  229. /**
  230. * Render a React Element to sign out of the currently connected third
  231. * party used for calendar integration.
  232. *
  233. * @private
  234. * @returns {ReactElement}
  235. */
  236. _renderSignOutState() {
  237. const { _profileEmail, classes, t } = this.props;
  238. return (
  239. <>
  240. { t('settings.calendar.signedIn',
  241. { email: _profileEmail }) }
  242. <Button
  243. className = { classes.button }
  244. id = 'calendar_logout'
  245. label = { t('settings.calendar.disconnect') }
  246. onClick = { this._onClickDisconnect } />
  247. </>
  248. );
  249. }
  250. }
  251. /**
  252. * Maps (parts of) the Redux state to the associated props for the
  253. * {@code CalendarTab} component.
  254. *
  255. * @param {Object} state - The Redux state.
  256. * @private
  257. * @returns {{
  258. * _appName: string,
  259. * _enableGoogleIntegration: boolean,
  260. * _enableMicrosoftIntegration: boolean,
  261. * _isConnectedToCalendar: boolean,
  262. * _profileEmail: string
  263. * }}
  264. */
  265. function _mapStateToProps(state: IReduxState) {
  266. const calendarState = state['features/calendar-sync'] || {};
  267. const {
  268. googleApiApplicationClientID,
  269. microsoftApiApplicationClientID
  270. } = state['features/base/config'];
  271. const calendarEnabled = isCalendarEnabled(state);
  272. return {
  273. _appName: interfaceConfig.APP_NAME,
  274. _enableGoogleIntegration: Boolean(
  275. calendarEnabled && googleApiApplicationClientID),
  276. _enableMicrosoftIntegration: Boolean(
  277. calendarEnabled && microsoftApiApplicationClientID),
  278. _isConnectedToCalendar: calendarState.integrationReady,
  279. _profileEmail: calendarState.profileEmail
  280. };
  281. }
  282. export default withStyles(styles)(translate(connect(_mapStateToProps)(CalendarTab)));