Files
supabase/apps/studio/components/interfaces/Database/Backups/BackupItem.tsx
T
Alaister YoungandKevin Grüneberg 70da0f1d1d chore: cleanup packages (#27770)
* chore: cleanup packages

- Avoid circular imports
- Export API-types as types
- pg-format without depending on Node internal Buffer (not browser-compatible)
- Avoid importing from barrel files in ui dir

* chore: avoid barrel file imports in studio (#27771)

* chore: avoid barrel file imports

- Removes some unused imports
- Avoids barrel file import for faster builds + less memory

* add eslint rule

* type fixes

* delete layouts barrel

* delete components/grid barrel file

* delete components/grid/utils barrel file

* delete components/grid/components/common barrel file

* delete components/grid/components/editor barrel file

* delete components/grid/components/formatter barrel file

* delete components/grid/components/grid barrel file

* delete components/grid/components/header/filter barrel file

* remote components/grid/store barrel file

* remove components/interfaces/Auth/Policies barrel file

* delete components/interfaces/Settings/Logs barrel file

* delete components/ui/CodeEditor barrel file

* delete components/ui/Forms barrel file

* delete components/ui/Shimmers barrel file

* delete data/analytics barrel file

* delete hooks barrel file

* cleanup lib/common/fetch barrel file

* final * barral files cleanup

* global react-data-grid styles

* remove console.log

---------

Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>

* fix build

---------

Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>
2024-07-04 14:48:10 +08:00

103 lines
3.2 KiB
TypeScript

import { PermissionAction } from '@supabase/shared-types/out/constants'
import dayjs from 'dayjs'
import { Download } from 'lucide-react'
import { ButtonTooltip } from 'components/ui/ButtonTooltip'
import { useBackupDownloadMutation } from 'data/database/backup-download-mutation'
import type { DatabaseBackup } from 'data/database/backups-query'
import { useCheckPermissions } from 'hooks/misc/useCheckPermissions'
import { Badge } from 'ui'
interface BackupItemProps {
index: number
isHealthy: boolean
projectRef: string
backup: DatabaseBackup
onSelectBackup: () => void
}
const BackupItem = ({ index, isHealthy, backup, projectRef, onSelectBackup }: BackupItemProps) => {
const canTriggerScheduledBackups = useCheckPermissions(
PermissionAction.INFRA_EXECUTE,
'queue_job.restore.prepare'
)
const { mutate: downloadBackup, isLoading: 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: any) => {
if (backup.status === 'COMPLETED')
return (
<div className="flex space-x-4">
<ButtonTooltip
type="default"
disabled={!isHealthy || !canTriggerScheduledBackups}
onClick={onSelectBackup}
tooltip={{
content: {
side: 'bottom',
text: !isHealthy
? 'Cannot be restored as project is not active'
: !canTriggerScheduledBackups
? 'You need additional permissions to trigger a restore'
: undefined,
},
}}
>
Restore
</ButtonTooltip>
{!backup.isPhysicalBackup && (
<ButtonTooltip
type="default"
icon={<Download />}
loading={isDownloading}
disabled={!canTriggerScheduledBackups || isDownloading}
onClick={() => downloadBackup({ ref: projectRef, backup })}
tooltip={{
content: {
side: 'bottom',
text: !canTriggerScheduledBackups
? 'You need additional permissions to download backups'
: undefined,
},
}}
>
Download
</ButtonTooltip>
)}
</div>
)
return <Badge variant="warning">Backup In Progress...</Badge>
}
const generateBackupName = (backup: any) => {
if (backup.status == 'COMPLETED') {
return `${dayjs(backup.inserted_at).format('DD MMM YYYY HH:mm:ss')} UTC`
}
return dayjs(backup.inserted_at).format('DD MMM YYYY')
}
return (
<div
className={`flex h-12 items-center justify-between px-6 ${
index ? 'border-t border-default' : ''
}`}
>
<p className="text-sm text-foreground ">{generateBackupName(backup)}</p>
<div>{generateSideButtons(backup)}</div>
</div>
)
}
export default BackupItem