linking-section.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 OLNotification from '@/features/ui/components/ol/ol-notification'
  9. const availableIntegrationLinkingWidgets = importOverleafModules(
  10. 'integrationLinkingWidgets'
  11. ) as any[]
  12. const availableReferenceLinkingWidgets = importOverleafModules(
  13. 'referenceLinkingWidgets'
  14. ) as any[]
  15. const availableLangFeedbackLinkingWidgets = importOverleafModules(
  16. 'langFeedbackLinkingWidgets'
  17. ) as any[]
  18. function LinkingSection() {
  19. useBroadcastUser()
  20. const { t } = useTranslation()
  21. const { subscriptions } = useSSOContext()
  22. const ssoErrorMessage = getMeta('ol-ssoErrorMessage')
  23. const cannotUseAi = getMeta('ol-cannot-use-ai')
  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. // since we only have Writefull here currently, we should hide the whole section if they cant use ai
  45. const haslangFeedbackLinkingWidgets =
  46. langFeedbackLinkingWidgets.length && !cannotUseAi
  47. const hasIntegrationLinkingSection =
  48. renderSyncSection && allIntegrationLinkingWidgets.length
  49. const hasReferencesLinkingSection = referenceLinkingWidgets.length
  50. // Filter out SSO providers that are not allowed to be linked by
  51. // managed users. Allow unlinking them if they are already linked.
  52. const hideGoogleSSO = getMeta('ol-cannot-link-google-sso')
  53. const hideOtherThirdPartySSO = getMeta('ol-cannot-link-other-third-party-sso')
  54. for (const providerId in subscriptions) {
  55. const isLinked = subscriptions[providerId].linked
  56. if (providerId === 'google') {
  57. if (hideGoogleSSO && !isLinked) {
  58. delete subscriptions[providerId]
  59. }
  60. } else {
  61. if (hideOtherThirdPartySSO && !isLinked) {
  62. delete subscriptions[providerId]
  63. }
  64. }
  65. }
  66. const hasSSOLinkingSection = Object.keys(subscriptions).length > 0
  67. if (
  68. !haslangFeedbackLinkingWidgets &&
  69. !hasIntegrationLinkingSection &&
  70. !hasReferencesLinkingSection &&
  71. !hasSSOLinkingSection
  72. ) {
  73. return null
  74. }
  75. return (
  76. <>
  77. <h3 id="integrations">{t('integrations')}</h3>
  78. <p className="small">{t('linked_accounts_explained')}</p>
  79. {haslangFeedbackLinkingWidgets ? (
  80. <>
  81. <h3 id="language-feedback" className="text-capitalize">
  82. {t('ai_features')}
  83. </h3>
  84. <div className="settings-widgets-container">
  85. {langFeedbackLinkingWidgets.map(
  86. ({ import: { default: widget }, path }, widgetIndex) => (
  87. <ModuleLinkingWidget
  88. key={path}
  89. ModuleComponent={widget}
  90. isLast={widgetIndex === langFeedbackLinkingWidgets.length - 1}
  91. />
  92. )
  93. )}
  94. </div>
  95. </>
  96. ) : null}
  97. {hasIntegrationLinkingSection ? (
  98. <>
  99. <h3 id="project-sync" className="text-capitalize">
  100. {t('project_synchronisation')}
  101. </h3>
  102. {projectSyncSuccessMessage ? (
  103. <OLNotification
  104. type="success"
  105. content={projectSyncSuccessMessage}
  106. />
  107. ) : null}
  108. <div className="settings-widgets-container">
  109. {allIntegrationLinkingWidgets.map(
  110. ({ import: importObject, path }, widgetIndex) => (
  111. <ModuleLinkingWidget
  112. key={Object.keys(importObject)[0]}
  113. ModuleComponent={Object.values(importObject)[0]}
  114. isLast={
  115. widgetIndex === allIntegrationLinkingWidgets.length - 1
  116. }
  117. />
  118. )
  119. )}
  120. </div>
  121. </>
  122. ) : null}
  123. {hasReferencesLinkingSection ? (
  124. <>
  125. <h3 id="references" className="text-capitalize">
  126. {t('reference_managers')}
  127. </h3>
  128. <div className="settings-widgets-container">
  129. {referenceLinkingWidgets.map(
  130. ({ import: importObject, path }, widgetIndex) => (
  131. <ModuleLinkingWidget
  132. key={Object.keys(importObject)[0]}
  133. ModuleComponent={Object.values(importObject)[0]}
  134. isLast={widgetIndex === referenceLinkingWidgets.length - 1}
  135. />
  136. )
  137. )}
  138. </div>
  139. </>
  140. ) : null}
  141. {hasSSOLinkingSection ? (
  142. <>
  143. <h3 id="linked-accounts" className="text-capitalize">
  144. {t('linked_accounts')}
  145. </h3>
  146. {ssoErrorMessage ? (
  147. <OLNotification
  148. type="error"
  149. content={`${t('sso_link_error')}: ${ssoErrorMessage}`}
  150. />
  151. ) : null}
  152. <div className="settings-widgets-container">
  153. {Object.values(subscriptions).map(
  154. (subscription, subscriptionIndex) => (
  155. <SSOLinkingWidgetContainer
  156. key={subscription.providerId}
  157. subscription={subscription}
  158. isLast={
  159. subscriptionIndex === Object.keys(subscriptions).length - 1
  160. }
  161. />
  162. )
  163. )}
  164. </div>
  165. </>
  166. ) : null}
  167. {haslangFeedbackLinkingWidgets ||
  168. hasIntegrationLinkingSection ||
  169. hasReferencesLinkingSection ||
  170. hasSSOLinkingSection ? (
  171. <hr />
  172. ) : null}
  173. </>
  174. )
  175. }
  176. type LinkingWidgetProps = {
  177. ModuleComponent: any
  178. isLast: boolean
  179. }
  180. function ModuleLinkingWidget({ ModuleComponent, isLast }: LinkingWidgetProps) {
  181. return (
  182. <>
  183. <ModuleComponent />
  184. {isLast ? null : <hr />}
  185. </>
  186. )
  187. }
  188. type SSOLinkingWidgetContainerProps = {
  189. subscription: SSOSubscription
  190. isLast: boolean
  191. }
  192. function SSOLinkingWidgetContainer({
  193. subscription,
  194. isLast,
  195. }: SSOLinkingWidgetContainerProps) {
  196. const { t } = useTranslation()
  197. const { unlink } = useSSOContext()
  198. let description = ''
  199. switch (subscription.providerId) {
  200. case 'collabratec':
  201. description = t('linked_collabratec_description')
  202. break
  203. case 'google':
  204. description = `${t('login_with_service', {
  205. service: subscription.provider.name,
  206. })}.`
  207. break
  208. case 'orcid':
  209. description = t('oauth_orcid_description')
  210. break
  211. }
  212. return (
  213. <>
  214. <SSOLinkingWidget
  215. providerId={subscription.providerId}
  216. title={subscription.provider.name}
  217. description={description}
  218. helpPath={subscription.provider.descriptionOptions?.link}
  219. linked={subscription.linked}
  220. linkPath={subscription.provider.linkPath}
  221. onUnlink={() => unlink(subscription.providerId)}
  222. />
  223. {isLast ? null : <hr />}
  224. </>
  225. )
  226. }
  227. export default LinkingSection