linking-section.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import { useState, ElementType } from 'react'
  2. import { Alert } from 'react-bootstrap'
  3. import { useTranslation } from 'react-i18next'
  4. import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
  5. import { useSSOContext, SSOSubscription } from '../context/sso-context'
  6. import { SSOLinkingWidget } from './linking/sso-widget'
  7. import getMeta from '../../../utils/meta'
  8. import { useBroadcastUser } from '@/shared/hooks/user-channel/use-broadcast-user'
  9. import { useSplitTestContext } from '@/shared/context/split-test-context'
  10. function LinkingSection() {
  11. useBroadcastUser()
  12. const { t } = useTranslation()
  13. const { subscriptions } = useSSOContext()
  14. const ssoErrorMessage = getMeta('ol-ssoErrorMessage') as string
  15. const projectSyncSuccessMessage = getMeta(
  16. 'ol-projectSyncSuccessMessage'
  17. ) as string
  18. const [integrationLinkingWidgets] = useState<any[]>(
  19. () =>
  20. getMeta('integrationLinkingWidgets') ||
  21. importOverleafModules('integrationLinkingWidgets')
  22. )
  23. const [referenceLinkingWidgets] = useState<any[]>(
  24. () =>
  25. getMeta('referenceLinkingWidgets') ||
  26. importOverleafModules('referenceLinkingWidgets')
  27. )
  28. const [langFeedbackLinkingWidgets] = useState<any[]>(
  29. () =>
  30. getMeta('langFeedbackLinkingWidgets') ||
  31. importOverleafModules('langFeedbackLinkingWidgets')
  32. )
  33. const oauth2ServerComponents = importOverleafModules('oauth2Server') as {
  34. import: { default: ElementType }
  35. path: string
  36. }[]
  37. const showPersonalAccessTokenComponents: boolean =
  38. getMeta('ol-showPersonalAccessToken') ||
  39. getMeta('ol-optionalPersonalAccessToken')
  40. const allIntegrationLinkingWidgets = showPersonalAccessTokenComponents
  41. ? integrationLinkingWidgets.concat(oauth2ServerComponents)
  42. : integrationLinkingWidgets
  43. // currently the only thing that is in the langFeedback section is writefull,
  44. // which is behind a split test. we should hide this section if the user is not in the split test
  45. // todo: remove split test check, and split test context after gradual rollout is complete
  46. const {
  47. splitTestVariants,
  48. }: { splitTestVariants: Record<string, string | undefined> } =
  49. useSplitTestContext()
  50. const shouldLoadWritefull =
  51. splitTestVariants['writefull-integration'] === 'enabled' &&
  52. !window.writefull // check if the writefull extension is installed, in which case we dont handle the integration
  53. const haslangFeedbackLinkingWidgets =
  54. langFeedbackLinkingWidgets.length && shouldLoadWritefull
  55. const hasIntegrationLinkingSection = allIntegrationLinkingWidgets.length
  56. const hasReferencesLinkingSection = referenceLinkingWidgets.length
  57. // Filter out SSO providers that are not allowed to be linked by
  58. // managed users. Allow unlinking them if they are already linked.
  59. const hideGoogleSSO = getMeta('ol-cannot-link-google-sso')
  60. const hideOtherThirdPartySSO = getMeta('ol-cannot-link-other-third-party-sso')
  61. for (const providerId in subscriptions) {
  62. const isLinked = subscriptions[providerId].linked
  63. if (providerId === 'google') {
  64. if (hideGoogleSSO && !isLinked) {
  65. delete subscriptions[providerId]
  66. }
  67. } else {
  68. if (hideOtherThirdPartySSO && !isLinked) {
  69. delete subscriptions[providerId]
  70. }
  71. }
  72. }
  73. const hasSSOLinkingSection = Object.keys(subscriptions).length > 0
  74. if (
  75. !haslangFeedbackLinkingWidgets &&
  76. !hasIntegrationLinkingSection &&
  77. !hasReferencesLinkingSection &&
  78. !hasSSOLinkingSection
  79. ) {
  80. return null
  81. }
  82. return (
  83. <>
  84. <h3>{t('integrations')}</h3>
  85. <p className="small">{t('linked_accounts_explained')}</p>
  86. {haslangFeedbackLinkingWidgets ? (
  87. <>
  88. <h3 id="project-sync" className="text-capitalize">
  89. {t('language_feedback')}
  90. </h3>
  91. <div className="settings-widgets-container">
  92. {langFeedbackLinkingWidgets.map(
  93. ({ import: { default: widget }, path }, widgetIndex) => (
  94. <ModuleLinkingWidget
  95. key={path}
  96. ModuleComponent={widget}
  97. isLast={widgetIndex === langFeedbackLinkingWidgets.length - 1}
  98. />
  99. )
  100. )}
  101. </div>
  102. </>
  103. ) : null}
  104. {hasIntegrationLinkingSection ? (
  105. <>
  106. <h3 id="project-sync" className="text-capitalize">
  107. {t('project_synchronisation')}
  108. </h3>
  109. {projectSyncSuccessMessage ? (
  110. <Alert bsStyle="success">{projectSyncSuccessMessage}</Alert>
  111. ) : null}
  112. <div className="settings-widgets-container">
  113. {allIntegrationLinkingWidgets.map(
  114. ({ import: importObject, path }, widgetIndex) => (
  115. <ModuleLinkingWidget
  116. key={Object.keys(importObject)[0]}
  117. ModuleComponent={Object.values(importObject)[0]}
  118. isLast={
  119. widgetIndex === allIntegrationLinkingWidgets.length - 1
  120. }
  121. />
  122. )
  123. )}
  124. </div>
  125. </>
  126. ) : null}
  127. {hasReferencesLinkingSection ? (
  128. <>
  129. <h3 id="references" className="text-capitalize">
  130. {t('reference_managers')}
  131. </h3>
  132. <div className="settings-widgets-container">
  133. {referenceLinkingWidgets.map(
  134. ({ import: importObject, path }, widgetIndex) => (
  135. <ModuleLinkingWidget
  136. key={Object.keys(importObject)[0]}
  137. ModuleComponent={Object.values(importObject)[0]}
  138. isLast={widgetIndex === referenceLinkingWidgets.length - 1}
  139. />
  140. )
  141. )}
  142. </div>
  143. </>
  144. ) : null}
  145. {hasSSOLinkingSection ? (
  146. <>
  147. <h3 id="linked-accounts" className="text-capitalize">
  148. {t('linked_accounts')}
  149. </h3>
  150. {ssoErrorMessage ? (
  151. <Alert bsStyle="danger">
  152. {t('sso_link_error')}: {ssoErrorMessage}
  153. </Alert>
  154. ) : null}
  155. <div className="settings-widgets-container">
  156. {Object.values(subscriptions).map(
  157. (subscription, subscriptionIndex) => (
  158. <SSOLinkingWidgetContainer
  159. key={subscription.providerId}
  160. subscription={subscription}
  161. isLast={
  162. subscriptionIndex === Object.keys(subscriptions).length - 1
  163. }
  164. />
  165. )
  166. )}
  167. </div>
  168. </>
  169. ) : null}
  170. {haslangFeedbackLinkingWidgets ||
  171. hasIntegrationLinkingSection ||
  172. hasReferencesLinkingSection ||
  173. hasSSOLinkingSection ? (
  174. <hr />
  175. ) : null}
  176. </>
  177. )
  178. }
  179. type LinkingWidgetProps = {
  180. ModuleComponent: any
  181. isLast: boolean
  182. }
  183. function ModuleLinkingWidget({ ModuleComponent, isLast }: LinkingWidgetProps) {
  184. return (
  185. <>
  186. <ModuleComponent />
  187. {isLast ? null : <hr />}
  188. </>
  189. )
  190. }
  191. type SSOLinkingWidgetContainerProps = {
  192. subscription: SSOSubscription
  193. isLast: boolean
  194. }
  195. function SSOLinkingWidgetContainer({
  196. subscription,
  197. isLast,
  198. }: SSOLinkingWidgetContainerProps) {
  199. const { t } = useTranslation()
  200. const { unlink } = useSSOContext()
  201. let description = ''
  202. switch (subscription.providerId) {
  203. case 'collabratec':
  204. description = t('linked_collabratec_description')
  205. break
  206. case 'google':
  207. description = `${t('login_with_service', {
  208. service: subscription.provider.name,
  209. })}.`
  210. break
  211. case 'twitter':
  212. description = t('login_with_service_will_stop_working_soon', {
  213. service: subscription.provider.name,
  214. })
  215. break
  216. case 'orcid':
  217. description = t('oauth_orcid_description')
  218. break
  219. }
  220. return (
  221. <>
  222. <SSOLinkingWidget
  223. providerId={subscription.providerId}
  224. title={subscription.provider.name}
  225. description={description}
  226. helpPath={subscription.provider.descriptionOptions?.link}
  227. linked={subscription.linked}
  228. linkPath={subscription.provider.linkPath}
  229. onUnlink={() => unlink(subscription.providerId)}
  230. />
  231. {isLast ? null : <hr />}
  232. </>
  233. )
  234. }
  235. export default LinkingSection