Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

StartLiveStreamDialog.web.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // @flow
  2. import Spinner from '@atlaskit/spinner';
  3. import React from 'react';
  4. import { connect } from 'react-redux';
  5. import { translate } from '../../../base/i18n';
  6. import {
  7. GOOGLE_API_STATES,
  8. GoogleSignInButton,
  9. loadGoogleAPI,
  10. requestAvailableYouTubeBroadcasts,
  11. requestLiveStreamsForYouTubeBroadcast,
  12. showAccountSelection,
  13. signIn,
  14. updateProfile
  15. } from '../../../google-api';
  16. import AbstractStartLiveStreamDialog, {
  17. _mapStateToProps,
  18. type Props
  19. } from './AbstractStartLiveStreamDialog';
  20. import BroadcastsDropdown from './BroadcastsDropdown';
  21. import StreamKeyForm from './StreamKeyForm';
  22. /**
  23. * A React Component for requesting a YouTube stream key to use for live
  24. * streaming of the current conference.
  25. *
  26. * @extends Component
  27. */
  28. class StartLiveStreamDialog
  29. extends AbstractStartLiveStreamDialog {
  30. /**
  31. * Initializes a new {@code StartLiveStreamDialog} instance.
  32. *
  33. * @param {Props} props - The React {@code Component} props to initialize
  34. * the new {@code StartLiveStreamDialog} instance with.
  35. */
  36. constructor(props: Props) {
  37. super(props);
  38. // Bind event handlers so they are only bound once per instance.
  39. this._onGetYouTubeBroadcasts = this._onGetYouTubeBroadcasts.bind(this);
  40. this._onInitializeGoogleApi = this._onInitializeGoogleApi.bind(this);
  41. this._onGoogleSignIn = this._onGoogleSignIn.bind(this);
  42. this._onRequestGoogleSignIn = this._onRequestGoogleSignIn.bind(this);
  43. this._onYouTubeBroadcastIDSelected
  44. = this._onYouTubeBroadcastIDSelected.bind(this);
  45. this._renderDialogContent = this._renderDialogContent.bind(this);
  46. }
  47. _onInitializeGoogleApi: () => Promise<*>;
  48. /**
  49. * Loads the Google web client application used for fetching stream keys.
  50. * If the user is already logged in, then a request for available YouTube
  51. * broadcasts is also made.
  52. *
  53. * @private
  54. * @returns {Promise}
  55. */
  56. _onInitializeGoogleApi() {
  57. this.props.dispatch(
  58. loadGoogleAPI(this.props._googleApiApplicationClientID))
  59. .catch(response => this._parseErrorFromResponse(response));
  60. }
  61. /**
  62. * Automatically selects the input field's value after starting to edit the
  63. * display name.
  64. *
  65. * @inheritdoc
  66. * @returns {void}
  67. */
  68. componentDidUpdate(previousProps) {
  69. if (previousProps._googleAPIState === GOOGLE_API_STATES.LOADED
  70. && this.props._googleAPIState === GOOGLE_API_STATES.SIGNED_IN) {
  71. this._onGetYouTubeBroadcasts();
  72. }
  73. }
  74. _onGetYouTubeBroadcasts: () => Promise<*>;
  75. /**
  76. * Asks the user to sign in, if not already signed in, and then requests a
  77. * list of the user's YouTube broadcasts.
  78. *
  79. * @private
  80. * @returns {void}
  81. */
  82. _onGetYouTubeBroadcasts() {
  83. this.props.dispatch(updateProfile())
  84. .catch(response => this._parseErrorFromResponse(response));
  85. this.props.dispatch(requestAvailableYouTubeBroadcasts())
  86. .then(broadcasts => {
  87. this._setStateIfMounted({
  88. broadcasts
  89. });
  90. if (broadcasts.length === 1) {
  91. const broadcast = broadcasts[0];
  92. this._onYouTubeBroadcastIDSelected(broadcast.boundStreamID);
  93. }
  94. })
  95. .catch(response => this._parseErrorFromResponse(response));
  96. }
  97. _onGoogleSignIn: () => Object;
  98. /**
  99. * Forces the Google web client application to prompt for a sign in, such as
  100. * when changing account, and will then fetch available YouTube broadcasts.
  101. *
  102. * @private
  103. * @returns {Promise}
  104. */
  105. _onGoogleSignIn() {
  106. this.props.dispatch(signIn())
  107. .catch(response => this._parseErrorFromResponse(response));
  108. }
  109. _onRequestGoogleSignIn: () => Object;
  110. /**
  111. * Forces the Google web client application to prompt for a sign in, such as
  112. * when changing account, and will then fetch available YouTube broadcasts.
  113. *
  114. * @private
  115. * @returns {Promise}
  116. */
  117. _onRequestGoogleSignIn() {
  118. // when there is an error we show the google sign-in button.
  119. // once we click it we want to clear the error from the state
  120. this.props.dispatch(showAccountSelection())
  121. .then(() =>
  122. this._setStateIfMounted({
  123. broadcasts: undefined,
  124. errorType: undefined
  125. }))
  126. .then(() => this._onGetYouTubeBroadcasts());
  127. }
  128. _onStreamKeyChange: string => void;
  129. _onYouTubeBroadcastIDSelected: (string) => Object;
  130. /**
  131. * Fetches the stream key for a YouTube broadcast and updates the internal
  132. * state to display the associated stream key as being entered.
  133. *
  134. * @param {string} boundStreamID - The bound stream ID associated with the
  135. * broadcast from which to get the stream key.
  136. * @private
  137. * @returns {Promise}
  138. */
  139. _onYouTubeBroadcastIDSelected(boundStreamID) {
  140. this.props.dispatch(
  141. requestLiveStreamsForYouTubeBroadcast(boundStreamID))
  142. .then(({ streamKey, selectedBoundStreamID }) =>
  143. this._setStateIfMounted({
  144. streamKey,
  145. selectedBoundStreamID
  146. }));
  147. }
  148. /**
  149. * Only show an error if an external request was made with the Google api.
  150. * Do not error if the login in canceled.
  151. * And searches in a Google API error response for the error type.
  152. *
  153. * @param {Object} response - The Google API response that may contain an
  154. * error.
  155. * @private
  156. * @returns {string|null}
  157. */
  158. _parseErrorFromResponse(response) {
  159. if (!response || !response.result) {
  160. return;
  161. }
  162. const result = response.result;
  163. const error = result.error;
  164. const errors = error && error.errors;
  165. const firstError = errors && errors[0];
  166. this._setStateIfMounted({
  167. errorType: (firstError && firstError.reason) || null
  168. });
  169. }
  170. _renderDialogContent: () => React$Component<*>
  171. /**
  172. * Renders the platform specific dialog content.
  173. *
  174. * @returns {React$Component}
  175. */
  176. _renderDialogContent() {
  177. const { _googleApiApplicationClientID } = this.props;
  178. return (
  179. <div className = 'live-stream-dialog'>
  180. { _googleApiApplicationClientID
  181. ? this._renderYouTubePanel() : null }
  182. <StreamKeyForm
  183. onChange = { this._onStreamKeyChange }
  184. value = { this.state.streamKey || this.props._streamKey } />
  185. </div>
  186. );
  187. }
  188. /**
  189. * Renders a React Element for authenticating with the Google web client.
  190. *
  191. * @private
  192. * @returns {ReactElement}
  193. */
  194. _renderYouTubePanel() {
  195. const {
  196. t,
  197. _googleProfileEmail
  198. } = this.props;
  199. const {
  200. broadcasts,
  201. selectedBoundStreamID
  202. } = this.state;
  203. let googleContent, helpText;
  204. switch (this.props._googleAPIState) {
  205. case GOOGLE_API_STATES.LOADED:
  206. googleContent = (
  207. <GoogleSignInButton
  208. onClick = { this._onGoogleSignIn }
  209. text = { t('liveStreaming.signIn') } />
  210. );
  211. helpText = t('liveStreaming.signInCTA');
  212. break;
  213. case GOOGLE_API_STATES.SIGNED_IN:
  214. googleContent = (
  215. <BroadcastsDropdown
  216. broadcasts = { broadcasts }
  217. onBroadcastSelected = { this._onYouTubeBroadcastIDSelected }
  218. selectedBoundStreamID = { selectedBoundStreamID } />
  219. );
  220. /**
  221. * FIXME: Ideally this help text would be one translation string
  222. * that also accepts the anchor. This can be done using the Trans
  223. * component of react-i18next but I couldn't get it working...
  224. */
  225. helpText = (
  226. <div>
  227. { `${t('liveStreaming.chooseCTA',
  228. { email: _googleProfileEmail })} ` }
  229. <a onClick = { this._onRequestGoogleSignIn }>
  230. { t('liveStreaming.changeSignIn') }
  231. </a>
  232. </div>
  233. );
  234. break;
  235. case GOOGLE_API_STATES.NEEDS_LOADING:
  236. default:
  237. googleContent = (
  238. <Spinner
  239. isCompleting = { false }
  240. size = 'medium' />
  241. );
  242. break;
  243. }
  244. if (this.state.errorType !== undefined) {
  245. googleContent = (
  246. <GoogleSignInButton
  247. onClick = { this._onRequestGoogleSignIn }
  248. text = { t('liveStreaming.signIn') } />
  249. );
  250. helpText = this._getGoogleErrorMessageToDisplay();
  251. }
  252. return (
  253. <div className = 'google-panel'>
  254. <div className = 'live-stream-cta'>
  255. { helpText }
  256. </div>
  257. <div className = 'google-api'>
  258. { googleContent }
  259. </div>
  260. </div>
  261. );
  262. }
  263. _setStateIfMounted: Object => void
  264. /**
  265. * Returns the error message to display for the current error state.
  266. *
  267. * @private
  268. * @returns {string} The error message to display.
  269. */
  270. _getGoogleErrorMessageToDisplay() {
  271. let text;
  272. switch (this.state.errorType) {
  273. case 'liveStreamingNotEnabled':
  274. text = this.props.t(
  275. 'liveStreaming.errorLiveStreamNotEnabled',
  276. { email: this.props._googleProfileEmail });
  277. break;
  278. default:
  279. text = this.props.t('liveStreaming.errorAPI');
  280. break;
  281. }
  282. return <div className = 'google-error'>{ text }</div>;
  283. }
  284. }
  285. export default translate(connect(_mapStateToProps)(StartLiveStreamDialog));