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

feat: exports qr code as wallpaper #9448

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion components/user/UserProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { FaShare } from "react-icons/fa6";
import { QRCodeCanvas } from "qrcode.react";
import { saveAs } from "file-saver";
import { useRouter } from "next/router";
import { toPng } from "html-to-image";
import LogoWide from "@public/logos/LogoWide";
import logger from "@config/logger";

import FallbackImage from "@components/FallbackImage";
import UserSocial from "./UserSocials";
Expand All @@ -24,13 +27,37 @@ function UserProfile({ BASE_URL, data }) {

//Declared Ref object for QR
const qrRef = useRef(null);
const elementRef = useRef(null);

//qrRef.current is pointing to the DOM node and firstChild to its canvas
const downloadQR = () =>
qrRef.current.firstChild.toBlob((blob) =>
saveAs(blob, `biodrop-${data.username}.png`),
);

const downloadWallpaper = () => {
// It removes the "hidden" class to make the element visible for rendering it.
elementRef.current.classList.remove("hidden");


toPng(elementRef.current, {
cacheBust: false,
backgroundColor: "#122640",
})
.then((dataUrl) => {
const link = document.createElement("a");
link.download = "biodrop-wallpaper.png";
link.href = dataUrl;
link.click();


// It adds the "hidden" class back to hide the element again.
elementRef.current.classList.add("hidden");
})
.catch((e) => {
logger.error(e);
});
};

return (
<>
<div className="flex justify-center items-center flex-col md:flex-row gap-x-6">
Expand Down Expand Up @@ -150,6 +177,13 @@ function UserProfile({ BASE_URL, data }) {
</Button>
)}
</div>
<div className="w-full px-2 mx-auto flex justify-center mb-4">
{qrShow && (
<Button primary={true} onClick={downloadWallpaper}>
Export as Wallpaper
</Button>
)}
</div>
</div>
{qrShow && (
<>
Expand Down Expand Up @@ -181,6 +215,22 @@ function UserProfile({ BASE_URL, data }) {
</>
)}
</div>
{/* Part that gets converted into the image */}
<div
ref={elementRef}
className="flex flex-col items-center justify-center mt-10 px-8 hidden"
>
{qrShow && (
<QRCodeCanvas
className="border border-white"
value={`${BASE_URL}/${data.username}`}
size={fallbackImageSize * 6}
/>
)}
<div className="flex m-20">
<LogoWide width={512} />
</div>
</div>
</Modal>
</>
);
Expand Down
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"autoprefixer": "^10.4.16",
"dotenv": "^16.3.1",
"file-saver": "^2.0.5",
"html-to-image": "^1.11.11",
"husky": "^8.0.3",
"leaflet": "^1.9.4",
"micro": "^10.0.1",
Expand Down
Loading