Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions web/src/lib/components/asset-viewer/editor/EditorPanel.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<script lang="ts">
import { EditToolType, editManager } from '#lib/managers/edit/edit-manager.svelte.ts';
import { EditToolType, editManager, type EditActions } from '#lib/managers/edit/edit-manager.svelte.ts';
import { editAssetMutation, getAssetEditsQuery, removeAssetEditsMutation } from '#lib/queries/asset.query.ts';
import { queryClient } from '#lib/queries/client.ts';
import { websocketEvents } from '#lib/stores/websocket.ts';
import { getAssetEdits, type AssetResponseDto } from '@immich/sdk';
import type { AssetResponseDto } from '@immich/sdk';
import { Button, HStack, IconButton, shortcuts } from '@immich/ui';
import { mdiClose } from '@mdi/js';
import { createMutation } from '@tanstack/svelte-query';
import { onDestroy, onMount } from 'svelte';
import { t } from 'svelte-i18n';

Expand All @@ -21,16 +24,24 @@
}

onMount(async () => {
const edits = await getAssetEdits({ id: asset.id });
const edits = await queryClient.query(getAssetEditsQuery(asset.id));
await editManager.activateTool(EditToolType.Transform, asset, edits);
});

onDestroy(() => {
editManager.cleanup();
});

const editAsset = createMutation(editAssetMutation);
const removeAssetEdits = createMutation(removeAssetEditsMutation);

const saveEdits = (id: string, edits: EditActions) =>
edits.length === 0
? removeAssetEdits.mutateAsync([{ id }])
: editAsset.mutateAsync([{ id, assetEditsCreateDto: { edits } }]);

async function applyEdits() {
const success = await editManager.applyEdits();
const success = await editManager.applyEdits(saveEdits);

if (success) {
onClose();
Expand Down
13 changes: 3 additions & 10 deletions web/src/lib/managers/edit/edit-manager.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { queryClient } from '#lib/queries/client.ts';
import { QueryKeys } from '#lib/queries/query-keys.ts';
import { waitForWebsocketEvent } from '#lib/stores/websocket.ts';
import { getFormatter } from '#lib/utils/i18n.ts';
import { editAsset, removeAssetEdits, type AssetEditsCreateDto, type AssetResponseDto } from '@immich/sdk';
import type { AssetEditsCreateDto, AssetResponseDto } from '@immich/sdk';
import { ConfirmModal, modalManager, toastManager } from '@immich/ui';
import { mdiCropRotate } from '@mdi/js';
import type { Component } from 'svelte';
Expand Down Expand Up @@ -116,7 +116,7 @@ export class EditManager {
}
}

async applyEdits(): Promise<boolean> {
async applyEdits(save: (id: string, edits: EditActions) => Promise<unknown>): Promise<boolean> {
this.isApplyingEdits = true;

const edits = this.tools.flatMap((tool) => tool.manager.edits);
Expand All @@ -131,14 +131,7 @@ export class EditManager {
// Setup the websocket listener before sending the edit request
const editCompleted = waitForWebsocketEvent('AssetEditReadyV2', (event) => event.asset.id === assetId, 10_000);

await (edits.length === 0
? removeAssetEdits({ id: assetId })
: editAsset({
id: assetId,
assetEditsCreateDto: {
edits,
},
}));
await save(assetId, edits);

await editCompleted;

Expand Down
14 changes: 14 additions & 0 deletions web/src/lib/queries/asset.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { QueryKeys } from '#lib/queries/query-keys.ts';
import {
checkBulkUpload,
deleteAssets,
editAsset,
getAssetEdits,
getAssetOcr,
getAssetStatistics,
removeAssetEdits,
runAssetJobs,
updateAsset,
updateAssets,
Expand All @@ -28,6 +31,13 @@ export const getAssetOcrQuery = (id: string) =>
staleTime: 0,
});

export const getAssetEditsQuery = (id: string) =>
queryOptions({
queryKey: QueryKeys.assetEdits({ id }),
queryFn: ({ signal }) => getAssetEdits({ id }, { signal }),
staleTime: 0,
});

export const checkBulkUploadMutation = () => asMutation(checkBulkUpload, { meta: { error: { toast: false } } });

export const runAssetJobsMutation = () =>
Expand All @@ -43,3 +53,7 @@ export const deleteAssetsMutation = (meta?: MutationMeta) =>
onSuccess: (_, [{ assetBulkDeleteDto }]) => eventManager.emit('AssetsDelete', assetBulkDeleteDto.ids),
meta: meta ?? { error: { toast: 'errors.unable_to_delete_assets' } },
});

export const editAssetMutation = () => asMutation(editAsset, { meta: { error: { toast: false } } });

export const removeAssetEditsMutation = () => asMutation(removeAssetEdits, { meta: { error: { toast: false } } });
3 changes: 2 additions & 1 deletion web/src/lib/queries/query-keys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
getRecentAlbumsQuery,
} from '#lib/queries/album.query.ts';
import { getApiKeysQuery } from '#lib/queries/api-key.query.ts';
import { getAssetOcrQuery, getAssetStatisticsQuery } from '#lib/queries/asset.query.ts';
import { getAssetEditsQuery, getAssetOcrQuery, getAssetStatisticsQuery } from '#lib/queries/asset.query.ts';
import { getAuthStatusQuery } from '#lib/queries/auth.query.ts';
import {
getClusterGroupRequestsForGroupQuery,
Expand Down Expand Up @@ -133,6 +133,7 @@ const queries: Record<string, QueryFactory> = {
memoriesStatisticsQuery: () => memoriesStatisticsQuery({ isSaved: true }),
getAssetStatisticsQuery: () => getAssetStatisticsQuery({ isFavorite: true }),
getAssetOcrQuery: () => getAssetOcrQuery(id),
getAssetEditsQuery: () => getAssetEditsQuery(id),
getAuthStatusQuery: () => getAuthStatusQuery(),
getClusterGroupRequestsQuery: () => getClusterGroupRequestsQuery(),
getClusterGroupRequestsForGroupQuery: () => getClusterGroupRequestsForGroupQuery(id),
Expand Down
1 change: 1 addition & 0 deletions web/src/lib/queries/query-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const QueryKeys = {
// assets
assetStatistics: (params: Parameters<typeof getAssetStatistics>[0]) => ['assets', 'statistics', params],
assetOcr: ({ id }: IdParams) => ['assets', id, 'ocr'],
assetEdits: ({ id }: IdParams) => ['assets', id, 'edits'],

// auth
authStatus: () => ['auth', 'status'],
Expand Down
Loading