From 474aa3eaf4c5b2f64986b9cfb6a4cf823cc5f738 Mon Sep 17 00:00:00 2001 From: maxdemaio Date: Thu, 13 Jun 2024 08:39:31 -0400 Subject: [PATCH 1/4] use share api for mobi --- src/components/ui/tweet-box.tsx | 44 ++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/components/ui/tweet-box.tsx b/src/components/ui/tweet-box.tsx index 49e6574..0ef83e1 100644 --- a/src/components/ui/tweet-box.tsx +++ b/src/components/ui/tweet-box.tsx @@ -92,24 +92,44 @@ export function TweetBox(props: { ctx.drawImage(img, 0, 0); const imageData: Blob = await new Promise((resolve) => { - canvas.toBlob((a) => a && resolve(a)); + canvas.toBlob((blob) => { + if (blob) resolve(blob); + else throw new Error("Canvas toBlob failed."); + }); }); - await navigator.clipboard.write([ - new ClipboardItem({ - [imageData.type]: imageData, - }), - ]); - - setCopySuccess(true); - setTimeout(() => { - setCopySuccess(false); - }, 2000); + // Copy to clipboard if available + if (navigator.clipboard?.write) { + await navigator.clipboard.write([ + new ClipboardItem({ + [imageData.type]: imageData, + }), + ]); + setCopySuccess(true); + setTimeout(() => { + setCopySuccess(false); + }, 2000); + } else if (navigator.share) { + const file = new File([imageData], "image.png", { + type: imageData.type, + }); + await navigator.share({ + title: "Share Image", + text: "Check out my Shiptalkers image!", + files: [file], + }); + setCopySuccess(true); + setTimeout(() => { + setCopySuccess(false); + }, 2000); + } else { + console.error("Both Clipboard and Share APIs are not available."); + } } catch (error) { console.error("Error copying image: ", error); } }; - + return (
From 46931f6d46ed9a54cf295eaa0b598f87d1138fc8 Mon Sep 17 00:00:00 2001 From: maxdemaio Date: Thu, 13 Jun 2024 23:25:59 -0400 Subject: [PATCH 2/4] check for share first --- src/components/ui/tweet-box.tsx | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/components/ui/tweet-box.tsx b/src/components/ui/tweet-box.tsx index 0ef83e1..3d1a5f0 100644 --- a/src/components/ui/tweet-box.tsx +++ b/src/components/ui/tweet-box.tsx @@ -99,17 +99,7 @@ export function TweetBox(props: { }); // Copy to clipboard if available - if (navigator.clipboard?.write) { - await navigator.clipboard.write([ - new ClipboardItem({ - [imageData.type]: imageData, - }), - ]); - setCopySuccess(true); - setTimeout(() => { - setCopySuccess(false); - }, 2000); - } else if (navigator.share) { + if (navigator.share) { const file = new File([imageData], "image.png", { type: imageData.type, }); @@ -122,6 +112,16 @@ export function TweetBox(props: { setTimeout(() => { setCopySuccess(false); }, 2000); + } else if (navigator.clipboard?.write) { + await navigator.clipboard.write([ + new ClipboardItem({ + [imageData.type]: imageData, + }), + ]); + setCopySuccess(true); + setTimeout(() => { + setCopySuccess(false); + }, 2000); } else { console.error("Both Clipboard and Share APIs are not available."); } From 041f1803b4073dd01a281ca0680acf3af6371898 Mon Sep 17 00:00:00 2001 From: maxdemaio Date: Fri, 14 Jun 2024 07:59:17 -0400 Subject: [PATCH 3/4] more chaining --- src/components/ui/tweet-box.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/ui/tweet-box.tsx b/src/components/ui/tweet-box.tsx index 3d1a5f0..ae572ac 100644 --- a/src/components/ui/tweet-box.tsx +++ b/src/components/ui/tweet-box.tsx @@ -99,7 +99,7 @@ export function TweetBox(props: { }); // Copy to clipboard if available - if (navigator.share) { + if (navigator?.share) { const file = new File([imageData], "image.png", { type: imageData.type, }); @@ -112,7 +112,7 @@ export function TweetBox(props: { setTimeout(() => { setCopySuccess(false); }, 2000); - } else if (navigator.clipboard?.write) { + } else if (navigator?.clipboard?.write) { await navigator.clipboard.write([ new ClipboardItem({ [imageData.type]: imageData, From fa48ed06e8c23838f9f23300cf5ed1bd12744d18 Mon Sep 17 00:00:00 2001 From: maxdemaio Date: Fri, 14 Jun 2024 08:44:46 -0400 Subject: [PATCH 4/4] use type check instead since always available --- src/components/ui/tweet-box.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/ui/tweet-box.tsx b/src/components/ui/tweet-box.tsx index ae572ac..46df32b 100644 --- a/src/components/ui/tweet-box.tsx +++ b/src/components/ui/tweet-box.tsx @@ -99,7 +99,7 @@ export function TweetBox(props: { }); // Copy to clipboard if available - if (navigator?.share) { + if (typeof navigator?.share == "function") { const file = new File([imageData], "image.png", { type: imageData.type, }); @@ -112,7 +112,7 @@ export function TweetBox(props: { setTimeout(() => { setCopySuccess(false); }, 2000); - } else if (navigator?.clipboard?.write) { + } else if (typeof navigator?.clipboard?.write == "function") { await navigator.clipboard.write([ new ClipboardItem({ [imageData.type]: imageData,