diff --git a/src/components/image-editor/EditorAgentConversation/useRightClickMenu.ts b/src/components/image-editor/EditorAgentConversation/useRightClickMenu.ts index e26c667dd..1f20e6d29 100644 --- a/src/components/image-editor/EditorAgentConversation/useRightClickMenu.ts +++ b/src/components/image-editor/EditorAgentConversation/useRightClickMenu.ts @@ -293,38 +293,52 @@ export function useRightClickMenu({ } : current, ); - const succeeded = - action === EditorAgentRightClickAction.CopyText && - target.kind === 'text' - ? await copyTextToClipboard(target.text) - : action === EditorAgentRightClickAction.CopyImage && - target.kind === 'asset' && - target.asset.mediaType === 'image' - ? await copyAssetImage(target.asset) - : action === EditorAgentRightClickAction.ReferenceImage && - target.kind === 'asset' && - target.asset.mediaType === 'image' && - contextAssetMediaSrc(target.asset).trim() - ? (onReferenceImage?.(target.asset) ?? false) - : action === EditorAgentRightClickAction.FocusCanvas && - target.kind === 'asset' - ? focusAssetOnCanvas(target.asset, onFocusResource) - : action === EditorAgentRightClickAction.DownloadAsset && - target.kind === 'asset' - ? await downloadAsset(target.asset) - : false; - setRightClickMenu((current) => - current?.target === target && current.pendingAction === action - ? succeeded - ? null - : { - ...current, - pendingAction: null, - resultAction: action, - result: 'error', - } - : current, - ); + let succeeded = false; + switch (action) { + case EditorAgentRightClickAction.CopyText: + if (target.kind === 'text') { + succeeded = await copyTextToClipboard(target.text); + } + break; + case EditorAgentRightClickAction.CopyImage: + if (target.kind === 'asset' && target.asset.mediaType === 'image') { + succeeded = await copyAssetImage(target.asset); + } + break; + case EditorAgentRightClickAction.ReferenceImage: + if ( + target.kind === 'asset' && + target.asset.mediaType === 'image' && + contextAssetMediaSrc(target.asset).trim() + ) { + succeeded = onReferenceImage?.(target.asset) ?? false; + } + break; + case EditorAgentRightClickAction.FocusCanvas: + if (target.kind === 'asset') { + succeeded = focusAssetOnCanvas(target.asset, onFocusResource); + } + break; + case EditorAgentRightClickAction.DownloadAsset: + if (target.kind === 'asset') { + succeeded = await downloadAsset(target.asset); + } + break; + } + setRightClickMenu((current) => { + if (current?.target !== target || current.pendingAction !== action) { + return current; + } + if (succeeded) { + return null; + } + return { + ...current, + pendingAction: null, + resultAction: action, + result: 'error', + }; + }); }, [onFocusResource, onReferenceImage, rightClickMenu], );