linking-section.tsx 7.4 KB

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