* Remove icons folder * Create folders for badge, button, and dropdown components * Remove Bootstrap 5 from test * Rename `getBootstrap5Breakpoint` to `getBootstrapBreakpoint` * Cleanup and update BS 5 comments * Move components to the shared folder * Rename `tooltips-bs5` to `tooltip` * Remove `-bs5` suffix * Fix path * Delete BS3 version file * Rename `_form_marketing-bootstrap-5` to `_form_marketing` * Delete BS3 version file * Rename `_contact_general_modal-marketing-bootstrap-5` to `_contact_general_modal-marketing` * Delete BS3 version file * Rename `_contact_modal-marketing-bootstrap-5` to `_contact_modal-marketing` * Delete BS3 version file * Rename `thin-footer-bootstrap-5` to `thin-footer` * Delete BS3 version file * Rename `language-picker-bootstrap-5` to `language-picker` * Rename `fat-footer-react-bootstrap-5` to `fat-footer-react` * Delete BS3 version file * Rename `navbar-marketing-bootstrap-5` to `navbar-marketing` * Rename `navbar-marketing-react-bootstrap-5` to `navbar-marketing-react` * Delete BS3 version file * Rename `layout-website-redesign-cms-bootstrap-5` to `layout-website-redesign-cms` * Source format * Fix path GitOrigin-RevId: cf0f5db7c84cf545c69213dcc271d9ff17fe5db7
146 lines
3.7 KiB
TypeScript
146 lines
3.7 KiB
TypeScript
import { ReactNode, useCallback } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import OLBadge from '@/shared/components/ol/ol-badge'
|
|
import OLTooltip from '@/shared/components/ol/ol-tooltip'
|
|
import { postJSON } from '@/infrastructure/fetch-json'
|
|
import OLButton from '@/shared/components/ol/ol-button'
|
|
import getMeta from '@/utils/meta'
|
|
|
|
type IntegrationLinkingWidgetProps = {
|
|
logo: ReactNode
|
|
title: string
|
|
description: string
|
|
helpPath?: string
|
|
labsEnabled?: boolean
|
|
experimentName: string
|
|
setErrorMessage: (message: string) => void
|
|
optedIn: boolean
|
|
setOptedIn: (optedIn: boolean) => void
|
|
}
|
|
|
|
export function LabsExperimentWidget({
|
|
logo,
|
|
title,
|
|
description,
|
|
helpPath,
|
|
labsEnabled,
|
|
experimentName,
|
|
setErrorMessage,
|
|
optedIn,
|
|
setOptedIn,
|
|
}: IntegrationLinkingWidgetProps) {
|
|
const { t } = useTranslation()
|
|
|
|
const experimentsErrorMessage = t(
|
|
'we_are_unable_to_opt_you_into_this_experiment'
|
|
)
|
|
|
|
const allowedExperiments = getMeta('ol-allowedExperiments')
|
|
const disabled = !allowedExperiments.includes(experimentName) && !optedIn
|
|
|
|
const handleEnable = useCallback(async () => {
|
|
try {
|
|
const enablePath = `/labs/participate/experiments/${experimentName}/opt-in`
|
|
await postJSON(enablePath)
|
|
setOptedIn(true)
|
|
} catch (err) {
|
|
setErrorMessage(experimentsErrorMessage)
|
|
}
|
|
}, [experimentName, setErrorMessage, experimentsErrorMessage, setOptedIn])
|
|
|
|
const handleDisable = useCallback(async () => {
|
|
try {
|
|
const disablePath = `/labs/participate/experiments/${experimentName}/opt-out`
|
|
await postJSON(disablePath)
|
|
setOptedIn(false)
|
|
} catch (err) {
|
|
setErrorMessage(experimentsErrorMessage)
|
|
}
|
|
}, [experimentName, setErrorMessage, experimentsErrorMessage, setOptedIn])
|
|
|
|
return (
|
|
<div
|
|
className={`labs-experiment-widget-container ${disabled ? 'disabled-experiment' : ''}`}
|
|
>
|
|
<div className="experiment-logo-container">{logo}</div>
|
|
<div className="description-container">
|
|
<div className="title-row">
|
|
<h3 className="h4">{title}</h3>
|
|
{optedIn && <OLBadge bg="info">{t('enabled')}</OLBadge>}
|
|
</div>
|
|
<p className="small">
|
|
{description}{' '}
|
|
{helpPath && (
|
|
<a href={helpPath} target="_blank" rel="noreferrer">
|
|
{t('learn_more')}
|
|
</a>
|
|
)}
|
|
</p>
|
|
</div>
|
|
{disabled && (
|
|
<div className="disabled-explanation">{t('experiment_full')}</div>
|
|
)}
|
|
<div>
|
|
{labsEnabled && (
|
|
<ActionButton
|
|
optedIn={optedIn}
|
|
handleDisable={handleDisable}
|
|
handleEnable={handleEnable}
|
|
disabled={disabled}
|
|
/>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
type ActionButtonProps = {
|
|
optedIn?: boolean
|
|
disabled?: boolean
|
|
handleEnable: () => void
|
|
handleDisable: () => void
|
|
}
|
|
|
|
function ActionButton({
|
|
optedIn,
|
|
disabled,
|
|
handleEnable,
|
|
handleDisable,
|
|
}: ActionButtonProps) {
|
|
const { t } = useTranslation()
|
|
|
|
if (optedIn) {
|
|
return (
|
|
<OLButton variant="secondary" onClick={handleDisable}>
|
|
{t('turn_off')}
|
|
</OLButton>
|
|
)
|
|
} else if (disabled) {
|
|
const tooltipableButton = (
|
|
<div className="d-inline-block">
|
|
<OLButton variant="primary" disabled>
|
|
{t('turn_on')}
|
|
</OLButton>
|
|
</div>
|
|
)
|
|
|
|
return (
|
|
<OLTooltip
|
|
id="experiment-disabled"
|
|
description={t('this_experiment_isnt_accepting_new_participants')}
|
|
overlayProps={{ delay: 0 }}
|
|
>
|
|
{tooltipableButton}
|
|
</OLTooltip>
|
|
)
|
|
} else {
|
|
return (
|
|
<OLButton variant="primary" onClick={handleEnable}>
|
|
{t('turn_on')}
|
|
</OLButton>
|
|
)
|
|
}
|
|
}
|
|
|
|
export default LabsExperimentWidget
|