linking-section.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 '@/shared/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. {haslangFeedbackLinkingWidgets ? (
  78. <>
  79. <h3 id="language-feedback">{t('ai_features')}</h3>
  80. {langFeedbackLinkingWidgets.map(
  81. ({ import: { default: widget }, path }, widgetIndex) => (
  82. <ModuleLinkingWidget
  83. key={path}
  84. ModuleComponent={widget}
  85. isLast={widgetIndex === langFeedbackLinkingWidgets.length - 1}
  86. />
  87. )
  88. )}
  89. </>
  90. ) : null}
  91. {hasIntegrationLinkingSection ? (
  92. <>
  93. <h3 id="project-sync">{t('project_synchronisation')}</h3>
  94. {projectSyncSuccessMessage ? (
  95. <OLNotification
  96. type="success"
  97. content={projectSyncSuccessMessage}
  98. />
  99. ) : null}
  100. <div className="settings-widgets-container">
  101. {allIntegrationLinkingWidgets.map(
  102. ({ import: importObject }, widgetIndex) => (
  103. <ModuleLinkingWidget
  104. key={Object.keys(importObject)[0]}
  105. ModuleComponent={Object.values(importObject)[0]}
  106. isLast={
  107. widgetIndex === allIntegrationLinkingWidgets.length - 1
  108. }
  109. />
  110. )
  111. )}
  112. </div>
  113. </>
  114. ) : null}
  115. {hasReferencesLinkingSection ? (
  116. <>
  117. <h3 id="references">{t('reference_managers')}</h3>
  118. <div className="settings-widgets-container">
  119. {referenceLinkingWidgets.map(
  120. ({ import: importObject }, widgetIndex) => (
  121. <ModuleLinkingWidget
  122. key={Object.keys(importObject)[0]}
  123. ModuleComponent={Object.values(importObject)[0]}
  124. isLast={widgetIndex === referenceLinkingWidgets.length - 1}
  125. />
  126. )
  127. )}
  128. </div>
  129. </>
  130. ) : null}
  131. {hasSSOLinkingSection ? (
  132. <>
  133. <h3 id="linked-accounts">{t('linked_accounts')}</h3>
  134. {ssoErrorMessage ? (
  135. <OLNotification
  136. type="error"
  137. content={`${t('sso_link_error')}: ${ssoErrorMessage}`}
  138. />
  139. ) : null}
  140. <div className="settings-widgets-container">
  141. {Object.values(subscriptions).map(
  142. (subscription, subscriptionIndex) => (
  143. <SSOLinkingWidgetContainer
  144. key={subscription.providerId}
  145. subscription={subscription}
  146. isLast={
  147. subscriptionIndex === Object.keys(subscriptions).length - 1
  148. }
  149. />
  150. )
  151. )}
  152. </div>
  153. </>
  154. ) : null}
  155. {haslangFeedbackLinkingWidgets ||
  156. hasIntegrationLinkingSection ||
  157. hasReferencesLinkingSection ||
  158. hasSSOLinkingSection ? (
  159. <hr />
  160. ) : null}
  161. </>
  162. )
  163. }
  164. type LinkingWidgetProps = {
  165. ModuleComponent: any
  166. isLast: boolean
  167. }
  168. function ModuleLinkingWidget({ ModuleComponent, isLast }: LinkingWidgetProps) {
  169. return (
  170. <>
  171. <ModuleComponent />
  172. {isLast ? null : <hr />}
  173. </>
  174. )
  175. }
  176. type SSOLinkingWidgetContainerProps = {
  177. subscription: SSOSubscription
  178. isLast: boolean
  179. }
  180. function SSOLinkingWidgetContainer({
  181. subscription,
  182. isLast,
  183. }: SSOLinkingWidgetContainerProps) {
  184. const { t } = useTranslation()
  185. const { unlink } = useSSOContext()
  186. let description = ''
  187. switch (subscription.providerId) {
  188. case 'collabratec':
  189. description = t('linked_collabratec_description')
  190. break
  191. case 'google':
  192. description = `${t('login_with_service', {
  193. service: subscription.provider.name,
  194. })}.`
  195. break
  196. case 'orcid':
  197. description = t('oauth_orcid_description')
  198. break
  199. }
  200. return (
  201. <>
  202. <SSOLinkingWidget
  203. providerId={subscription.providerId}
  204. title={subscription.provider.name}
  205. description={description}
  206. helpPath={subscription.provider.descriptionOptions?.link}
  207. linked={subscription.linked}
  208. linkPath={subscription.provider.linkPath}
  209. onUnlink={() => unlink(subscription.providerId)}
  210. />
  211. {isLast ? null : <hr />}
  212. </>
  213. )
  214. }
  215. export default LinkingSection