import { PermissionAction } from '@supabase/shared-types/out/constants' import { observer } from 'mobx-react-lite' import { useState } from 'react' import ConfirmationModal from 'components/ui/ConfirmationModal' import { useCheckPermissions, useStore } from 'hooks' import { isResponseOk } from 'lib/common/fetch' import Link from 'next/link' import { extensions } from 'shared-data' import { Badge, IconExternalLink, IconLoader, Modal, Toggle } from 'ui' import EnableExtensionModal from './EnableExtensionModal' interface ExtensionCardProps { extension: any } const ExtensionCard = ({ extension }: ExtensionCardProps) => { const { ui, meta } = useStore() const isOn = extension.installed_version !== null const [loading, setLoading] = useState(false) const [showConfirmEnableModal, setShowConfirmEnableModal] = useState(false) const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || '' const canUpdateExtensions = useCheckPermissions( PermissionAction.TENANT_SQL_ADMIN_WRITE, 'extensions' ) async function enableExtension() { return setShowConfirmEnableModal(true) } const [isDisableModalOpen, setIsDisableModalOpen] = useState(false) function openDisableModal() { setIsDisableModalOpen(true) } async function disableExtension() { try { setLoading(true) const response = await meta.extensions.del(extension.name) if (!isResponseOk(response)) { throw response.error } ui.setNotification({ category: 'success', message: `${extension.name.toUpperCase()} is off.`, }) setIsDisableModalOpen(false) } catch (error: any) { ui.setNotification({ category: 'error', message: `Toggle ${extension.name.toUpperCase()} failed: ${error.message}`, }) } finally { // Need to reload them because the delete function // removes the extension from the store meta.extensions.load() setLoading(false) } } return ( <>

{extension.name}

{extension?.installed_version ?? extension.default_version}

{extensions.find((item: any) => item.name === extension.name) ? ( item.name === extension.name) ?.link.startsWith('/guides') ? siteUrl === 'http://localhost:8082' ? `http://localhost:3001/docs${ extensions.find((item: any) => item.name === extension.name)?.link }` : `https://supabase.com/docs${ extensions.find((item: any) => item.name === extension.name)?.link }` : extensions.find((item: any) => item.name === extension.name)?.link ?? '' } className="max-w-[85%] cursor-default zans" target="_blank" rel="noreferrer" > ) : null}
{loading ? ( ) : ( (isOn ? openDisableModal() : enableExtension())} /> )}

{extension.comment}

{isOn && extension.schema && (
Schema: {`${extension.schema}`}
)}
setShowConfirmEnableModal(false)} /> setIsDisableModalOpen(false)} onSelectConfirm={() => { disableExtension() }} >

Are you sure you want to turn OFF "{extension.name}" extension?

) } export default observer(ExtensionCard)