refactor: use eventmanager for onundoarchive

This commit is contained in:
Yaros
2026-06-11 21:46:27 +02:00
parent 3c43b7240b
commit 7cdc252384
7 changed files with 22 additions and 29 deletions
@@ -20,7 +20,7 @@
import { alwaysLoadOriginalVideo } from '$lib/stores/preferences.store';
import { SlideshowNavigation, SlideshowState, slideshowStore } from '$lib/stores/slideshow.store';
import { getSharedLink, handlePromiseError } from '$lib/utils';
import type { OnUndoArchive, OnUndoDelete } from '$lib/utils/actions';
import type { OnUndoDelete } from '$lib/utils/actions';
import { navigateToAsset } from '$lib/utils/asset-utils';
import { handleError } from '$lib/utils/handle-error';
import { InvocationTracker } from '$lib/utils/invocationTracker';
@@ -70,7 +70,6 @@
preAction?: PreAction;
onAction?: OnAction;
onUndoDelete?: OnUndoDelete;
onUndoArchive?: OnUndoArchive;
onClose?: (assetId: string) => void;
onRemoveFromAlbum?: (assetIds: string[]) => void;
onRandom?: () => Promise<{ id: string } | undefined>;
@@ -87,7 +86,6 @@
preAction,
onAction,
onUndoDelete,
onUndoArchive,
onClose,
onRemoveFromAlbum,
onRandom,
@@ -504,7 +502,6 @@
preAction={handlePreAction}
onAction={handleAction}
{onUndoDelete}
{onUndoArchive}
onPlaySlideshow={() => ($slideshowState = SlideshowState.PlaySlideshow)}
onClose={onClose ? () => onClose(stack?.primaryAssetId ?? asset.id) : undefined}
{onRemoveFromAlbum}
@@ -27,7 +27,7 @@
import { getGlobalActions } from '$lib/services/app.service';
import { getAssetActions } from '$lib/services/asset.service';
import { getSharedLink, withoutIcons } from '$lib/utils';
import type { OnUndoArchive, OnUndoDelete } from '$lib/utils/actions';
import type { OnUndoDelete } from '$lib/utils/actions';
import { toTimelineAsset } from '$lib/utils/timeline-util';
import {
AssetTypeEnum,
@@ -58,7 +58,6 @@
preAction: PreAction;
onAction: OnAction;
onUndoDelete?: OnUndoDelete;
onUndoArchive?: OnUndoArchive;
onPlaySlideshow: () => void;
onClose?: () => void;
onRemoveFromAlbum?: (assetIds: string[]) => void;
@@ -75,7 +74,6 @@
preAction,
onAction,
onUndoDelete = undefined,
onUndoArchive = undefined,
onPlaySlideshow,
onClose,
onRemoveFromAlbum,
@@ -190,7 +188,7 @@
{#if !isLocked}
{#if isOwner}
<ArchiveAction {asset} {onAction} {preAction} {onUndoArchive} />
<ArchiveAction {asset} {onAction} {preAction} />
{#if !asset.isArchived && !asset.isTrashed}
<MenuOption
icon={mdiImageSearch}
@@ -3,7 +3,6 @@
import type { OnAction, PreAction } from '$lib/components/asset-viewer/actions/action';
import MenuOption from '$lib/components/shared-components/context-menu/MenuOption.svelte';
import { AssetAction } from '$lib/constants';
import type { TimelineAsset } from '$lib/managers/timeline-manager/types';
import { toggleArchive } from '$lib/utils/asset-utils';
import { toTimelineAsset } from '$lib/utils/timeline-util';
import type { AssetResponseDto } from '@immich/sdk';
@@ -14,16 +13,15 @@
asset: AssetResponseDto;
onAction: OnAction;
preAction: PreAction;
onUndoArchive?: (assets: TimelineAsset[]) => void;
}
let { asset, onAction, preAction, onUndoArchive }: Props = $props();
let { asset, onAction, preAction }: Props = $props();
const onArchive = async () => {
if (!asset.isArchived) {
preAction({ type: AssetAction.ARCHIVE, asset: toTimelineAsset(asset) });
}
const updatedAsset = await toggleArchive(asset, onUndoArchive);
const updatedAsset = await toggleArchive(asset);
if (updatedAsset) {
onAction({ type: asset.isArchived ? AssetAction.ARCHIVE : AssetAction.UNARCHIVE, asset: toTimelineAsset(asset) });
}
@@ -7,6 +7,7 @@
import { authManager } from '$lib/managers/auth-manager.svelte';
import { TimelineManager } from '$lib/managers/timeline-manager/timeline-manager.svelte';
import type { TimelineAsset } from '$lib/managers/timeline-manager/types';
import { eventManager } from '$lib/managers/event-manager.svelte';
import { websocketEvents } from '$lib/stores/websocket';
import { handlePromiseError } from '$lib/utils';
import { updateStackedAssetInTimeline, updateUnstackedAssetInTimeline } from '$lib/utils/actions';
@@ -209,17 +210,6 @@
await navigate({ targetRoute: 'current', assetId: restoredAsset.id });
};
const handleUndoArchive = async (assets: TimelineAsset[]) => {
if (assets.length === 0) {
return;
}
const restoredAsset = assets[0];
const asset = await getAssetInfo({ ...authManager.params, id: restoredAsset.id });
assetViewerManager.setAsset(asset);
await navigate({ targetRoute: 'current', assetId: restoredAsset.id });
};
const handleUpdateOrUpload = (asset: AssetResponseDto) => {
if (asset.id === assetCursor.current.id) {
void loadCloseAssets(asset);
@@ -230,6 +220,17 @@
const unsubscribes = [
websocketEvents.on('on_upload_success', (asset: AssetResponseDto) => handleUpdateOrUpload(asset)),
websocketEvents.on('on_asset_update', (asset: AssetResponseDto) => handleUpdateOrUpload(asset)),
eventManager.on({
AssetsUndoArchive: async (assets) => {
if (assets.length === 0) {
return;
}
const restoredAsset = assets[0];
const asset = await getAssetInfo({ ...authManager.params, id: restoredAsset.id });
assetViewerManager.setAsset(asset);
await navigate({ targetRoute: 'current', assetId: restoredAsset.id });
},
}),
];
return () => {
for (const unsubscribe of unsubscribes) {
@@ -259,7 +260,6 @@
assetCacheManager.invalidate();
}}
onUndoDelete={handleUndoDelete}
onUndoArchive={handleUndoArchive}
onRandom={handleRandom}
onRemoveFromAlbum={handleRemoveFromAlbum}
onClose={handleClose}
@@ -35,6 +35,7 @@ export type Events = {
AssetUpdate: [AssetResponseDto];
AssetsArchive: [string[]];
AssetsUnarchive: [TimelineAsset[]];
AssetsUndoArchive: [TimelineAsset[]];
AssetsDelete: [string[]];
AssetEditsApplied: [string];
AssetsTag: [string[]];
-1
View File
@@ -9,7 +9,6 @@ import { handleError } from './handle-error';
export type OnDelete = (assetIds: string[]) => void;
export type OnUndoDelete = (assets: TimelineAsset[]) => void;
export type OnUndoArchive = (assets: TimelineAsset[]) => void;
export type OnRestore = (ids: string[]) => void;
export type OnLink = (assets: { still: TimelineAsset; motion: TimelineAsset }) => void;
export type OnUnlink = (assets: { still: TimelineAsset; motion: TimelineAsset }) => void;
+4 -4
View File
@@ -391,7 +391,7 @@ export const selectAllAssets = async (timelineManager: TimelineManager, assetInt
}
};
export const toggleArchive = async (asset: AssetResponseDto, onUndoArchive?: (assets: TimelineAsset[]) => void) => {
export const toggleArchive = async (asset: AssetResponseDto) => {
const $t = get(t);
try {
const data = await updateAsset({
@@ -409,7 +409,7 @@ export const toggleArchive = async (asset: AssetResponseDto, onUndoArchive?: (as
description: $t('added_to_archive'),
button: {
label: $t('undo'),
onclick: () => undoArchiveAssets([timelineAsset], onUndoArchive),
onclick: () => undoArchiveAssets([timelineAsset]),
},
},
{ timeout: 5000 },
@@ -424,7 +424,7 @@ export const toggleArchive = async (asset: AssetResponseDto, onUndoArchive?: (as
return asset;
};
const undoArchiveAssets = async (assets: TimelineAsset[], onUndoArchive?: (assets: TimelineAsset[]) => void) => {
const undoArchiveAssets = async (assets: TimelineAsset[]) => {
const $t = get(t);
try {
const ids = assets.map((a) => a.id);
@@ -441,7 +441,7 @@ const undoArchiveAssets = async (assets: TimelineAsset[], onUndoArchive?: (asset
asset.visibility = AssetVisibility.Timeline;
}
eventManager.emit('AssetsUnarchive', assets);
onUndoArchive?.(assets);
eventManager.emit('AssetsUndoArchive', assets);
} catch (error) {
handleError(error, $t('errors.unable_to_archive_unarchive', { values: { archived: false } }));
}