@@ -6,6 +6,7 @@ import Drive from 'remixicon-react/HardDrive2LineIcon'
|
|||||||
import MoreFill from 'remixicon-react/MoreFillIcon'
|
import MoreFill from 'remixicon-react/MoreFillIcon'
|
||||||
|
|
||||||
import { Context as FMContext } from '../../../../../providers/FileManager'
|
import { Context as FMContext } from '../../../../../providers/FileManager'
|
||||||
|
import { Context as SettingsContext } from '../../../../../providers/Settings'
|
||||||
import { useContextMenu } from '../../../hooks/useContextMenu'
|
import { useContextMenu } from '../../../hooks/useContextMenu'
|
||||||
import { handleDestroyAndForgetDrive } from '../../../utils/bee'
|
import { handleDestroyAndForgetDrive } from '../../../utils/bee'
|
||||||
import { truncateNameMiddle } from '../../../utils/common'
|
import { truncateNameMiddle } from '../../../utils/common'
|
||||||
@@ -21,6 +22,7 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function ExpiredDriveItem({ drive, onForgot, setErrorMessage }: Props): ReactElement {
|
export function ExpiredDriveItem({ drive, onForgot, setErrorMessage }: Props): ReactElement {
|
||||||
|
const { beeApi } = useContext(SettingsContext)
|
||||||
const { fm, adminDrive, setShowError } = useContext(FMContext)
|
const { fm, adminDrive, setShowError } = useContext(FMContext)
|
||||||
const [isHovered, setIsHovered] = useState(false)
|
const [isHovered, setIsHovered] = useState(false)
|
||||||
const [showForgetConfirm, setShowForgetConfirm] = useState(false)
|
const [showForgetConfirm, setShowForgetConfirm] = useState(false)
|
||||||
@@ -93,6 +95,7 @@ export function ExpiredDriveItem({ drive, onForgot, setErrorMessage }: Props): R
|
|||||||
onCancel={() => setShowForgetConfirm(false)}
|
onCancel={() => setShowForgetConfirm(false)}
|
||||||
onConfirm={async () => {
|
onConfirm={async () => {
|
||||||
await handleDestroyAndForgetDrive({
|
await handleDestroyAndForgetDrive({
|
||||||
|
beeApi,
|
||||||
fm,
|
fm,
|
||||||
drive,
|
drive,
|
||||||
isDestroy: false,
|
isDestroy: false,
|
||||||
@@ -101,9 +104,9 @@ export function ExpiredDriveItem({ drive, onForgot, setErrorMessage }: Props): R
|
|||||||
setShowForgetConfirm(false)
|
setShowForgetConfirm(false)
|
||||||
await onForgot?.()
|
await onForgot?.()
|
||||||
},
|
},
|
||||||
onError: () => {
|
onError: (err: unknown) => {
|
||||||
setShowForgetConfirm(false)
|
setShowForgetConfirm(false)
|
||||||
setErrorMessage?.(`Failed to forget drive ${drive.name}`)
|
setErrorMessage?.(`Failed to forget drive ${drive.name}: ${err}`)
|
||||||
setShowError(true)
|
setShowError(true)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -218,19 +218,19 @@ export interface DestroyDriveOptions {
|
|||||||
export const handleDestroyAndForgetDrive = async (options: DestroyDriveOptions): Promise<void> => {
|
export const handleDestroyAndForgetDrive = async (options: DestroyDriveOptions): Promise<void> => {
|
||||||
const { beeApi, fm, adminDrive, drive, isDestroy, onSuccess, onError } = { ...options }
|
const { beeApi, fm, adminDrive, drive, isDestroy, onSuccess, onError } = { ...options }
|
||||||
|
|
||||||
if (!beeApi || !fm || !fm.adminStamp || !adminDrive) {
|
if (!beeApi) {
|
||||||
onError?.('Error destroying drive: Admin Drive, Bee API or FM is invalid!')
|
onError?.('Bee API is invalid!')
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fm || !fm.adminStamp || !adminDrive) {
|
||||||
|
onError?.('FM is invalid!')
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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({
|
verifyDriveSpace({
|
||||||
fm,
|
fm,
|
||||||
driveId: drive.id.toString(),
|
driveId: drive.id.toString(),
|
||||||
@@ -243,21 +243,27 @@ export const handleDestroyAndForgetDrive = async (options: DestroyDriveOptions):
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const ttlDays = stamp.duration.toDays()
|
if (!isDestroy) {
|
||||||
|
|
||||||
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.`)
|
|
||||||
}
|
|
||||||
|
|
||||||
await fm.forgetDrive(drive)
|
await fm.forgetDrive(drive)
|
||||||
|
onSuccess?.()
|
||||||
|
|
||||||
return
|
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?.()
|
onSuccess?.()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
onError?.(e)
|
onError?.(e)
|
||||||
|
|||||||
Reference in New Issue
Block a user