|
| 1 | +import { ChevronDown, Download } from 'lucide-react' |
| 2 | +import { useState } from 'react' |
| 3 | +import { toast } from 'sonner' |
| 4 | + |
| 5 | +import { useParams } from 'common' |
| 6 | +import { DropdownMenuItemTooltip } from 'components/ui/DropdownMenuItemTooltip' |
| 7 | +import { useBackupDownloadMutation } from 'data/database/backup-download-mutation' |
| 8 | +import { useProjectPauseStatusQuery } from 'data/projects/project-pause-status-query' |
| 9 | +import { useStorageArchiveCreateMutation } from 'data/storage/storage-archive-create-mutation' |
| 10 | +import { useStorageArchiveQuery } from 'data/storage/storage-archive-query' |
| 11 | +import { useFlag } from 'hooks/ui/useFlag' |
| 12 | +import { Database, Storage } from 'icons' |
| 13 | +import { PROJECT_STATUS } from 'lib/constants' |
| 14 | +import { |
| 15 | + Alert_Shadcn_, |
| 16 | + AlertDescription_Shadcn_, |
| 17 | + AlertTitle_Shadcn_, |
| 18 | + Button, |
| 19 | + DropdownMenu, |
| 20 | + DropdownMenuContent, |
| 21 | + DropdownMenuItem, |
| 22 | + DropdownMenuTrigger, |
| 23 | + WarningIcon, |
| 24 | +} from 'ui' |
| 25 | +import { useProjectContext } from '../ProjectContext' |
| 26 | + |
| 27 | +export const PauseDisabledState = () => { |
| 28 | + const { ref } = useParams() |
| 29 | + const { project } = useProjectContext() |
| 30 | + const [toastId, setToastId] = useState<string | number>() |
| 31 | + const [refetchInterval, setRefetchInterval] = useState<number | false>(false) |
| 32 | + |
| 33 | + const enforceNinetyDayUnpauseExpiry = useFlag('enforceNinetyDayUnpauseExpiry') |
| 34 | + const allowStorageObjectsDownload = useFlag('enableNinetyDayStorageDownload') |
| 35 | + |
| 36 | + const { data: pauseStatus } = useProjectPauseStatusQuery( |
| 37 | + { ref }, |
| 38 | + { |
| 39 | + enabled: project?.status === PROJECT_STATUS.INACTIVE && enforceNinetyDayUnpauseExpiry, |
| 40 | + } |
| 41 | + ) |
| 42 | + const latestBackup = pauseStatus?.latest_downloadable_backup_id |
| 43 | + |
| 44 | + const { data: storageArchive } = useStorageArchiveQuery( |
| 45 | + { projectRef: ref }, |
| 46 | + { |
| 47 | + refetchInterval, |
| 48 | + refetchOnWindowFocus: false, |
| 49 | + onSuccess: (data) => { |
| 50 | + if (data.fileUrl && refetchInterval !== false) { |
| 51 | + toast.success('Downloading storage objects', { id: toastId }) |
| 52 | + setToastId(undefined) |
| 53 | + setRefetchInterval(false) |
| 54 | + downloadStorageArchive(data.fileUrl) |
| 55 | + } |
| 56 | + }, |
| 57 | + } |
| 58 | + ) |
| 59 | + const storageArchiveUrl = storageArchive?.fileUrl |
| 60 | + |
| 61 | + const { mutate: downloadBackup } = useBackupDownloadMutation({ |
| 62 | + onSuccess: (res) => { |
| 63 | + const { fileUrl } = res |
| 64 | + |
| 65 | + // Trigger browser download by create,trigger and remove tempLink |
| 66 | + const tempLink = document.createElement('a') |
| 67 | + tempLink.href = fileUrl |
| 68 | + document.body.appendChild(tempLink) |
| 69 | + tempLink.click() |
| 70 | + document.body.removeChild(tempLink) |
| 71 | + }, |
| 72 | + }) |
| 73 | + |
| 74 | + const { mutate: createStorageArchive } = useStorageArchiveCreateMutation({ |
| 75 | + onSuccess: () => { |
| 76 | + const toastId = toast.loading( |
| 77 | + 'Retrieving storage archive. This may take a few minutes depending on the size of your storage objects.' |
| 78 | + ) |
| 79 | + setToastId(toastId) |
| 80 | + setRefetchInterval(5000) |
| 81 | + }, |
| 82 | + }) |
| 83 | + |
| 84 | + const onSelectDownloadBackup = () => { |
| 85 | + if (ref === undefined) return console.error('Project ref is required') |
| 86 | + if (!latestBackup) return toast.error('No backups available for download') |
| 87 | + |
| 88 | + const toastId = toast.loading('Fetching database backup') |
| 89 | + |
| 90 | + downloadBackup( |
| 91 | + { |
| 92 | + ref, |
| 93 | + backup: { |
| 94 | + id: latestBackup, |
| 95 | + // [Joshen] Just FYI these params aren't required for the download backup request |
| 96 | + // API types need to be updated |
| 97 | + project_id: -1, |
| 98 | + inserted_at: '', |
| 99 | + isPhysicalBackup: false, |
| 100 | + status: {}, |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + onSuccess: () => { |
| 105 | + toast.success('Downloading database backup', { id: toastId }) |
| 106 | + }, |
| 107 | + } |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + const downloadStorageArchive = (url: string) => { |
| 112 | + const tempLink = document.createElement('a') |
| 113 | + tempLink.href = url |
| 114 | + document.body.appendChild(tempLink) |
| 115 | + tempLink.click() |
| 116 | + document.body.removeChild(tempLink) |
| 117 | + } |
| 118 | + |
| 119 | + const onSelectDownloadStorageArchive = () => { |
| 120 | + if (!storageArchiveUrl) { |
| 121 | + createStorageArchive({ projectRef: ref }) |
| 122 | + } else { |
| 123 | + toast.success('Downloading storage objects') |
| 124 | + downloadStorageArchive(storageArchiveUrl) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return ( |
| 129 | + <Alert_Shadcn_ variant="warning"> |
| 130 | + <WarningIcon /> |
| 131 | + <AlertTitle_Shadcn_>Project cannot be restored through the dashboard</AlertTitle_Shadcn_> |
| 132 | + <AlertDescription_Shadcn_> |
| 133 | + This project has been paused for over{' '} |
| 134 | + <span className="text-foreground"> |
| 135 | + {pauseStatus?.max_days_till_restore_disabled ?? 90} days |
| 136 | + </span>{' '} |
| 137 | + and cannot be restored through the dashboard. However, your data remains intact and can be |
| 138 | + downloaded as a backup. |
| 139 | + </AlertDescription_Shadcn_> |
| 140 | + <AlertDescription_Shadcn_ className="flex items-center gap-x-2 mt-3"> |
| 141 | + <DropdownMenu> |
| 142 | + <DropdownMenuTrigger asChild> |
| 143 | + <Button type="default" icon={<Download />} iconRight={<ChevronDown />}> |
| 144 | + Download backup |
| 145 | + </Button> |
| 146 | + </DropdownMenuTrigger> |
| 147 | + <DropdownMenuContent className="w-56" align="start"> |
| 148 | + <DropdownMenuItemTooltip |
| 149 | + className="gap-x-2" |
| 150 | + disabled={!latestBackup} |
| 151 | + onClick={() => onSelectDownloadBackup()} |
| 152 | + tooltip={{ |
| 153 | + content: { |
| 154 | + side: 'right', |
| 155 | + text: 'No backups available, please reach out via support for assistance', |
| 156 | + }, |
| 157 | + }} |
| 158 | + > |
| 159 | + <Database size={16} /> |
| 160 | + Download database backup |
| 161 | + </DropdownMenuItemTooltip> |
| 162 | + <DropdownMenuItemTooltip |
| 163 | + className="gap-x-2" |
| 164 | + disabled={!allowStorageObjectsDownload} |
| 165 | + onClick={() => onSelectDownloadStorageArchive()} |
| 166 | + tooltip={{ |
| 167 | + content: { |
| 168 | + side: 'right', |
| 169 | + text: 'This feature is not available yet, please reach out to support for assistance', |
| 170 | + }, |
| 171 | + }} |
| 172 | + > |
| 173 | + <Storage size={16} /> |
| 174 | + Download storage objects |
| 175 | + </DropdownMenuItemTooltip> |
| 176 | + {/* [Joshen] Once storage object download is supported, can just use the below component */} |
| 177 | + {/* <DropdownMenuItem className="gap-x-2" onClick={() => onSelectDownloadStorageArchive()}> |
| 178 | + <Storage size={16} /> |
| 179 | + Download storage objects |
| 180 | + </DropdownMenuItem> */} |
| 181 | + </DropdownMenuContent> |
| 182 | + </DropdownMenu> |
| 183 | + </AlertDescription_Shadcn_> |
| 184 | + </Alert_Shadcn_> |
| 185 | + ) |
| 186 | +} |
0 commit comments