From 7e05a56073a2be306a1394bf5b2e798a1a457c74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Ujv=C3=A1ri?= <58116288+bosi95@users.noreply.github.com> Date: Fri, 6 Mar 2026 11:53:03 +0100 Subject: [PATCH] fix: cannot forget expired drives (#214) * fix: forget expired drives --- .../Sidebar/DriveItem/ExpiredDriveItem.tsx | 7 +++- src/modules/filemanager/utils/bee.ts | 40 +++++++++++-------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/modules/filemanager/components/Sidebar/DriveItem/ExpiredDriveItem.tsx b/src/modules/filemanager/components/Sidebar/DriveItem/ExpiredDriveItem.tsx index 8bd479e..b90b32c 100644 --- a/src/modules/filemanager/components/Sidebar/DriveItem/ExpiredDriveItem.tsx +++ b/src/modules/filemanager/components/Sidebar/DriveItem/ExpiredDriveItem.tsx @@ -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) }, }) diff --git a/src/modules/filemanager/utils/bee.ts b/src/modules/filemanager/utils/bee.ts index 1d9ce5d..8abfdba 100644 --- a/src/modules/filemanager/utils/bee.ts +++ b/src/modules/filemanager/utils/bee.ts @@ -218,19 +218,19 @@ export interface DestroyDriveOptions { export const handleDestroyAndForgetDrive = async (options: DestroyDriveOptions): Promise => { 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)