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

Commit

Permalink
feat: capability to export user qr-code into wallpaper (#10115)
Browse files Browse the repository at this point in the history
Co-authored-by: Eddie Jaoude <[email protected]>
  • Loading branch information
akash19coder and eddiejaoude authored Jun 8, 2024
1 parent 70d0a3c commit 7756f16
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
22 changes: 22 additions & 0 deletions components/user/QRcodeWallpaper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react";
import { QRCodeSVG } from "qrcode.react";
import LogoWide from "@public/logos/LogoWide";

function QRcodeWallpaper({ BASE_URL, data }) {
const fallbackImageSize = 120;

return (
<div style={{ marginTop: "50%", marginLeft: "16%" }}>
<QRCodeSVG
className="border border-white"
value={`${BASE_URL}/${data.username}`}
size={fallbackImageSize * 6}
/>
<div style={{ marginLeft: "7rem", marginTop: "5rem" }}>
<LogoWide width={512} />
</div>
</div>
);
}

export default QRcodeWallpaper;
41 changes: 41 additions & 0 deletions components/user/UserProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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 FallbackImage from "@components/FallbackImage";
import UserSocial from "./UserSocials";
Expand All @@ -14,6 +15,19 @@ import Modal from "@components/Modal";
import ClipboardCopy from "@components/ClipboardCopy";
import { socials } from "@config/socials";
import Markdown from "@components/Markdown";
import { renderToString } from "react-dom/server";
import QRcodeWallpaper from "./QRcodeWallpaper";

const renderQRCodeWallpaperToString = (BASE_URL, data) => {
const qrCodeElement = React.createElement(QRcodeWallpaper, {
BASE_URL: BASE_URL,
data: data,
});

const qrCodeString = renderToString(qrCodeElement);

return qrCodeString;
};

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

const downloadWallpaper = async () => {
try {
const qrCodeString = renderQRCodeWallpaperToString(BASE_URL, data);

const container = document.createElement("div");
container.innerHTML = qrCodeString;

const dataUrl = await toPng(container, {
cacheBust: false,
backgroundColor: "#122640",
width: 1080,
height: 1920,
});

saveAs(dataUrl, `Biodrop-Wallpaper-${data.username}.png`);
} catch (e) {
console.error(e);
}
};

return (
<>
<div className="flex justify-center items-center flex-col md:flex-row gap-x-6">
Expand Down Expand Up @@ -128,6 +162,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
6 changes: 6 additions & 0 deletions 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 @@ -20,6 +20,7 @@
"autoprefixer": "^10.4.19",
"dotenv": "^16.4.5",
"file-saver": "^2.0.5",
"html-to-image": "^1.11.11",
"husky": "^9.0.11",
"leaflet": "^1.9.4",
"micro": "^10.0.1",
Expand Down

0 comments on commit 7756f16

Please sign in to comment.