linking-section.tsx 8.2 KB

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