-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added fucntionality to donwload
- Loading branch information
1 parent
6e3f569
commit 147e0b7
Showing
4 changed files
with
145 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HTMLCanvasElement>(null!); | ||
const componentRef = useRef<HTMLDivElement>(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 ( | ||
<div> | ||
<div ref={componentRef} className="border-[1px] rounded-lg mt-4 p-4"> | ||
<div className="flex flex-col gap-6"> | ||
<div className="w-1/3"> | ||
<Logo /> | ||
</div> | ||
<div> | ||
<p className="text-lg font-bold text-gray-700 mb-4">Streaks</p> | ||
<Streak dates={dates} /> | ||
</div> | ||
<div> | ||
<p className="text-lg font-bold text-gray-700 mb-4"> | ||
Contribution Graph | ||
</p> | ||
<ContributionGraph | ||
dates={dates} | ||
tweets={tweets} | ||
username={username} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
<canvas | ||
className="hidden" | ||
ref={canvasRef} | ||
width={width} | ||
height={height} | ||
/> | ||
<button | ||
className="bg-red-400 p-4 mt-10 text-lg rounded-lg" | ||
onClick={onDownload} | ||
> | ||
download | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default DownloadCanvas; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |