Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.

Commit 7756f16

Browse files
feat: capability to export user qr-code into wallpaper (#10115)
Co-authored-by: Eddie Jaoude <[email protected]>
1 parent 70d0a3c commit 7756f16

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

components/user/QRcodeWallpaper.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from "react";
2+
import { QRCodeSVG } from "qrcode.react";
3+
import LogoWide from "@public/logos/LogoWide";
4+
5+
function QRcodeWallpaper({ BASE_URL, data }) {
6+
const fallbackImageSize = 120;
7+
8+
return (
9+
<div style={{ marginTop: "50%", marginLeft: "16%" }}>
10+
<QRCodeSVG
11+
className="border border-white"
12+
value={`${BASE_URL}/${data.username}`}
13+
size={fallbackImageSize * 6}
14+
/>
15+
<div style={{ marginLeft: "7rem", marginTop: "5rem" }}>
16+
<LogoWide width={512} />
17+
</div>
18+
</div>
19+
);
20+
}
21+
22+
export default QRcodeWallpaper;

components/user/UserProfile.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FaShare } from "react-icons/fa6";
33
import { QRCodeCanvas } from "qrcode.react";
44
import { saveAs } from "file-saver";
55
import { useRouter } from "next/router";
6+
import { toPng } from "html-to-image";
67

78
import FallbackImage from "@components/FallbackImage";
89
import UserSocial from "./UserSocials";
@@ -14,6 +15,19 @@ import Modal from "@components/Modal";
1415
import ClipboardCopy from "@components/ClipboardCopy";
1516
import { socials } from "@config/socials";
1617
import Markdown from "@components/Markdown";
18+
import { renderToString } from "react-dom/server";
19+
import QRcodeWallpaper from "./QRcodeWallpaper";
20+
21+
const renderQRCodeWallpaperToString = (BASE_URL, data) => {
22+
const qrCodeElement = React.createElement(QRcodeWallpaper, {
23+
BASE_URL: BASE_URL,
24+
data: data,
25+
});
26+
27+
const qrCodeString = renderToString(qrCodeElement);
28+
29+
return qrCodeString;
30+
};
1731

1832
function UserProfile({ BASE_URL, data }) {
1933
const [qrShow, setQrShow] = useState(false);
@@ -30,6 +44,26 @@ function UserProfile({ BASE_URL, data }) {
3044
saveAs(blob, `biodrop-${data.username}.png`),
3145
);
3246

47+
const downloadWallpaper = async () => {
48+
try {
49+
const qrCodeString = renderQRCodeWallpaperToString(BASE_URL, data);
50+
51+
const container = document.createElement("div");
52+
container.innerHTML = qrCodeString;
53+
54+
const dataUrl = await toPng(container, {
55+
cacheBust: false,
56+
backgroundColor: "#122640",
57+
width: 1080,
58+
height: 1920,
59+
});
60+
61+
saveAs(dataUrl, `Biodrop-Wallpaper-${data.username}.png`);
62+
} catch (e) {
63+
console.error(e);
64+
}
65+
};
66+
3367
return (
3468
<>
3569
<div className="flex justify-center items-center flex-col md:flex-row gap-x-6">
@@ -128,6 +162,13 @@ function UserProfile({ BASE_URL, data }) {
128162
</Button>
129163
)}
130164
</div>
165+
<div className="w-full px-2 mx-auto flex justify-center mb-4">
166+
{qrShow && (
167+
<Button primary={true} onClick={downloadWallpaper}>
168+
Export as Wallpaper
169+
</Button>
170+
)}
171+
</div>
131172
</div>
132173
{qrShow && (
133174
<>

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"autoprefixer": "^10.4.19",
2121
"dotenv": "^16.4.5",
2222
"file-saver": "^2.0.5",
23+
"html-to-image": "^1.11.11",
2324
"husky": "^9.0.11",
2425
"leaflet": "^1.9.4",
2526
"micro": "^10.0.1",

0 commit comments

Comments
 (0)