-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
622 additions
and
434 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.