2024-01-17 20:18:04 +01:00
|
|
|
import { notificationController, NotificationType } from '$lib/components/shared-components/notification/notification';
|
2025-05-17 22:57:08 -04:00
|
|
|
import type { AssetStore, TimelineAsset } from '$lib/stores/assets-store.svelte';
|
2025-03-21 12:42:36 -05:00
|
|
|
import type { StackResponse } from '$lib/utils/asset-utils';
|
2025-05-19 17:40:48 -04:00
|
|
|
import { AssetVisibility, deleteAssets as deleteBulk } from '@immich/sdk';
|
2024-06-24 15:50:01 +02:00
|
|
|
import { t } from 'svelte-i18n';
|
|
|
|
|
import { get } from 'svelte/store';
|
2024-01-17 20:18:04 +01:00
|
|
|
import { handleError } from './handle-error';
|
|
|
|
|
|
2024-03-02 01:49:31 +01:00
|
|
|
export type OnDelete = (assetIds: string[]) => void;
|
2024-01-17 20:18:04 +01:00
|
|
|
export type OnRestore = (ids: string[]) => void;
|
2025-05-17 22:57:08 -04:00
|
|
|
export type OnLink = (assets: { still: TimelineAsset; motion: TimelineAsset }) => void;
|
|
|
|
|
export type OnUnlink = (assets: { still: TimelineAsset; motion: TimelineAsset }) => void;
|
2024-10-17 21:53:12 +02:00
|
|
|
export type OnAddToAlbum = (ids: string[], albumId: string) => void;
|
2025-05-19 17:40:48 -04:00
|
|
|
export type OnArchive = (ids: string[], visibility: AssetVisibility) => void;
|
2024-01-17 20:18:04 +01:00
|
|
|
export type OnFavorite = (ids: string[], favorite: boolean) => void;
|
2025-03-21 12:42:36 -05:00
|
|
|
export type OnStack = (result: StackResponse) => void;
|
2025-05-17 22:57:08 -04:00
|
|
|
export type OnUnstack = (assets: TimelineAsset[]) => void;
|
2025-05-15 09:35:21 -06:00
|
|
|
export type OnSetVisibility = (ids: string[]) => void;
|
2024-01-17 20:18:04 +01:00
|
|
|
|
|
|
|
|
export const deleteAssets = async (force: boolean, onAssetDelete: OnDelete, ids: string[]) => {
|
2024-06-24 15:50:01 +02:00
|
|
|
const $t = get(t);
|
2024-01-17 20:18:04 +01:00
|
|
|
try {
|
2024-02-14 08:09:49 -05:00
|
|
|
await deleteBulk({ assetBulkDeleteDto: { ids, force } });
|
2024-03-02 01:49:31 +01:00
|
|
|
onAssetDelete(ids);
|
2024-01-17 20:18:04 +01:00
|
|
|
|
|
|
|
|
notificationController.show({
|
2024-06-24 15:50:01 +02:00
|
|
|
message: force
|
|
|
|
|
? $t('assets_permanently_deleted_count', { values: { count: ids.length } })
|
|
|
|
|
: $t('assets_trashed_count', { values: { count: ids.length } }),
|
2024-01-17 20:18:04 +01:00
|
|
|
type: NotificationType.Info,
|
|
|
|
|
});
|
2024-02-02 04:18:00 +01:00
|
|
|
} catch (error) {
|
2024-06-24 15:50:01 +02:00
|
|
|
handleError(error, $t('errors.unable_to_delete_assets'));
|
2024-01-17 20:18:04 +01:00
|
|
|
}
|
|
|
|
|
};
|
2025-03-21 12:42:36 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the asset stack state in the asset store based on the provided stack response.
|
|
|
|
|
* This function updates the stack information so that the icon is shown for the primary asset
|
|
|
|
|
* and removes any assets from the timeline that are marked for deletion.
|
|
|
|
|
*
|
|
|
|
|
* @param {AssetStore} assetStore - The asset store to update.
|
|
|
|
|
* @param {StackResponse} stackResponse - The stack response containing the stack and assets to delete.
|
|
|
|
|
*/
|
|
|
|
|
export function updateStackedAssetInTimeline(assetStore: AssetStore, { stack, toDeleteIds }: StackResponse) {
|
|
|
|
|
if (stack != undefined) {
|
|
|
|
|
assetStore.updateAssetOperation([stack.primaryAssetId], (asset) => {
|
|
|
|
|
asset.stack = {
|
|
|
|
|
id: stack.id,
|
|
|
|
|
primaryAssetId: stack.primaryAssetId,
|
|
|
|
|
assetCount: stack.assets.length,
|
|
|
|
|
};
|
|
|
|
|
return { remove: false };
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assetStore.removeAssets(toDeleteIds);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the asset store to reflect the unstacked state of assets.
|
|
|
|
|
* This function updates the stack property of each asset to undefined, effectively unstacking them.
|
|
|
|
|
* It also adds the unstacked assets back to the asset store.
|
|
|
|
|
*
|
|
|
|
|
* @param assetStore - The asset store to update.
|
|
|
|
|
* @param assets - The array of asset response DTOs to update in the asset store.
|
|
|
|
|
*/
|
2025-05-17 22:57:08 -04:00
|
|
|
export function updateUnstackedAssetInTimeline(assetStore: AssetStore, assets: TimelineAsset[]) {
|
2025-03-21 12:42:36 -05:00
|
|
|
assetStore.updateAssetOperation(
|
|
|
|
|
assets.map((asset) => asset.id),
|
|
|
|
|
(asset) => {
|
2025-05-17 22:57:08 -04:00
|
|
|
asset.stack = null;
|
2025-03-21 12:42:36 -05:00
|
|
|
return { remove: false };
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assetStore.addAssets(assets);
|
|
|
|
|
}
|