linking-section.tsx 8.4 KB

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