Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
Exporter Client App [Tauri]
Browse files Browse the repository at this point in the history
Reff Issue : [Bug] 'export' button does not work ChatGPTNextWeb#2884
[+] fix(exporter.tsx): fix download function to handle saving file in Tauri app
[+] feat(exporter.tsx): add support for downloading file in Tauri app
[+] fix(exporter.tsx): fix download function to handle saving file in Tauri app
[+] feat(exporter.tsx): add support for downloading file in Tauri app
  • Loading branch information
H0llyW00dzZ committed Oct 1, 2023
1 parent f34a02a commit b13ce09
Showing 1 changed file with 63 additions and 18 deletions.
81 changes: 63 additions & 18 deletions app/components/exporter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -433,26 +433,47 @@ export function ImagePreviewer(props: {

const isMobile = useMobileScreen();

const download = () => {
const download = async () => {
showToast(Locale.Export.Image.Toast);
const dom = previewRef.current;
if (!dom) return;
toPng(dom)
.then((blob) => {
if (!blob) return;

if (isMobile || getClientConfig()?.isApp) {
showImageModal(blob);

const isMobile = useMobileScreen();
const isApp = getClientConfig()?.isApp;

try {
const blob = await toPng(dom);
if (!blob) return;

if (isMobile || (isApp && window.__TAURI__)) {
if (isApp && window.__TAURI__) {
const result = await window.__TAURI__.dialog.save({
defaultPath: `${props.topic}.png`,
});

if (result !== null) {
const response = await fetch(blob);
const buffer = await response.arrayBuffer();
const uint8Array = new Uint8Array(buffer);
await window.__TAURI__.fs.writeBinaryFile(result, uint8Array);
showToast(Locale.Download.Success);
} else {
showToast(Locale.Download.Failed);
}
} else {
const link = document.createElement("a");
link.download = `${props.topic}.png`;
link.href = blob;
link.click();
refreshPreview();
showImageModal(blob);
}
})
.catch((e) => console.log("[Export Image] ", e));
};
} else {
const link = document.createElement("a");
link.download = `${props.topic}.png`;
link.href = blob;
link.click();
refreshPreview();
}
} catch (error) {
showToast(Locale.Download.Failed);
}
};

const refreshPreview = () => {
const dom = previewRef.current;
Expand Down Expand Up @@ -566,14 +587,38 @@ export function MarkdownPreviewer(props: {
const copy = () => {
copyToClipboard(mdText);
};
const download = () => {
const download = async () => {
const isApp = getClientConfig()?.isApp;
const blob = new Blob([mdText], { type: "text/markdown" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = `${props.topic}.md`;
link.click();
};

if (isApp && window.__TAURI__) {
try {
const result = await window.__TAURI__.dialog.save({
defaultPath: `${props.topic}.md`,
});

if (result !== null) {
const response = await fetch(url);
const buffer = await response.arrayBuffer();
const uint8Array = new Uint8Array(buffer);
await window.__TAURI__.fs.writeBinaryFile(result, uint8Array);
showToast(Locale.Download.Success);
} else {
showToast(Locale.Download.Failed);
}
} catch (error) {
showToast(Locale.Download.Failed);
}
} else {
link.click();
}

URL.revokeObjectURL(url);
};
return (
<>
<PreviewActions
Expand Down

0 comments on commit b13ce09

Please sign in to comment.