|
2 | 2 | import { useToast } from 'primevue/usetoast' |
3 | 3 | import { inject } from 'vue' |
4 | 4 |
|
| 5 | +import ConfirmationDialogContent from '@/components/dialog/content/ConfirmationDialogContent.vue' |
5 | 6 | import { downloadFile } from '@/base/common/downloadUtil' |
6 | 7 | import { t } from '@/i18n' |
| 8 | +import { isCloud } from '@/platform/distribution/types' |
7 | 9 | import { api } from '@/scripts/api' |
8 | 10 | import { getOutputAssetMetadata } from '../schemas/assetMetadataSchema' |
| 11 | +import { useAssetsStore } from '@/stores/assetsStore' |
| 12 | +import { useDialogStore } from '@/stores/dialogStore' |
9 | 13 |
|
| 14 | +import type { AssetItem } from '../schemas/assetSchema' |
10 | 15 | import type { AssetMeta } from '../schemas/mediaAssetSchema' |
11 | 16 | import { MediaAssetKey } from '../schemas/mediaAssetSchema' |
| 17 | +import { assetService } from '../services/assetService' |
12 | 18 |
|
13 | 19 | export function useMediaAssetActions() { |
14 | 20 | const toast = useToast() |
| 21 | + const dialogStore = useDialogStore() |
15 | 22 | const mediaContext = inject(MediaAssetKey, null) |
16 | 23 |
|
17 | 24 | const selectAsset = (asset: AssetMeta) => { |
@@ -47,8 +54,98 @@ export function useMediaAssetActions() { |
47 | 54 | } |
48 | 55 | } |
49 | 56 |
|
50 | | - const deleteAsset = (assetId: string) => { |
51 | | - console.log('Deleting asset:', assetId) |
| 57 | + /** |
| 58 | + * Show confirmation dialog and delete asset if confirmed |
| 59 | + * @param asset The asset to delete |
| 60 | + * @returns true if the asset was deleted, false otherwise |
| 61 | + */ |
| 62 | + const confirmDelete = async (asset: AssetItem): Promise<boolean> => { |
| 63 | + const assetType = asset.tags?.[0] || 'output' |
| 64 | + |
| 65 | + return new Promise((resolve) => { |
| 66 | + dialogStore.showDialog({ |
| 67 | + key: 'delete-asset-confirmation', |
| 68 | + title: t('mediaAsset.deleteAssetTitle'), |
| 69 | + component: ConfirmationDialogContent, |
| 70 | + props: { |
| 71 | + message: t('mediaAsset.deleteAssetDescription'), |
| 72 | + type: 'delete', |
| 73 | + itemList: [asset.name], |
| 74 | + onConfirm: async () => { |
| 75 | + const success = await deleteAsset(asset, assetType) |
| 76 | + resolve(success) |
| 77 | + }, |
| 78 | + onCancel: () => { |
| 79 | + resolve(false) |
| 80 | + } |
| 81 | + } |
| 82 | + }) |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + const deleteAsset = async (asset: AssetItem, assetType: string) => { |
| 87 | + const assetsStore = useAssetsStore() |
| 88 | + |
| 89 | + try { |
| 90 | + if (assetType === 'output') { |
| 91 | + // For output files, delete from history |
| 92 | + const promptId = |
| 93 | + asset.id || getOutputAssetMetadata(asset.user_metadata)?.promptId |
| 94 | + if (!promptId) { |
| 95 | + throw new Error('Unable to extract prompt ID from asset') |
| 96 | + } |
| 97 | + |
| 98 | + await api.deleteItem('history', promptId) |
| 99 | + |
| 100 | + // Update history assets in store after deletion |
| 101 | + await assetsStore.updateHistory() |
| 102 | + |
| 103 | + toast.add({ |
| 104 | + severity: 'success', |
| 105 | + summary: t('g.success'), |
| 106 | + detail: t('mediaAsset.assetDeletedSuccessfully'), |
| 107 | + life: 2000 |
| 108 | + }) |
| 109 | + return true |
| 110 | + } else { |
| 111 | + // For input files, only allow deletion in cloud environment |
| 112 | + if (!isCloud) { |
| 113 | + toast.add({ |
| 114 | + severity: 'warn', |
| 115 | + summary: t('g.warning'), |
| 116 | + detail: t('mediaAsset.deletingImportedFilesCloudOnly'), |
| 117 | + life: 3000 |
| 118 | + }) |
| 119 | + return false |
| 120 | + } |
| 121 | + |
| 122 | + // In cloud environment, use the assets API to delete |
| 123 | + await assetService.deleteAsset(asset.id) |
| 124 | + |
| 125 | + // Update input assets in store after deletion |
| 126 | + await assetsStore.updateInputs() |
| 127 | + |
| 128 | + toast.add({ |
| 129 | + severity: 'success', |
| 130 | + summary: t('g.success'), |
| 131 | + detail: t('mediaAsset.assetDeletedSuccessfully'), |
| 132 | + life: 2000 |
| 133 | + }) |
| 134 | + return true |
| 135 | + } |
| 136 | + } catch (error) { |
| 137 | + console.error('Failed to delete asset:', error) |
| 138 | + toast.add({ |
| 139 | + severity: 'error', |
| 140 | + summary: t('g.error'), |
| 141 | + detail: |
| 142 | + error instanceof Error |
| 143 | + ? error.message |
| 144 | + : t('mediaAsset.failedToDeleteAsset'), |
| 145 | + life: 3000 |
| 146 | + }) |
| 147 | + return false |
| 148 | + } |
52 | 149 | } |
53 | 150 |
|
54 | 151 | const playAsset = (assetId: string) => { |
@@ -110,6 +207,7 @@ export function useMediaAssetActions() { |
110 | 207 | return { |
111 | 208 | selectAsset, |
112 | 209 | downloadAsset, |
| 210 | + confirmDelete, |
113 | 211 | deleteAsset, |
114 | 212 | playAsset, |
115 | 213 | copyJobId, |
|
0 commit comments