Skip to content

Commit

Permalink
fix: memory leak in image component
Browse files Browse the repository at this point in the history
  • Loading branch information
reyamir committed Jun 3, 2024
1 parent 38d6c51 commit 7c7b082
Show file tree
Hide file tree
Showing 10 changed files with 622 additions and 434 deletions.
Binary file added apps/desktop2/public/404.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion apps/desktop2/src/components/note/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function NoteContent({
<div className="flex flex-col gap-2">
<div
className={cn(
"select-text text-[15px] text-pretty overflow-hidden",
"select-text text-[15px] text-pretty content-break overflow-hidden",
event.content.length > 500 ? "max-h-[300px] gradient-mask-b-0" : "",
className,
)}
Expand Down
4 changes: 3 additions & 1 deletion apps/desktop2/src/components/note/contentLarge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ export function NoteContentLarge({

return (
<div className={cn("select-text", className)}>
<div className="text-[15px] text-pretty leading-normal">{content}</div>
<div className="text-[15px] text-pretty content-break leading-normal">
{content}
</div>
</div>
);
}
2 changes: 1 addition & 1 deletion apps/desktop2/src/components/note/mentions/note.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function MentionNote({
</User.Provider>
<div
className={cn(
"px-3 select-text whitespace-normal text-pretty leading-normal",
"px-3 select-text whitespace-normal text-pretty content-break leading-normal",
data.content.length > 100 ? "max-h-[150px] gradient-mask-b-0" : "",
)}
>
Expand Down
63 changes: 23 additions & 40 deletions apps/desktop2/src/components/note/preview/image.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,44 @@
import { CheckCircleIcon, DownloadIcon } from "@lume/icons";
import { downloadDir } from "@tauri-apps/api/path";
import { WebviewWindow } from "@tauri-apps/api/webviewWindow";
import { download } from "@tauri-apps/plugin-upload";
import { type SyntheticEvent, useState } from "react";

export function ImagePreview({ url }: { url: string }) {
const [downloaded, setDownloaded] = useState(false);
const open = async (url: string) => {
const name = new URL(url).pathname
.split("/")
.pop()
.replace(/[^a-zA-Z ]/g, "");
const label = `viewer-${name}`;
const window = WebviewWindow.getByLabel(label);

const downloadImage = async (e: { stopPropagation: () => void }) => {
try {
e.stopPropagation();
if (!window) {
const newWindow = new WebviewWindow(label, {
url,
title: "Image Viewer",
width: 800,
height: 800,
titleBarStyle: "overlay",
});

const downloadDirPath = await downloadDir();
const filename = url.substring(url.lastIndexOf("/") + 1);
await download(url, `${downloadDirPath}/${filename}`);

setDownloaded(true);
} catch (e) {
console.error(e);
return newWindow;
}
};

const open = async () => {
const name = new URL(url).pathname.split("/").pop();
return new WebviewWindow("image-viewer", {
url,
title: name,
});
};

const fallback = (event: SyntheticEvent<HTMLImageElement, Event>) => {
event.currentTarget.src = "/fallback-image.jpg";
return await window.setFocus();
};

return (
// biome-ignore lint/a11y/useKeyWithClickEvents: <explanation>
<div onClick={() => open()} className="group relative my-1">
<div className="group relative my-1">
<img
src={url}
alt={url}
loading="lazy"
decoding="async"
style={{ contentVisibility: "auto" }}
onError={fallback}
className="max-h-[600px] w-auto object-cover rounded-lg outline outline-1 -outline-offset-1 outline-black/15"
onClick={() => open(url)}
onError={({ currentTarget }) => {
currentTarget.onerror = null;
currentTarget.src = "/404.jpg";
}}
/>
<button
type="button"
onClick={(e) => downloadImage(e)}
className="absolute right-2 top-2 z-20 hidden size-8 items-center justify-center rounded-md bg-white/10 text-white/70 backdrop-blur-2xl hover:bg-blue-500 hover:text-white group-hover:inline-flex"
>
{downloaded ? (
<CheckCircleIcon className="size-4" />
) : (
<DownloadIcon className="size-4" />
)}
</button>
</div>
);
}
8 changes: 8 additions & 0 deletions apps/desktop2/src/components/note/preview/images.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export function Images({ urls }: { urls: string[] }) {
style={{ contentVisibility: "auto" }}
className="max-h-[400px] w-auto object-cover rounded-lg outline outline-1 -outline-offset-1 outline-black/15"
onClick={() => open(urls[0])}
onError={({ currentTarget }) => {
currentTarget.onerror = null;
currentTarget.src = "/404.jpg";
}}
/>
</div>
);
Expand All @@ -54,6 +58,10 @@ export function Images({ urls }: { urls: string[] }) {
style={{ contentVisibility: "auto" }}
className="w-full h-full object-cover rounded-lg outline outline-1 -outline-offset-1 outline-black/15"
onClick={() => open(item)}
onError={({ currentTarget }) => {
currentTarget.onerror = null;
currentTarget.src = "/404.jpg";
}}
/>
</CarouselItem>
)}
Expand Down
5 changes: 4 additions & 1 deletion apps/desktop2/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ function Screen() {
});

return (
<div className="relative flex h-full w-full items-center justify-center">
<div
data-tauri-drag-region
className="relative flex h-full w-full items-center justify-center"
>
<div className="relative z-20 flex flex-col items-center gap-16">
<div className="text-center">
<h2 className="text-xl text-neutral-700 dark:text-neutral-300">
Expand Down
Loading

0 comments on commit 7c7b082

Please sign in to comment.