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

feat: adds capability to export user qr-code into wallpaper #10115

Merged
merged 3 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
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 @@ -20,6 +20,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