labs-experiments-widget.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { ReactNode, useCallback } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import OLBadge from '@/shared/components/ol/ol-badge'
  4. import { postJSON } from '@/infrastructure/fetch-json'
  5. import OLButton from '@/shared/components/ol/ol-button'
  6. import getMeta from '@/utils/meta'
  7. import Notification from '../notification'
  8. export type LabsExperimentWidgetProps = {
  9. logo: ReactNode
  10. title: string
  11. description: string | ReactNode
  12. optedInDescription?: string | ReactNode
  13. helpPath?: string
  14. labsEnabled?: boolean
  15. experimentName: string
  16. setErrorMessage: (message: string) => void
  17. optedIn: boolean
  18. setOptedIn: (optedIn: boolean) => void
  19. feedbackLink?: string
  20. }
  21. /** @knipignore */
  22. export function LabsExperimentWidget({
  23. logo,
  24. title,
  25. description,
  26. optedInDescription,
  27. helpPath,
  28. labsEnabled,
  29. experimentName,
  30. setErrorMessage,
  31. optedIn,
  32. setOptedIn,
  33. feedbackLink,
  34. }: LabsExperimentWidgetProps) {
  35. const { t } = useTranslation()
  36. const experimentsErrorMessage = t(
  37. 'we_are_unable_to_opt_you_into_this_experiment'
  38. )
  39. const allowedExperiments = getMeta('ol-allowedExperiments')
  40. const disabled = !allowedExperiments.includes(experimentName) && !optedIn
  41. const handleEnable = useCallback(async () => {
  42. try {
  43. const enablePath = `/labs/participate/experiments/${experimentName}/opt-in`
  44. await postJSON(enablePath)
  45. setOptedIn(true)
  46. } catch (err) {
  47. setErrorMessage(experimentsErrorMessage)
  48. }
  49. }, [experimentName, setErrorMessage, experimentsErrorMessage, setOptedIn])
  50. const handleDisable = useCallback(async () => {
  51. try {
  52. const disablePath = `/labs/participate/experiments/${experimentName}/opt-out`
  53. await postJSON(disablePath)
  54. setOptedIn(false)
  55. } catch (err) {
  56. setErrorMessage(experimentsErrorMessage)
  57. }
  58. }, [experimentName, setErrorMessage, experimentsErrorMessage, setOptedIn])
  59. return (
  60. <div
  61. className={`labs-experiment-widget-container ${disabled ? 'disabled-experiment' : ''}`}
  62. >
  63. <div className="experiment-logo-container">{logo}</div>
  64. <div className="description-container">
  65. <div className="title-row">
  66. <h3 className="h4">{title}</h3>
  67. {optedIn && <OLBadge bg="info">{t('enabled')}</OLBadge>}
  68. </div>
  69. <p className="small">
  70. {optedIn && optedInDescription ? optedInDescription : description}{' '}
  71. {helpPath && (
  72. <a href={helpPath} target="_blank" rel="noreferrer">
  73. {t('learn_more')}
  74. </a>
  75. )}
  76. </p>
  77. </div>
  78. <div>
  79. {optedIn && feedbackLink && (
  80. <OLButton
  81. variant="ghost"
  82. href={feedbackLink}
  83. target="_blank"
  84. rel="noreferrer"
  85. >
  86. {t('give_feedback')}
  87. </OLButton>
  88. )}
  89. </div>
  90. <div>
  91. {labsEnabled && (
  92. <ActionButton
  93. optedIn={optedIn}
  94. handleDisable={handleDisable}
  95. handleEnable={handleEnable}
  96. disabled={disabled}
  97. />
  98. )}
  99. </div>
  100. {disabled && (
  101. <>
  102. <div />
  103. <Notification
  104. type="info"
  105. content={t('experiment_full_check_back_soon')}
  106. />
  107. <div />
  108. <div />
  109. </>
  110. )}
  111. </div>
  112. )
  113. }
  114. type ActionButtonProps = {
  115. optedIn?: boolean
  116. disabled?: boolean
  117. handleEnable: () => void
  118. handleDisable: () => void
  119. }
  120. function ActionButton({
  121. optedIn,
  122. disabled,
  123. handleEnable,
  124. handleDisable,
  125. }: ActionButtonProps) {
  126. const { t } = useTranslation()
  127. if (optedIn) {
  128. return (
  129. <OLButton variant="secondary" onClick={handleDisable}>
  130. {t('disable')}
  131. </OLButton>
  132. )
  133. } else if (disabled) {
  134. return (
  135. <OLButton variant="primary" disabled>
  136. {t('enable')}
  137. </OLButton>
  138. )
  139. } else {
  140. return (
  141. <OLButton variant="primary" onClick={handleEnable}>
  142. {t('enable')}
  143. </OLButton>
  144. )
  145. }
  146. }
  147. export default LabsExperimentWidget