2023-10-27 15:34:01 -05:00
|
|
|
<script lang="ts">
|
|
|
|
|
import MenuOption from '$lib/components/shared-components/context-menu/menu-option.svelte';
|
|
|
|
|
import {
|
|
|
|
|
NotificationType,
|
|
|
|
|
notificationController,
|
|
|
|
|
} from '$lib/components/shared-components/notification/notification';
|
2024-01-17 20:18:04 +01:00
|
|
|
import type { OnStack } from '$lib/utils/actions';
|
2024-02-14 08:09:49 -05:00
|
|
|
import { handleError } from '$lib/utils/handle-error';
|
|
|
|
|
import { updateAssets } from '@immich/sdk';
|
|
|
|
|
import { getAssetControlContext } from '../asset-select-control-bar.svelte';
|
2024-03-21 19:39:33 +01:00
|
|
|
import { mdiImageMultipleOutline } from '@mdi/js';
|
2023-10-27 15:34:01 -05:00
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
export let onStack: OnStack | undefined;
|
2023-10-27 15:34:01 -05:00
|
|
|
|
2023-11-11 15:06:19 -06:00
|
|
|
const { clearSelect, getOwnedAssets } = getAssetControlContext();
|
2023-10-27 15:34:01 -05:00
|
|
|
|
|
|
|
|
const handleStack = async () => {
|
|
|
|
|
try {
|
2024-02-02 04:18:00 +01:00
|
|
|
const assets = [...getOwnedAssets()];
|
2023-10-27 15:34:01 -05:00
|
|
|
const parent = assets.at(0);
|
|
|
|
|
|
|
|
|
|
if (parent == undefined) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const children = assets.slice(1);
|
|
|
|
|
const ids = children.map(({ id }) => id);
|
|
|
|
|
|
|
|
|
|
if (children.length > 0) {
|
2024-02-14 08:09:49 -05:00
|
|
|
await updateAssets({ assetBulkUpdateDto: { ids, stackParentId: parent.id } });
|
2023-10-27 15:34:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let childrenCount = parent.stackCount ?? 0;
|
|
|
|
|
for (const asset of children) {
|
|
|
|
|
asset.stackParentId = parent?.id;
|
|
|
|
|
// Add grand-children's count to new parent
|
2024-02-02 04:18:00 +01:00
|
|
|
childrenCount += asset.stackCount == undefined ? 1 : asset.stackCount + 1;
|
2023-10-27 15:34:01 -05:00
|
|
|
// Reset children stack info
|
|
|
|
|
asset.stackCount = null;
|
|
|
|
|
asset.stack = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent.stackCount = childrenCount;
|
|
|
|
|
onStack?.(ids);
|
|
|
|
|
|
|
|
|
|
notificationController.show({
|
|
|
|
|
message: `Stacked ${ids.length + 1} assets`,
|
|
|
|
|
type: NotificationType.Info,
|
|
|
|
|
timeout: 1500,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
clearSelect();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
handleError(error, `Unable to stack`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
|
2024-03-21 19:39:33 +01:00
|
|
|
<MenuOption text="Stack" icon={mdiImageMultipleOutline} on:click={handleStack} />
|