import { PermissionAction } from '@supabase/shared-types/out/constants' import { useParams } from 'common' import { Download } from 'lucide-react' import { Badge, Tooltip, TooltipContent, TooltipTrigger } from 'ui' import { TimestampInfo } from 'ui-patterns' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' import { InlineLink } from '@/components/ui/InlineLink' import { useBackupDownloadMutation } from '@/data/database/backup-download-mutation' import type { DatabaseBackup } from '@/data/database/backups-query' import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions' interface BackupItemProps { index: number isHealthy: boolean backup: DatabaseBackup onSelectBackup: () => void } export const BackupItem = ({ index, isHealthy, backup, onSelectBackup }: BackupItemProps) => { const { ref: projectRef } = useParams() const { can: canTriggerScheduledBackups } = useAsyncCheckPermissions( PermissionAction.INFRA_EXECUTE, 'queue_job.restore.prepare' ) const { mutate: downloadBackup, isPending: isDownloading } = useBackupDownloadMutation({ onSuccess: (res) => { const { fileUrl } = res // Trigger browser download by create,trigger and remove tempLink const tempLink = document.createElement('a') tempLink.href = fileUrl document.body.appendChild(tempLink) tempLink.click() document.body.removeChild(tempLink) }, }) const generateSideButtons = (backup: DatabaseBackup) => { if (backup.status === 'COMPLETED') return (
Restore {!backup.isPhysicalBackup && ( } loading={isDownloading} disabled={!canTriggerScheduledBackups || isDownloading} onClick={() => { if (!projectRef) return console.error('Project ref is required') downloadBackup({ ref: projectRef, backup }) }} tooltip={{ content: { side: 'bottom', text: !canTriggerScheduledBackups ? 'You need additional permissions to download backups' : undefined, }, }} > Download )}
) return Backup In Progress... } return (
{backup.isPhysicalBackup ? 'Physical' : 'Logical'} {backup.isPhysicalBackup ? 'File-level backups of your entire database.' : 'SQL-based backups of your entire database.'}{' '} Learn more
{generateSideButtons(backup)}
) }