From 369acc7beae2465b80ceee67e0d9eb572669b99b Mon Sep 17 00:00:00 2001 From: Michel Heusschen <59014050+michelheusschen@users.noreply.github.com> Date: Thu, 29 Feb 2024 18:44:30 +0100 Subject: [PATCH] fix(web): asset disappears from album after metadata edit (#7520) --- .../components/album-page/album-viewer.svelte | 2 -- .../shared-components/update-panel.svelte | 18 ---------- web/src/lib/stores/assets.store.ts | 34 ++++++++++++++++--- .../(user)/albums/[albumId]/+page.svelte | 3 -- web/src/routes/(user)/archive/+page.svelte | 2 -- web/src/routes/(user)/favorites/+page.svelte | 2 -- .../(user)/partners/[userId]/+page.svelte | 2 -- web/src/routes/(user)/photos/+page.svelte | 2 -- web/src/routes/(user)/trash/+page.svelte | 2 -- 9 files changed, 30 insertions(+), 37 deletions(-) delete mode 100644 web/src/lib/components/shared-components/update-panel.svelte diff --git a/web/src/lib/components/album-page/album-viewer.svelte b/web/src/lib/components/album-page/album-viewer.svelte index d4f60ab60a..0e5abc1fac 100644 --- a/web/src/lib/components/album-page/album-viewer.svelte +++ b/web/src/lib/components/album-page/album-viewer.svelte @@ -20,7 +20,6 @@ import ThemeButton from '../shared-components/theme-button.svelte'; import { shouldIgnoreShortcut } from '$lib/utils/shortcut'; import { mdiFileImagePlusOutline, mdiFolderDownloadOutline } from '@mdi/js'; - import UpdatePanel from '../shared-components/update-panel.svelte'; import { handlePromiseError } from '$lib/utils'; export let sharedLink: SharedLinkResponseDto; @@ -168,5 +167,4 @@ {/if} - diff --git a/web/src/lib/components/shared-components/update-panel.svelte b/web/src/lib/components/shared-components/update-panel.svelte deleted file mode 100644 index c566bf0e79..0000000000 --- a/web/src/lib/components/shared-components/update-panel.svelte +++ /dev/null @@ -1,18 +0,0 @@ - diff --git a/web/src/lib/stores/assets.store.ts b/web/src/lib/stores/assets.store.ts index 940877e36a..66661fc9b5 100644 --- a/web/src/lib/stores/assets.store.ts +++ b/web/src/lib/stores/assets.store.ts @@ -49,6 +49,11 @@ interface AddAsset { value: AssetResponseDto; } +interface UpdateAsset { + type: 'update'; + value: AssetResponseDto; +} + interface DeleteAsset { type: 'delete'; value: string; @@ -61,7 +66,7 @@ interface TrashAsset { export const photoViewer = writable(null); -type PendingChange = AddAsset | DeleteAsset | TrashAsset; +type PendingChange = AddAsset | UpdateAsset | DeleteAsset | TrashAsset; export class AssetStore { private store$ = writable(this); @@ -102,6 +107,9 @@ export class AssetStore { websocketEvents.on('on_asset_trash', (ids) => { this.addPendingChanges(...ids.map((id): TrashAsset => ({ type: 'trash', value: id }))); }), + websocketEvents.on('on_asset_update', (asset) => { + this.addPendingChanges({ type: 'update', value: asset }); + }), websocketEvents.on('on_asset_delete', (id: string) => { this.addPendingChanges({ type: 'delete', value: id }); }), @@ -122,6 +130,11 @@ export class AssetStore { break; } + case 'update': { + this.updateAsset(value); + break; + } + case 'trash': { if (!this.options.isTrashed) { this.removeAsset(value); @@ -276,6 +289,10 @@ export class AssetStore { return; } + this.addAssetToBucket(asset); + } + + private addAssetToBucket(asset: AssetResponseDto) { const timeBucket = DateTime.fromISO(asset.fileCreatedAt).toUTC().startOf('month').toString(); let bucket = this.getBucketByDate(timeBucket); @@ -307,7 +324,7 @@ export class AssetStore { // If we added an asset to the store, we need to recalculate // asset store containers this.assets.push(asset); - this.updateAsset(asset, true); + this.emit(true); } getBucketByDate(bucketDate: string): AssetBucket | null { @@ -338,14 +355,20 @@ export class AssetStore { return null; } - updateAsset(_asset: AssetResponseDto, recalculate = false) { + updateAsset(_asset: AssetResponseDto) { const asset = this.assets.find((asset) => asset.id === _asset.id); if (!asset) { return; } - Object.assign(asset, _asset); + const recalculate = asset.fileCreatedAt !== _asset.fileCreatedAt; + if (recalculate) { + this.removeAsset(asset.id); + this.addAssetToBucket(_asset); + return; + } + Object.assign(asset, _asset); this.emit(recalculate); } @@ -357,6 +380,9 @@ export class AssetStore { } removeAsset(id: string) { + this.assets = this.assets.filter((asset) => asset.id !== id); + delete this.assetToBucket[id]; + for (let index = 0; index < this.buckets.length; index++) { const bucket = this.buckets[index]; for (let index_ = 0; index_ < bucket.assets.length; index_++) { diff --git a/web/src/routes/(user)/albums/[albumId]/+page.svelte b/web/src/routes/(user)/albums/[albumId]/+page.svelte index 5d05708eaf..fca02b710e 100644 --- a/web/src/routes/(user)/albums/[albumId]/+page.svelte +++ b/web/src/routes/(user)/albums/[albumId]/+page.svelte @@ -30,7 +30,6 @@ NotificationType, notificationController, } from '$lib/components/shared-components/notification/notification'; - import UpdatePanel from '$lib/components/shared-components/update-panel.svelte'; import UserAvatar from '$lib/components/shared-components/user-avatar.svelte'; import { AppRoute, dateFormats } from '$lib/constants'; import { numberOfComments, setNumberOfComments, updateNumberOfComments } from '$lib/stores/activity.store'; @@ -718,8 +717,6 @@ /> {/if} - -