linking-section.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { 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. const availableIntegrationLinkingWidgets = importOverleafModules(
  11. 'integrationLinkingWidgets'
  12. ) as any[]
  13. const availableReferenceLinkingWidgets = importOverleafModules(
  14. 'referenceLinkingWidgets'
  15. ) as any[]
  16. const availableLangFeedbackLinkingWidgets = importOverleafModules(
  17. 'langFeedbackLinkingWidgets'
  18. ) as any[]
  19. function LinkingSection() {
  20. useBroadcastUser()
  21. const { t } = useTranslation()
  22. const { subscriptions } = useSSOContext()
  23. const ssoErrorMessage = getMeta('ol-ssoErrorMessage')
  24. const projectSyncSuccessMessage = getMeta('ol-projectSyncSuccessMessage')
  25. // hide linking widgets in CI
  26. const integrationLinkingWidgets = getMeta('ol-hideLinkingWidgets')
  27. ? []
  28. : availableIntegrationLinkingWidgets
  29. const referenceLinkingWidgets = getMeta('ol-hideLinkingWidgets')
  30. ? []
  31. : availableReferenceLinkingWidgets
  32. const langFeedbackLinkingWidgets = getMeta('ol-hideLinkingWidgets')
  33. ? []
  34. : availableLangFeedbackLinkingWidgets
  35. const oauth2ServerComponents = importOverleafModules('oauth2Server') as {
  36. import: { default: ElementType }
  37. path: string
  38. }[]
  39. const renderSyncSection =
  40. getMeta('ol-isSaas') || getMeta('ol-gitBridgeEnabled')
  41. const allIntegrationLinkingWidgets = integrationLinkingWidgets.concat(
  42. oauth2ServerComponents
  43. )
  44. // currently the only thing that is in the langFeedback section is writefull,
  45. // which is behind a split test. we should hide this section if the user is not in the split test
  46. // todo: remove split test check, and split test context after gradual rollout is complete
  47. const hasWritefullOauthPromotion = useFeatureFlag('writefull-oauth-promotion')
  48. // even if they arent in the split test, if they have it enabled let them toggle it off
  49. const user = getMeta('ol-user')
  50. const shouldLoadWritefull =
  51. (hasWritefullOauthPromotion || user.writefull?.enabled === true) &&
  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 =
  56. renderSyncSection && allIntegrationLinkingWidgets.length
  57. const hasReferencesLinkingSection = referenceLinkingWidgets.length
  58. // Filter out SSO providers that are not allowed to be linked by
  59. // managed users. Allow unlinking them if they are already linked.
  60. const hideGoogleSSO = getMeta('ol-cannot-link-google-sso')
  61. const hideOtherThirdPartySSO = getMeta('ol-cannot-link-other-third-party-sso')
  62. for (const providerId in subscriptions) {
  63. const isLinked = subscriptions[providerId].linked
  64. if (providerId === 'google') {
  65. if (hideGoogleSSO && !isLinked) {
  66. delete subscriptions[providerId]
  67. }
  68. } else {
  69. if (hideOtherThirdPartySSO && !isLinked) {
  70. delete subscriptions[providerId]
  71. }
  72. }
  73. }
  74. const hasSSOLinkingSection = Object.keys(subscriptions).length > 0
  75. if (
  76. !haslangFeedbackLinkingWidgets &&
  77. !hasIntegrationLinkingSection &&
  78. !hasReferencesLinkingSection &&
  79. !hasSSOLinkingSection
  80. ) {
  81. return null
  82. }
  83. return (
  84. <>
  85. <h3 id="integrations">{t('integrations')}</h3>
  86. <p className="small">{t('linked_accounts_explained')}</p>
  87. {haslangFeedbackLinkingWidgets ? (
  88. <>
  89. <h3 id="language-feedback" className="text-capitalize">
  90. {t('language_feedback')}
  91. </h3>
  92. <div className="settings-widgets-container">
  93. {langFeedbackLinkingWidgets.map(
  94. ({ import: { default: widget }, path }, widgetIndex) => (
  95. <ModuleLinkingWidget
  96. key={path}
  97. ModuleComponent={widget}
  98. isLast={widgetIndex === langFeedbackLinkingWidgets.length - 1}
  99. />
  100. )
  101. )}
  102. </div>
  103. </>
  104. ) : null}
  105. {hasIntegrationLinkingSection ? (
  106. <>
  107. <h3 id="project-sync" className="text-capitalize">
  108. {t('project_synchronisation')}
  109. </h3>
  110. {projectSyncSuccessMessage ? (
  111. <OLNotification
  112. type="success"
  113. content={projectSyncSuccessMessage}
  114. />
  115. ) : null}
  116. <div className="settings-widgets-container">
  117. {allIntegrationLinkingWidgets.map(
  118. ({ import: importObject, path }, widgetIndex) => (
  119. <ModuleLinkingWidget
  120. key={Object.keys(importObject)[0]}
  121. ModuleComponent={Object.values(importObject)[0]}
  122. isLast={
  123. widgetIndex === allIntegrationLinkingWidgets.length - 1
  124. }
  125. />
  126. )
  127. )}
  128. </div>
  129. </>
  130. ) : null}
  131. {hasReferencesLinkingSection ? (
  132. <>
  133. <h3 id="references" className="text-capitalize">
  134. {t('reference_managers')}
  135. </h3>
  136. <div className="settings-widgets-container">
  137. {referenceLinkingWidgets.map(
  138. ({ import: importObject, path }, widgetIndex) => (
  139. <ModuleLinkingWidget
  140. key={Object.keys(importObject)[0]}
  141. ModuleComponent={Object.values(importObject)[0]}
  142. isLast={widgetIndex === referenceLinkingWidgets.length - 1}
  143. />
  144. )
  145. )}
  146. </div>
  147. </>
  148. ) : null}
  149. {hasSSOLinkingSection ? (
  150. <>
  151. <h3 id="linked-accounts" className="text-capitalize">
  152. {t('linked_accounts')}
  153. </h3>
  154. {ssoErrorMessage ? (
  155. <OLNotification
  156. type="error"
  157. content={`${t('sso_link_error')}: ${ssoErrorMessage}`}
  158. />
  159. ) : null}
  160. <div className="settings-widgets-container">
  161. {Object.values(subscriptions).map(
  162. (subscription, subscriptionIndex) => (
  163. <SSOLinkingWidgetContainer
  164. key={subscription.providerId}
  165. subscription={subscription}
  166. isLast={
  167. subscriptionIndex === Object.keys(subscriptions).length - 1
  168. }
  169. />
  170. )
  171. )}
  172. </div>
  173. </>
  174. ) : null}
  175. {haslangFeedbackLinkingWidgets ||
  176. hasIntegrationLinkingSection ||
  177. hasReferencesLinkingSection ||
  178. hasSSOLinkingSection ? (
  179. <hr />
  180. ) : null}
  181. </>
  182. )
  183. }
  184. type LinkingWidgetProps = {
  185. ModuleComponent: any
  186. isLast: boolean
  187. }
  188. function ModuleLinkingWidget({ ModuleComponent, isLast }: LinkingWidgetProps) {
  189. return (
  190. <>
  191. <ModuleComponent />
  192. {isLast ? null : <hr />}
  193. </>
  194. )
  195. }
  196. type SSOLinkingWidgetContainerProps = {
  197. subscription: SSOSubscription
  198. isLast: boolean
  199. }
  200. function SSOLinkingWidgetContainer({
  201. subscription,
  202. isLast,
  203. }: SSOLinkingWidgetContainerProps) {
  204. const { t } = useTranslation()
  205. const { unlink } = useSSOContext()
  206. let description = ''
  207. switch (subscription.providerId) {
  208. case 'collabratec':
  209. description = t('linked_collabratec_description')
  210. break
  211. case 'google':
  212. description = `${t('login_with_service', {
  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