import { ReactNode, useCallback } from 'react' import { useTranslation } from 'react-i18next' import Badge from '@/shared/components/badge' import Tooltip from '@/shared/components/tooltip' import { postJSON } from '@/infrastructure/fetch-json' import { Button } from 'react-bootstrap' 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 (
{logo}

{title}

{optedIn && {t('enabled')}}

{description}{' '} {helpPath && ( {t('learn_more')} )}

{disabled && (
{t('experiment_full')}
)}
{labsEnabled && ( )}
) } type ActionButtonProps = { optedIn?: boolean disabled?: boolean handleEnable: () => void handleDisable: () => void } function ActionButton({ optedIn, disabled, handleEnable, handleDisable, }: ActionButtonProps) { const { t } = useTranslation() if (optedIn) { return ( ) } else if (disabled) { return ( ) } else { return ( ) } } export default LabsExperimentWidget