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
5 changes: 5 additions & 0 deletions .changeset/urlpreview-middleclick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

Handles a middle-click on the url preview card thumbnail image by downloading the full image from the homeserver proxy through a fetch request and opening received blob in the new tab
29 changes: 27 additions & 2 deletions src/app/components/url-preview/UrlPreviewCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
getIntersectionObserverEntry,
useIntersectionObserver,
} from '$hooks/useIntersectionObserver';
import { mxcUrlToHttp } from '$utils/matrix';
import { mxcUrlToHttp, downloadMedia } from '$utils/matrix';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import * as css from './UrlPreviewCard.css';
import { UrlPreview, UrlPreviewContent, UrlPreviewDescription } from './UrlPreview';
Expand All @@ -17,6 +17,16 @@

const linkStyles = { color: color.Success.Main };

const openMediaInNewTab = async (url: string | undefined) => {
if (!url) {
console.warn('Attempted to open an empty url');

Check warning on line 22 in src/app/components/url-preview/UrlPreviewCard.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
return;
}
const blob = await downloadMedia(url);
const blobUrl = URL.createObjectURL(blob);
window.open(blobUrl, '_blank');
};

export const UrlPreviewCard = as<'div', { url: string; ts: number; mediaType?: string | null }>(
({ url, ts, mediaType, ...props }, ref) => {
const mx = useMatrixClient();
Expand Down Expand Up @@ -50,6 +60,21 @@
'scale',
false
);
const handleAuxClick = (ev: React.MouseEvent) => {
if (!prev['og:image']) {
console.warn('No image');

Check warning on line 65 in src/app/components/url-preview/UrlPreviewCard.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
return;
}
if (ev.button === 1) {
ev.preventDefault();
const mxcUrl = mxcUrlToHttp(mx, prev['og:image'], /* useAuthentication */ true);
if (!mxcUrl) {
console.error('Error converting mxc:// url.');

Check warning on line 72 in src/app/components/url-preview/UrlPreviewCard.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
return;
}
openMediaInNewTab(mxcUrl);
}
};

return (
<Box
Expand Down Expand Up @@ -128,6 +153,7 @@
<ImageContent
style={{ width: '100%', height: '100%', position: 'absolute', inset: 0 }}
autoPlay
onAuxClick={handleAuxClick}
body={prev['og:title']}
url={prev['og:image']}
renderViewer={(p) => <ImageViewer {...p} />}
Expand Down Expand Up @@ -187,7 +213,6 @@
</Box>
);
}

return (
<UrlPreview
{...props}
Expand Down
Loading