From ff75f1bc584bb82efb9ebd5da17d72f73a31f9c0 Mon Sep 17 00:00:00 2001 From: FTCHD <144691102+FTCHD@users.noreply.github.com> Date: Wed, 13 Aug 2025 00:11:33 +0200 Subject: [PATCH] Cleanup command Signed-off-by: FTCHD <144691102+FTCHD@users.noreply.github.com> --- lib/menu.ts | 25 ++++++++++++++++++++- lib/rclone/api.ts | 15 +++++++++++++ lib/store.ts | 2 +- src/components/RemoteDefaultsDrawer.tsx | 29 +++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/lib/menu.ts b/lib/menu.ts index 1b723ee..416eed8 100644 --- a/lib/menu.ts +++ b/lib/menu.ts @@ -8,7 +8,7 @@ import { openPath } from '@tauri-apps/plugin-opener' import { platform } from '@tauri-apps/plugin-os' import { exit } from '@tauri-apps/plugin-process' import { isDirectoryEmpty } from './fs' -import { deleteRemote, mountRemote, unmountRemote } from './rclone/api' +import { cleanupRemote, deleteRemote, mountRemote, unmountRemote } from './rclone/api' import { dialogGetMountPlugin, needsMountPlugin } from './rclone/mount' import { usePersistedStore, useStore } from './store' import { getLoadingTray, getMainTray, rebuildTrayMenu } from './tray' @@ -259,6 +259,29 @@ async function parseRemotes(remotes: string[]) { submenuItems.push(browseMenuItem) } + if (!remoteConfig?.disabledActions?.includes('tray-cleanup')) { + const cleanupMenuItem = await MenuItem.new({ + id: `cleanup-${remote}`, + text: 'Cleanup', + action: async () => { + try { + await cleanupRemote(remote) + await message(`Cleanup started for ${remote}`, { + title: 'Cleanup Started', + okLabel: 'OK', + }) + } catch (error) { + await message(`Failed to start cleanup job, ${error}`, { + title: 'Cleanup Error', + kind: 'error', + okLabel: 'OK', + }) + } + }, + }) + submenuItems.push(cleanupMenuItem) + } + if (!remoteConfig?.disabledActions?.includes('tray-remove')) { const removeMenuItem = await MenuItem.new({ id: `remove-${remote}`, diff --git a/lib/rclone/api.ts b/lib/rclone/api.ts index 232317d..18b6834 100644 --- a/lib/rclone/api.ts +++ b/lib/rclone/api.ts @@ -109,6 +109,21 @@ export async function deleteRemote(remote: string) { // console.log(JSON.stringify(r, null, 2)) } +export async function cleanupRemote(remote: string) { + console.log('[cleanupRemote]', remote) + + const r = await fetch(`http://localhost:5572/operations/cleanup?fs=${remote}&_async=true`, { + method: 'POST', + headers: getAuthHeader(), + }) + + if (!r.ok) { + throw new Error('Failed to start cleanup job') + } + + // console.log(JSON.stringify(r, null, 2)) +} + export async function getMountPoints() { console.log('[getMountPoints]') diff --git a/lib/store.ts b/lib/store.ts index 67f2879..7f65aa3 100644 --- a/lib/store.ts +++ b/lib/store.ts @@ -9,7 +9,7 @@ import type { ScheduledTask } from '../types/task' const store = new LazyStore('store.json') export interface RemoteConfig { - disabledActions?: ('tray' | 'tray-mount' | 'tray-browse' | 'tray-remove')[] + disabledActions?: ('tray' | 'tray-mount' | 'tray-browse' | 'tray-remove' | 'tray-cleanup')[] defaultRemotePath?: string defaultMountPoint?: string diff --git a/src/components/RemoteDefaultsDrawer.tsx b/src/components/RemoteDefaultsDrawer.tsx index cfdb277..3dba0ff 100644 --- a/src/components/RemoteDefaultsDrawer.tsx +++ b/src/components/RemoteDefaultsDrawer.tsx @@ -255,6 +255,35 @@ export default function RemoteDefaultsDrawer({ Browse{' '} option + { + if (value) { + setConfig((prev) => ({ + ...prev, + disabledActions: + prev?.disabledActions?.filter( + (action) => + action !== 'tray-cleanup' + ), + })) + } else { + setConfig((prev) => ({ + ...prev, + disabledActions: [ + ...(prev?.disabledActions || []), + 'tray-cleanup', + ], + })) + } + }} + > + Show{' '} + Cleanup{' '} + option +