From 18578a63ec00a642b412dd15af8c5f64a494881c Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Tue, 3 Jun 2025 15:54:01 -0700 Subject: [PATCH] Rationalize openObjectRLInNewTab --- src/common/util/imageUtils.ts | 17 ----------------- src/common/util/urlUtils.ts | 17 +++++++++++++++++ .../blocks/image/RenderImageRefDBlob.tsx | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/common/util/imageUtils.ts b/src/common/util/imageUtils.ts index 0d35937b1..c95d0ae85 100644 --- a/src/common/util/imageUtils.ts +++ b/src/common/util/imageUtils.ts @@ -26,23 +26,6 @@ export type CommonImageMimeTypes = 'image/png' | 'image/jpeg' | 'image/webp'; export type LLMImageResizeMode = 'openai-low-res' | 'openai-high-res' | 'google' | 'anthropic' | 'thumbnail-128' | 'thumbnail-256'; -/** - * Opens a blob URL in a new tab - * @returns true if window.open succeeded, false otherwise - */ -export function showBlobObjectURLInNewTab(blobURL: string): boolean { - if (typeof window !== 'undefined') { - try { - return window.open(blobURL, '_blank', 'noopener,noreferrer') !== null; - } catch (error) { - console.warn('showBlobURLInNewTab: Failed to open new tab.', error); - return false; - } - } - return false; -} - - /** * Converts an SVG string to a PNG Blob via an intermediate canvas. */ diff --git a/src/common/util/urlUtils.ts b/src/common/util/urlUtils.ts index 19ce742e6..450ca3951 100644 --- a/src/common/util/urlUtils.ts +++ b/src/common/util/urlUtils.ts @@ -105,6 +105,23 @@ export function extractUrlsFromText(text: string): string[] { return text.match(urlRegex) || []; } +/** + * Opens a blob URL in a new tab + * @deprecated minimize this usage, prefer to open in the app instead, so we can handle URL revocation properly + * @returns true if window.open succeeded, false otherwise + */ +export function openObjectRLInNewTab(blobURL: string): boolean { + if (typeof window !== 'undefined') { + try { + return window.open(blobURL, '_blank', 'noopener,noreferrer') !== null; + } catch (error) { + console.warn('showBlobURLInNewTab: Failed to open new tab.', error); + return false; + } + } + return false; +} + // added for future in-app routing // export namespace SearchParams { diff --git a/src/modules/blocks/image/RenderImageRefDBlob.tsx b/src/modules/blocks/image/RenderImageRefDBlob.tsx index 5c37de958..444835422 100644 --- a/src/modules/blocks/image/RenderImageRefDBlob.tsx +++ b/src/modules/blocks/image/RenderImageRefDBlob.tsx @@ -12,7 +12,7 @@ import { useDBAsset } from '~/modules/dblobs/dblobs.hooks'; import type { DMessageContentFragment, DMessageDataRef } from '~/common/stores/chat/chat.fragments'; import { addSnackbar } from '~/common/components/snackbar/useSnackbarsStore'; -import { showBlobObjectURLInNewTab } from '~/common/util/imageUtils'; +import { openObjectRLInNewTab } from '~/common/util/urlUtils'; import { RenderImageURL, RenderImageURLVariant } from './RenderImageURL'; @@ -33,7 +33,7 @@ export async function showImageDataRefInNewTab(dataRef: DMessageDataRef) { // notify the user that the image has been opened in a new tab (for Safari when it's blocking by default) addSnackbar({ key: 'opened-image-in-new-tab', message: 'Image opened in a New Tab.', type: 'success', closeButton: false, overrides: { autoHideDuration: 1600 } }); - return showBlobObjectURLInNewTab(imageBlobURL); + return openObjectRLInNewTab(imageBlobURL); }