linking-section.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { useState, ElementType } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
  4. import { useSSOContext, SSOSubscription } from '../context/sso-context'
  5. import { SSOLinkingWidget } from './linking/sso-widget'
  6. import getMeta from '../../../utils/meta'
  7. import { useBroadcastUser } from '@/shared/hooks/user-channel/use-broadcast-user'
  8. import { useFeatureFlag } from '@/shared/context/split-test-context'
  9. import OLNotification from '@/features/ui/components/ol/ol-notification'
  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 hasWritefullOauthPromotion = useFeatureFlag('writefull-oauth-promotion')
  49. // even if they arent in the split test, if they have it enabled let them toggle it off
  50. const user = getMeta('ol-user')
  51. const shouldLoadWritefull =
  52. (hasWritefullOauthPromotion || user.writefull?.enabled === true) &&
  53. !window.writefull // check if the writefull extension is installed, in which case we dont handle the integration
  54. const haslangFeedbackLinkingWidgets =
  55. langFeedbackLinkingWidgets.length && shouldLoadWritefull
  56. const hasIntegrationLinkingSection =
  57. renderSyncSection && allIntegrationLinkingWidgets.length
  58. const hasReferencesLinkingSection = referenceLinkingWidgets.length
  59. // Filter out SSO providers that are not allowed to be linked by
  60. // managed users. Allow unlinking them if they are already linked.
  61. const hideGoogleSSO = getMeta('ol-cannot-link-google-sso')
  62. const hideOtherThirdPartySSO = getMeta('ol-cannot-link-other-third-party-sso')
  63. for (const providerId in subscriptions) {
  64. const isLinked = subscriptions[providerId].linked
  65. if (providerId === 'google') {
  66. if (hideGoogleSSO && !isLinked) {
  67. delete subscriptions[providerId]
  68. }
  69. } else {
  70. if (hideOtherThirdPartySSO && !isLinked) {
  71. delete subscriptions[providerId]
  72. }
  73. }
  74. }
  75. const hasSSOLinkingSection = Object.keys(subscriptions).length > 0
  76. if (
  77. !haslangFeedbackLinkingWidgets &&
  78. !hasIntegrationLinkingSection &&
  79. !hasReferencesLinkingSection &&
  80. !hasSSOLinkingSection
  81. ) {
  82. return null
  83. }
  84. return (
  85. <>
  86. <h3>{t('integrations')}</h3>
  87. <p className="small">{t('linked_accounts_explained')}</p>
  88. {haslangFeedbackLinkingWidgets ? (
  89. <>
  90. <h3 id="project-sync" className="text-capitalize">
  91. {t('language_feedback')}
  92. </h3>
  93. <div className="settings-widgets-container">
  94. {langFeedbackLinkingWidgets.map(
  95. ({ import: { default: widget }, path }, widgetIndex) => (
  96. <ModuleLinkingWidget
  97. key={path}
  98. ModuleComponent={widget}
  99. isLast={widgetIndex === langFeedbackLinkingWidgets.length - 1}
  100. />
  101. )
  102. )}
  103. </div>
  104. </>
  105. ) : null}
  106. {hasIntegrationLinkingSection ? (
  107. <>
  108. <h3 id="project-sync" className="text-capitalize">
  109. {t('project_synchronisation')}
  110. </h3>
  111. {projectSyncSuccessMessage ? (
  112. <OLNotification
  113. type="success"
  114. content={projectSyncSuccessMessage}
  115. />
  116. ) : null}
  117. <div className="settings-widgets-container">
  118. {allIntegrationLinkingWidgets.map(
  119. ({ import: importObject, path }, widgetIndex) => (
  120. <ModuleLinkingWidget
  121. key={Object.keys(importObject)[0]}
  122. ModuleComponent={Object.values(importObject)[0]}
  123. isLast={
  124. widgetIndex === allIntegrationLinkingWidgets.length - 1
  125. }
  126. />
  127. )
  128. )}
  129. </div>
  130. </>
  131. ) : null}
  132. {hasReferencesLinkingSection ? (
  133. <>
  134. <h3 id="references" className="text-capitalize">
  135. {t('reference_managers')}
  136. </h3>
  137. <div className="settings-widgets-container">
  138. {referenceLinkingWidgets.map(
  139. ({ import: importObject, path }, widgetIndex) => (
  140. <ModuleLinkingWidget
  141. key={Object.keys(importObject)[0]}
  142. ModuleComponent={Object.values(importObject)[0]}
  143. isLast={widgetIndex === referenceLinkingWidgets.length - 1}
  144. />
  145. )
  146. )}
  147. </div>
  148. </>
  149. ) : null}
  150. {hasSSOLinkingSection ? (
  151. <>
  152. <h3 id="linked-accounts" className="text-capitalize">
  153. {t('linked_accounts')}
  154. </h3>
  155. {ssoErrorMessage ? (
  156. <OLNotification
  157. type="error"
  158. content={`${t('sso_link_error')}: ${ssoErrorMessage}`}
  159. />
  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