@@ -6,6 +6,7 @@ import Drive from 'remixicon-react/HardDrive2LineIcon'
|
||||
import MoreFill from 'remixicon-react/MoreFillIcon'
|
||||
|
||||
import { Context as FMContext } from '../../../../../providers/FileManager'
|
||||
import { Context as SettingsContext } from '../../../../../providers/Settings'
|
||||
import { useContextMenu } from '../../../hooks/useContextMenu'
|
||||
import { handleDestroyAndForgetDrive } from '../../../utils/bee'
|
||||
import { truncateNameMiddle } from '../../../utils/common'
|
||||
@@ -21,6 +22,7 @@ interface Props {
|
||||
}
|
||||
|
||||
export function ExpiredDriveItem({ drive, onForgot, setErrorMessage }: Props): ReactElement {
|
||||
const { beeApi } = useContext(SettingsContext)
|
||||
const { fm, adminDrive, setShowError } = useContext(FMContext)
|
||||
const [isHovered, setIsHovered] = useState(false)
|
||||
const [showForgetConfirm, setShowForgetConfirm] = useState(false)
|
||||
@@ -93,6 +95,7 @@ export function ExpiredDriveItem({ drive, onForgot, setErrorMessage }: Props): R
|
||||
onCancel={() => setShowForgetConfirm(false)}
|
||||
onConfirm={async () => {
|
||||
await handleDestroyAndForgetDrive({
|
||||
beeApi,
|
||||
fm,
|
||||
drive,
|
||||
isDestroy: false,
|
||||
@@ -101,9 +104,9 @@ export function ExpiredDriveItem({ drive, onForgot, setErrorMessage }: Props): R
|
||||
setShowForgetConfirm(false)
|
||||
await onForgot?.()
|
||||
},
|
||||
onError: () => {
|
||||
onError: (err: unknown) => {
|
||||
setShowForgetConfirm(false)
|
||||
setErrorMessage?.(`Failed to forget drive ${drive.name}`)
|
||||
setErrorMessage?.(`Failed to forget drive ${drive.name}: ${err}`)
|
||||
setShowError(true)
|
||||
},
|
||||
})
|
||||
|
||||
@@ -218,19 +218,19 @@ export interface DestroyDriveOptions {
|
||||
export const handleDestroyAndForgetDrive = async (options: DestroyDriveOptions): Promise<void> => {
|
||||
const { beeApi, fm, adminDrive, drive, isDestroy, onSuccess, onError } = { ...options }
|
||||
|
||||
if (!beeApi || !fm || !fm.adminStamp || !adminDrive) {
|
||||
onError?.('Error destroying drive: Admin Drive, Bee API or FM is invalid!')
|
||||
if (!beeApi) {
|
||||
onError?.('Bee API is invalid!')
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (!fm || !fm.adminStamp || !adminDrive) {
|
||||
onError?.('FM is invalid!')
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const stamp = (await getUsableStamps(beeApi)).find(s => s.batchID.toString() === drive.batchId.toString())
|
||||
|
||||
if (!stamp) {
|
||||
throw new Error(`Postage stamp (${drive.batchId}) for the current drive (${drive.name}) not found`)
|
||||
}
|
||||
|
||||
verifyDriveSpace({
|
||||
fm,
|
||||
driveId: drive.id.toString(),
|
||||
@@ -243,21 +243,27 @@ export const handleDestroyAndForgetDrive = async (options: DestroyDriveOptions):
|
||||
},
|
||||
})
|
||||
|
||||
const ttlDays = stamp.duration.toDays()
|
||||
|
||||
if (ttlDays <= 2 || !isDestroy) {
|
||||
if (isDestroy) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn(`Stamp TTL ${ttlDays} <= 2 days, skipping drive destruction: forgetting the drive.`)
|
||||
}
|
||||
|
||||
if (!isDestroy) {
|
||||
await fm.forgetDrive(drive)
|
||||
onSuccess?.()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
await fm.destroyDrive(drive, stamp)
|
||||
const driveStamp = (await getUsableStamps(beeApi)).find(s => s.batchID.toString() === drive.batchId.toString())
|
||||
|
||||
const ttlDays = driveStamp?.duration.toDays() ?? 0
|
||||
|
||||
if (!driveStamp || ttlDays <= 2) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn(`Stamp not found or TTL ${ttlDays} <= 2 days, skipping drive destruction: forgetting the drive.`)
|
||||
await fm.forgetDrive(drive)
|
||||
onSuccess?.()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
await fm.destroyDrive(drive, driveStamp)
|
||||
onSuccess?.()
|
||||
} catch (e) {
|
||||
onError?.(e)
|
||||
|
||||
Reference in New Issue
Block a user