diff --git a/app/components/DownloadCanvas.tsx b/app/components/DownloadCanvas.tsx new file mode 100644 index 0000000..5db0091 --- /dev/null +++ b/app/components/DownloadCanvas.tsx @@ -0,0 +1,90 @@ +"use client"; + +import { FC, useEffect, useRef, useState } from "react"; +import html2canvas from "html2canvas"; +import { download } from "@/utils/download"; +import useTweetStore from "../store/tweetStore"; +import Streak from "../dashboard/Streak"; +import ContributionGraph from "../dashboard/ContributionGraph"; +import Logo from "./Logo"; + +const DownloadCanvas: FC = ({}) => { + const canvasRef = useRef(null!); + const componentRef = useRef(null!); + const [width, setWidth] = useState(0); + const [height, setHeight] = useState(0); + + useEffect(() => { + const updateSize = () => { + setWidth(componentRef.current.offsetWidth); + setHeight(componentRef.current.offsetHeight); + }; + + updateSize(); + + html2canvas(componentRef.current).then((canvas) => { + const ctx = canvasRef.current.getContext("2d"); + if (ctx) { + ctx.clearRect(0, 0, canvasRef.current.width, canvasRef.current.height); + ctx.drawImage(canvas, 0, 0); + } + }); + + window.addEventListener("resize", updateSize); + + return () => { + window.removeEventListener("resize", updateSize); + }; + }, []); + + const onDownload = (e: any) => { + e.preventDefault(); + download(canvasRef.current); + }; + const { tweets, dates, username, loading } = useTweetStore((state) => ({ + tweets: state.tweets, + dates: state.dates, + username: state.username, + loading: state.loading, + })); + + return ( +
+
+
+
+ +
+
+

Streaks

+ +
+
+

+ Contribution Graph +

+ +
+
+
+ + +
+ ); +}; + +export default DownloadCanvas; diff --git a/app/dashboard/page.tsx b/app/dashboard/page.tsx index 7fb4311..ce95225 100644 --- a/app/dashboard/page.tsx +++ b/app/dashboard/page.tsx @@ -9,6 +9,10 @@ import Streak from "./Streak"; import Loading from "../components/Loading"; import Navbar from "../components/Navbar"; import { Poppins } from "@next/font/google"; +import DownloadCanvas from "../components/DownloadCanvas"; +import { download } from "@/utils/download"; +import { useState } from "react"; +import { Dialog } from "@headlessui/react"; const poppins = Poppins({ subsets: ["latin"], @@ -17,6 +21,11 @@ const poppins = Poppins({ }); const dashboardPage = () => { + const [isDownloadModalOpen, setIsDownloadModalOpen] = + useState(false); + + const openDownloadModal = () => setIsDownloadModalOpen(true); + const { tweets, dates, username, loading } = useTweetStore((state) => ({ tweets: state.tweets, dates: state.dates, @@ -30,7 +39,9 @@ const dashboardPage = () => {
-
+ {/* top */} +
+ {/* streaks */}

Saturday, Jan 21 @@ -39,19 +50,39 @@ const dashboardPage = () => { Hello, {username} -

+
-
-

Badges

- {/* */} -
+ + + {/* canvas */} + {isDownloadModalOpen && ( + setIsDownloadModalOpen(false)} + className="relative z-50" + > + + )}
+ + {/* bottom */}

Contribution Graph diff --git a/package.json b/package.json index 67ac881..475d495 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "@emotion/react": "^11.10.5", "@emotion/styled": "^11.10.5", + "@headlessui/react": "^1.7.13", "@mui/icons-material": "^5.11.0", "@mui/material": "^5.11.6", "@next/font": "^13.1.3", @@ -29,6 +30,7 @@ "framer-motion": "^8.5.0", "frappe-charts": "^1.6.2", "github-contributions-canvas": "^0.6.0", + "html2canvas": "^1.4.1", "moment": "^2.29.4", "next": "13.1.3", "node-fetch": "^2.6.1", diff --git a/utils/download.ts b/utils/download.ts new file mode 100644 index 0000000..63ca264 --- /dev/null +++ b/utils/download.ts @@ -0,0 +1,13 @@ +export function download(canvas: any) { + try { + const dataUrl = canvas.toDataURL(); + const a = document.createElement("a"); + document.body.insertAdjacentElement("beforeend", a); + a.download = "contributions.png"; + a.href = dataUrl; + a.click(); + document.body.removeChild(a); + } catch (err) { + console.error(err); + } +}