Delete command
Signed-off-by: FTCHD <144691102+FTCHD@users.noreply.github.com>
This commit is contained in:
+14
@@ -396,6 +396,20 @@ export async function buildMenu() {
|
||||
menuItems.push(syncMenuItem)
|
||||
}
|
||||
|
||||
if (!persistedStoreState.disabledActions?.includes('tray-delete')) {
|
||||
const deleteMenuItem = await MenuItem.new({
|
||||
id: 'delete',
|
||||
text: 'Delete',
|
||||
action: async () => {
|
||||
await openWindow({
|
||||
name: 'Delete',
|
||||
url: '/delete',
|
||||
})
|
||||
},
|
||||
})
|
||||
menuItems.push(deleteMenuItem)
|
||||
}
|
||||
|
||||
await PredefinedMenuItem.new({
|
||||
item: 'Separator',
|
||||
}).then((item) => {
|
||||
|
||||
@@ -516,6 +516,42 @@ export async function startSync({
|
||||
}
|
||||
}
|
||||
|
||||
export async function startDelete({
|
||||
fs,
|
||||
rmDirs,
|
||||
_filter,
|
||||
}: {
|
||||
fs: string
|
||||
rmDirs?: boolean // delete empty src directories if set
|
||||
_filter?: Record<string, string | number | boolean | string[]>
|
||||
}) {
|
||||
console.log('[startDelete]', fs, rmDirs)
|
||||
|
||||
const params = new URLSearchParams()
|
||||
params.set('fs', fs)
|
||||
|
||||
if (rmDirs) {
|
||||
params.set('rmDirs', 'true')
|
||||
}
|
||||
|
||||
params.set('_async', 'true')
|
||||
|
||||
if (_filter && Object.keys(_filter).length > 0) {
|
||||
params.set('_filter', JSON.stringify(_filter))
|
||||
}
|
||||
|
||||
const r = await fetch(`http://localhost:5572/operations/delete?${params.toString()}`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeader(),
|
||||
})
|
||||
|
||||
console.log('[startDelete] operation started:', r)
|
||||
|
||||
if (!r.ok) {
|
||||
throw new Error('Failed to start delete job')
|
||||
}
|
||||
}
|
||||
|
||||
/* FLAGS */
|
||||
|
||||
export async function getGlobalFlags() {
|
||||
|
||||
@@ -53,6 +53,7 @@ interface PersistedState {
|
||||
| 'tray-copy'
|
||||
| 'tray-serve'
|
||||
| 'tray-move'
|
||||
| 'tray-delete'
|
||||
)[]
|
||||
setDisabledActions: (
|
||||
actions: (
|
||||
@@ -61,6 +62,7 @@ interface PersistedState {
|
||||
| 'tray-copy'
|
||||
| 'tray-serve'
|
||||
| 'tray-move'
|
||||
| 'tray-delete'
|
||||
)[]
|
||||
) => void
|
||||
|
||||
@@ -162,6 +164,7 @@ export const usePersistedStore = create<PersistedState>()(
|
||||
| 'tray-copy'
|
||||
| 'tray-serve'
|
||||
| 'tray-move'
|
||||
| 'tray-delete'
|
||||
)[]
|
||||
) => set((_) => ({ disabledActions: actions })),
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
listRemotes,
|
||||
mountRemote,
|
||||
startCopy,
|
||||
startDelete,
|
||||
startMove,
|
||||
startSync,
|
||||
unmountAllRemotes,
|
||||
@@ -322,10 +323,17 @@ async function handleTask(task: ScheduledTask) {
|
||||
_filter,
|
||||
createEmptySrcDirs,
|
||||
deleteEmptyDstDirs,
|
||||
fs,
|
||||
rmDirs,
|
||||
} = task.args
|
||||
|
||||
switch (task.type) {
|
||||
case 'delete':
|
||||
await startDelete({
|
||||
fs,
|
||||
rmDirs,
|
||||
_filter,
|
||||
})
|
||||
break
|
||||
case 'copy':
|
||||
await startCopy({
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
"Move",
|
||||
"Sync",
|
||||
"Mount",
|
||||
"Delete",
|
||||
"Settings",
|
||||
"Jobs",
|
||||
"Cron",
|
||||
|
||||
@@ -7,6 +7,7 @@ import { HeroUIProvider } from '@heroui/react'
|
||||
import { debug, error, info, trace, warn } from '@tauri-apps/plugin-log'
|
||||
import Copy from './pages/Copy'
|
||||
import Cron from './pages/Cron'
|
||||
import Delete from './pages/Delete'
|
||||
import Jobs from './pages/Jobs'
|
||||
import Mount from './pages/Mount'
|
||||
import Move from './pages/Move'
|
||||
@@ -54,6 +55,10 @@ const router = createBrowserRouter([
|
||||
path: '/move',
|
||||
element: <Move />,
|
||||
},
|
||||
{
|
||||
path: '/delete',
|
||||
element: <Delete />,
|
||||
},
|
||||
{
|
||||
path: '/mount',
|
||||
element: <Mount />,
|
||||
|
||||
@@ -0,0 +1,254 @@
|
||||
import { Accordion, AccordionItem, Avatar, Button } from '@heroui/react'
|
||||
import { message } from '@tauri-apps/plugin-dialog'
|
||||
import {} from '@tauri-apps/plugin-fs'
|
||||
import cronstrue from 'cronstrue'
|
||||
import { AlertOctagonIcon, ClockIcon, FilterIcon, FoldersIcon, PlayIcon } from 'lucide-react'
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react'
|
||||
import { useSearchParams } from 'react-router-dom'
|
||||
import { getRemoteName } from '../../lib/format'
|
||||
import { getFilterFlags, getGlobalFlags, startDelete } from '../../lib/rclone/api'
|
||||
import { usePersistedStore } from '../../lib/store'
|
||||
import CronEditor from '../components/CronEditor'
|
||||
import OptionsSection from '../components/OptionsSection'
|
||||
import { PathField } from '../components/PathFinder'
|
||||
|
||||
export default function Delete() {
|
||||
const [searchParams] = useSearchParams()
|
||||
|
||||
const [sourceFs, setSourceFs] = useState<string | undefined>(
|
||||
searchParams.get('initialSource') ? searchParams.get('initialSource')! : undefined
|
||||
)
|
||||
const [rmDirs, setRmDirs] = useState(false)
|
||||
|
||||
const [cronExpression, setCronExpression] = useState<string | null>(null)
|
||||
|
||||
const [isStarted, setIsStarted] = useState(false)
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const [jsonError, setJsonError] = useState<'filter' | null>(null)
|
||||
|
||||
const [filterOptionsLocked, setFilterOptionsLocked] = useState(false)
|
||||
const [filterOptions, setFilterOptions] = useState<Record<string, string>>({})
|
||||
const [filterOptionsJson, setFilterOptionsJson] = useState<string>('{}')
|
||||
|
||||
const [globalOptions, setGlobalOptions] = useState<any[]>([])
|
||||
|
||||
// biome-ignore lint/correctness/useExhaustiveDependencies: when unlocking, we don't want to re-run the effect
|
||||
useEffect(() => {
|
||||
const storeData = usePersistedStore.getState()
|
||||
|
||||
const sourceRemote = getRemoteName(sourceFs?.[0])
|
||||
|
||||
let mergedFilterDefaults = {}
|
||||
|
||||
// Helper function to merge defaults from a remote
|
||||
const mergeRemoteDefaults = (remote: string | null) => {
|
||||
if (!remote || !(remote in storeData.remoteConfigList)) return
|
||||
|
||||
const remoteConfig = storeData.remoteConfigList[remote]
|
||||
|
||||
if (remoteConfig.filterDefaults) {
|
||||
mergedFilterDefaults = {
|
||||
...mergedFilterDefaults,
|
||||
...remoteConfig.filterDefaults,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Only merge defaults for remote paths
|
||||
if (sourceRemote) mergeRemoteDefaults(sourceRemote)
|
||||
|
||||
if (Object.keys(mergedFilterDefaults).length > 0 && !filterOptionsLocked) {
|
||||
setFilterOptionsJson(JSON.stringify(mergedFilterDefaults, null, 2))
|
||||
}
|
||||
}, [sourceFs])
|
||||
|
||||
useEffect(() => {
|
||||
getGlobalFlags().then((flags) => setGlobalOptions(flags))
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
setFilterOptions(JSON.parse(filterOptionsJson))
|
||||
|
||||
setJsonError(null)
|
||||
} catch (error) {
|
||||
setJsonError('filter')
|
||||
console.error('Error parsing filter options:', error)
|
||||
}
|
||||
}, [filterOptionsJson])
|
||||
|
||||
const handleStartDelete = useCallback(async () => {
|
||||
setIsLoading(true)
|
||||
|
||||
if (!sourceFs) {
|
||||
await message('Please select a source path', {
|
||||
title: 'Error',
|
||||
kind: 'error',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (cronExpression) {
|
||||
try {
|
||||
cronstrue.toString(cronExpression)
|
||||
} catch {
|
||||
await message('Invalid cron expression', {
|
||||
title: 'Error',
|
||||
kind: 'error',
|
||||
})
|
||||
setIsLoading(false)
|
||||
return
|
||||
}
|
||||
usePersistedStore.getState().addScheduledTask({
|
||||
'type': 'delete',
|
||||
'cron': cronExpression,
|
||||
'args': {
|
||||
'fs': sourceFs,
|
||||
'rmDirs': rmDirs,
|
||||
'_filter': filterOptions,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
await startDelete({
|
||||
fs: sourceFs,
|
||||
rmDirs,
|
||||
_filter: filterOptions,
|
||||
})
|
||||
|
||||
setIsStarted(true)
|
||||
|
||||
await message('Delete job started', {
|
||||
title: 'Success',
|
||||
okLabel: 'OK',
|
||||
})
|
||||
} catch (error) {
|
||||
await message(`Failed to start delete job, ${error}`, {
|
||||
title: 'Error',
|
||||
kind: 'error',
|
||||
okLabel: 'OK',
|
||||
})
|
||||
} finally {
|
||||
setIsLoading(false)
|
||||
}
|
||||
}, [sourceFs, filterOptions, rmDirs, cronExpression])
|
||||
|
||||
const buttonText = useMemo(() => {
|
||||
if (isLoading) return 'STARTING...'
|
||||
if (!sourceFs || sourceFs.length === 0) return 'Please select a source path'
|
||||
if (jsonError) return 'Invalid JSON for ' + jsonError.toUpperCase() + ' options'
|
||||
return 'START DELETE'
|
||||
}, [isLoading, jsonError, sourceFs])
|
||||
|
||||
const buttonIcon = useMemo(() => {
|
||||
if (isLoading) return
|
||||
if (!sourceFs || sourceFs.length === 0) return <FoldersIcon className="w-5 h-5" />
|
||||
if (jsonError) return <AlertOctagonIcon className="w-5 h-5" />
|
||||
return <PlayIcon className="w-5 h-5" />
|
||||
}, [isLoading, jsonError, sourceFs])
|
||||
|
||||
return (
|
||||
<div className="flex flex-col h-screen gap-10 pt-10">
|
||||
{/* Main Content */}
|
||||
<div className="flex flex-col flex-1 w-full max-w-3xl gap-6 mx-auto">
|
||||
{/* Path Display */}
|
||||
<PathField
|
||||
path={sourceFs || ''}
|
||||
setPath={setSourceFs}
|
||||
label="Path"
|
||||
placeholder="Enter a remote:/path"
|
||||
showPicker={false}
|
||||
/>
|
||||
|
||||
{/* <div className="flex flex-col gap-2 pt-2 -mb-5">
|
||||
<Switch isSelected={rmDirs} onValueChange={setRmDirs}>
|
||||
Delete empty source directories after delete
|
||||
</Switch>
|
||||
</div> */}
|
||||
|
||||
<Accordion>
|
||||
<AccordionItem
|
||||
key="filters"
|
||||
startContent={
|
||||
<Avatar color="danger" radius="lg" fallback={<FilterIcon />} />
|
||||
}
|
||||
indicator={<FilterIcon />}
|
||||
subtitle="Tap to toggle filtering options for this operation"
|
||||
title="Filters"
|
||||
>
|
||||
<OptionsSection
|
||||
globalOptions={globalOptions['filter' as keyof typeof globalOptions]}
|
||||
optionsJson={filterOptionsJson}
|
||||
setOptionsJson={setFilterOptionsJson}
|
||||
optionsFetcher={getFilterFlags}
|
||||
rows={4}
|
||||
isLocked={filterOptionsLocked}
|
||||
setIsLocked={setFilterOptionsLocked}
|
||||
/>
|
||||
</AccordionItem>
|
||||
<AccordionItem
|
||||
key="cron"
|
||||
startContent={
|
||||
<Avatar color="warning" radius="lg" fallback={<ClockIcon />} />
|
||||
}
|
||||
indicator={<ClockIcon />}
|
||||
subtitle="Tap to toggle cron options for this operation"
|
||||
title="Cron"
|
||||
>
|
||||
<CronEditor expression={cronExpression} onChange={setCronExpression} />
|
||||
</AccordionItem>
|
||||
</Accordion>
|
||||
</div>
|
||||
|
||||
<div className="sticky bottom-0 z-50 flex items-center justify-center flex-none gap-2 p-4 border-t border-neutral-500/20 bg-neutral-900/50 backdrop-blur-lg">
|
||||
{isStarted ? (
|
||||
<>
|
||||
<Button
|
||||
fullWidth={true}
|
||||
size="lg"
|
||||
onPress={() => {
|
||||
setFilterOptionsJson('{}')
|
||||
setSourceFs(undefined)
|
||||
setIsStarted(false)
|
||||
}}
|
||||
data-focus-visible="false"
|
||||
>
|
||||
New Delete
|
||||
</Button>
|
||||
|
||||
{/* <Button
|
||||
fullWidth={true}
|
||||
size="lg"
|
||||
color="primary"
|
||||
onPress={async () => {
|
||||
await openWindow({ name: 'Jobs', url: '/jobs' })
|
||||
// await getCurrentWindow().hide()
|
||||
|
||||
// await getCurrentWindow().destroy()
|
||||
}}
|
||||
data-focus-visible="false"
|
||||
>
|
||||
Show Jobs
|
||||
</Button> */}
|
||||
</>
|
||||
) : (
|
||||
<Button
|
||||
onPress={handleStartDelete}
|
||||
size="lg"
|
||||
fullWidth={true}
|
||||
type="button"
|
||||
color="primary"
|
||||
isDisabled={isLoading || !!jsonError || !sourceFs || sourceFs.length === 0}
|
||||
isLoading={isLoading}
|
||||
endContent={buttonIcon}
|
||||
className="max-w-2xl"
|
||||
data-focus-visible="false"
|
||||
>
|
||||
{buttonText}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -401,6 +401,36 @@ function GeneralSection() {
|
||||
>
|
||||
Show <span className="font-mono text-blue-300">Copy</span> option
|
||||
</Checkbox>
|
||||
<Checkbox
|
||||
isSelected={!disabledActions?.includes('tray-move')}
|
||||
onValueChange={(value) => {
|
||||
if (value) {
|
||||
setDisabledActions(
|
||||
disabledActions?.filter((action) => action !== 'tray-move') ||
|
||||
[]
|
||||
)
|
||||
} else {
|
||||
setDisabledActions([...(disabledActions || []), 'tray-move'])
|
||||
}
|
||||
}}
|
||||
>
|
||||
Show <span className="font-mono text-blue-300">Move</span> option
|
||||
</Checkbox>
|
||||
<Checkbox
|
||||
isSelected={!disabledActions?.includes('tray-delete')}
|
||||
onValueChange={(value) => {
|
||||
if (value) {
|
||||
setDisabledActions(
|
||||
disabledActions?.filter((action) => action !== 'tray-delete') ||
|
||||
[]
|
||||
)
|
||||
} else {
|
||||
setDisabledActions([...(disabledActions || []), 'tray-delete'])
|
||||
}
|
||||
}}
|
||||
>
|
||||
Show <span className="font-mono text-blue-300">Delete</span> option
|
||||
</Checkbox>
|
||||
</div>
|
||||
|
||||
{/* <Button
|
||||
|
||||
Reference in New Issue
Block a user