Files
supabase/apps/studio/components/interfaces/Database/Extensions/ExtensionCard.tsx
T
df52ea7ee0 feat: Replace all toasts with sonner (#28250)
* Update the design of the sonner toasts. Add the close button by default.

* Migrate studio and www apps to use the SonnerToaster.

* Migrate all toasts from studio.

* Migrate all leftover toasts in studio.

* Add a new toast component with progress. Use it in studio.

* Migrate the design-system app.

* Refactor the consent toast to use sonner.

* Switch docs to use the new sonner toasts.

* Remove toast examples from the design-system app.

* Remove all toast-related components and old code.

* Fix the progress bar in the toast progress component. Also make the bottom components vertically centered.

* Fix the width of the toast progress.

* Use text-foreground-lighter instead of muted for ToastProgress text

* Rename ToastProgress to SonnerProgress.

* Shorten the text in sonner progress.

* Use the correct classes for the close button. Add a const var for the default toast duration. Remove the custom width class from sonner.

* Set the position for all progress toasts to bottom right. Set the duration for all toasts to the default (when reusing a toast id from loading/progress toast, the duration is set to infinity).

* Fix the playwright tests.

* Refactor imports to use ui instead of @ui.

* Change all imports of react-hot-toast with sonner. These components were merged since the last commit to this branch.

* Remove react-hot-toast lib.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
2024-08-31 07:50:51 +08:00

180 lines
6.6 KiB
TypeScript

import { PermissionAction } from '@supabase/shared-types/out/constants'
import { Book, Github, Loader2, Settings } from 'lucide-react'
import Link from 'next/link'
import { useState } from 'react'
import { toast } from 'sonner'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { useDatabaseExtensionDisableMutation } from 'data/database-extensions/database-extension-disable-mutation'
import { DatabaseExtension } from 'data/database-extensions/database-extensions-query'
import { useCheckPermissions } from 'hooks/misc/useCheckPermissions'
import { extensions } from 'shared-data'
import { Button, cn, Switch } from 'ui'
import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
import EnableExtensionModal from './EnableExtensionModal'
interface ExtensionCardProps {
extension: DatabaseExtension
}
const ExtensionCard = ({ extension }: ExtensionCardProps) => {
const { project } = useProjectContext()
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || ''
const isOn = extension.installed_version !== null
const [isDisableModalOpen, setIsDisableModalOpen] = useState(false)
const [showConfirmEnableModal, setShowConfirmEnableModal] = useState(false)
const canUpdateExtensions = useCheckPermissions(
PermissionAction.TENANT_SQL_ADMIN_WRITE,
'extensions'
)
const X_PADDING = 'px-5'
const extensionMeta = extensions.find((item: any) => item.name === extension.name)
const { mutate: disableExtension, isLoading: isDisabling } = useDatabaseExtensionDisableMutation({
onSuccess: () => {
toast.success(`${extension.name} is off.`)
setIsDisableModalOpen(false)
},
})
const onConfirmDisable = () => {
if (project === undefined) return console.error('Project is required')
disableExtension({
projectRef: project.ref,
connectionString: project.connectionString,
id: extension.name,
})
}
return (
<>
<div className="bg-surface-100 border border-overlay flex flex-col overflow-hidden rounded shadow-sm">
<div className={cn('border-b border-overlay flex justify-between w-full py-3', X_PADDING)}>
<div className="max-w-[85%] flex items-center space-x-3 truncate">
<h3
title={extension.name}
className="h-5 m-0 text-sm truncate cursor-pointer text-foreground"
>
{extension.name}
</h3>
<p className="text-sm text-foreground-light font-mono tracking-tighter">
{extension?.installed_version ?? extension.default_version}
</p>
</div>
{isDisabling ? (
<Loader2 className="animate-spin" size={16} />
) : (
<Switch
disabled={!canUpdateExtensions}
checked={isOn}
onCheckedChange={() =>
isOn ? setIsDisableModalOpen(true) : setShowConfirmEnableModal(true)
}
/>
)}
</div>
{isOn && (
<div className={cn('border-b border-overlay py-2', X_PADDING)}>
<p className="text-foreground-light text-sm">
Installed in <span className="text-foreground">{extension.schema}</span> schema
</p>
</div>
)}
<div className={cn('flex h-full flex-col gap-y-3 py-3', X_PADDING)}>
<p className="text-sm text-foreground-light capitalize-sentence">{extension.comment}</p>
<div className="flex items-center gap-x-2">
{extensionMeta?.github_url && (
<Button asChild type="default" icon={<Github />} className="rounded-full">
<a
target="_blank"
rel="noreferrer"
href={extensionMeta.github_url}
className="font-mono tracking-tighter"
>
{extensionMeta.github_url.split('/').slice(-2).join('/')}
</a>
</Button>
)}
<Button asChild type="default" icon={<Book />} className="rounded-full">
<a
target="_blank"
rel="noreferrer"
className="font-mono tracking-tighter"
href={
extensionMeta?.link.startsWith('/guides')
? siteUrl === 'http://localhost:8082'
? `http://localhost:3001/docs${
extensions.find((item) => item.name === extension.name)?.link
}`
: `https://supabase.com/docs${
extensions.find((item) => item.name === extension.name)?.link
}`
: extensions.find((item: any) => item.name === extension.name)?.link ?? ''
}
>
Docs
</a>
</Button>
</div>
</div>
{extensionMeta?.product && (
<div className={cn('border-t border-overlay py-3 flex items-center gap-x-3', X_PADDING)}>
<div className="min-w-5 w-5 h-5 border border-brand/50 rounded flex items-center justify-center">
<Settings className="text-brand" size={12} />
</div>
<div>
<p className="text-foreground-light text-xs">
<span className="text-foreground">{extension.name}</span> is used by{' '}
{extensionMeta.product_url ? (
<Link
href={extensionMeta.product_url.replace('{ref}', project?.ref ?? '')}
className="transition hover:text-foreground"
>
{extensionMeta.product}
</Link>
) : (
extensionMeta.product
)}
</p>
{!isOn && (
<p className="text-foreground-lighter text-xs">
Install extension to use {extensionMeta.product}
</p>
)}
</div>
</div>
)}
</div>
<EnableExtensionModal
visible={showConfirmEnableModal}
extension={extension}
onCancel={() => setShowConfirmEnableModal(false)}
/>
<ConfirmationModal
visible={isDisableModalOpen}
title="Confirm to disable extension"
confirmLabel="Disable"
confirmLabelLoading="Disabling"
onCancel={() => setIsDisableModalOpen(false)}
onConfirm={() => onConfirmDisable()}
>
<p className="text-sm text-foreground-light">
Are you sure you want to turn OFF the "{extension.name}" extension?
</p>
</ConfirmationModal>
</>
)
}
export default ExtensionCard