linking-section.tsx 8.2 KB

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