-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created new and refactor utils functions
- Loading branch information
1 parent
c57b73f
commit 54f6f52
Showing
4 changed files
with
109 additions
and
0 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,8 @@ | ||
export const downloadBlob = (blob: Blob, filename: string) => { | ||
const url = URL.createObjectURL(blob); | ||
const link = document.createElement("a"); | ||
link.href = url; | ||
link.download = filename; | ||
link.click(); | ||
URL.revokeObjectURL(url); | ||
}; |
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,6 @@ | ||
/* eslint-disable no-unused-vars */ | ||
export enum ExportOptions { | ||
Download = "download", | ||
Clipboard = "clipboard", | ||
} | ||
/* eslint-enable no-unused-vars */ |
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,63 @@ | ||
import { toPng } from "html-to-image"; | ||
import { toast } from "react-toastify"; | ||
import { PullRequestContributionsByRepository } from "@/types/github"; | ||
import { ExportOptions } from "./exportOptions"; | ||
import { downloadBlob } from "./downloadBlob"; | ||
|
||
export const exportAsJSON = (data: PullRequestContributionsByRepository[]) => { | ||
const jsonStringData = JSON.stringify(data, null, 2); | ||
downloadBlob( | ||
new Blob([jsonStringData], { type: "application/json" }), | ||
"data.json" | ||
); | ||
}; | ||
|
||
export const exportAsText = (text: string) => { | ||
downloadBlob(new Blob([text], { type: "text/plain" }), "data.txt"); | ||
}; | ||
|
||
export const exportAsImage = async ( | ||
selector: string, | ||
option: ExportOptions, | ||
filename?: string | ||
) => { | ||
try { | ||
const dataURI = await toPng( | ||
document.querySelector(selector) as HTMLElement, | ||
{ | ||
includeQueryParams: true, | ||
cacheBust: true, | ||
} | ||
); | ||
|
||
const image = new Image(); | ||
image.src = dataURI; | ||
image.onload = () => { | ||
const canvas = document.createElement("canvas"); | ||
canvas.width = image.width; | ||
canvas.height = image.height; | ||
const context = canvas.getContext("2d"); | ||
context?.drawImage(image, 0, 0); | ||
canvas.toBlob((blob) => { | ||
if (blob) { | ||
switch (option) { | ||
case ExportOptions.Download: | ||
downloadBlob(blob, filename ? `${filename}.png` : "image.png"); | ||
break; | ||
case ExportOptions.Clipboard: | ||
navigator.clipboard | ||
.write([new ClipboardItem({ "image/png": blob })]) | ||
.then(() => toast.success("Image copied to clipboard")) | ||
.catch(() => toast.error("Failed to copy image to clipboard")); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
}); | ||
canvas.remove(); | ||
}; | ||
} catch (error) { | ||
toast.error("Failed to export image"); | ||
} | ||
}; |
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,32 @@ | ||
import { PullRequestContributionsByRepository } from "@/types/github"; | ||
|
||
export const generateText = ( | ||
repositories: PullRequestContributionsByRepository[] | ||
): string => { | ||
let text = "List of repositories and their pull requests:\n\n"; | ||
|
||
for (const repoData of repositories) { | ||
const repositoryName = repoData.repository.name; | ||
const ownerLogin = repoData.repository.owner.login; | ||
const stargazerCount = repoData.repository.stargazerCount; | ||
const avatarUrl = repoData.repository.owner.avatarUrl; | ||
|
||
text += `Repository: ${repositoryName}\n`; | ||
text += `Owner: ${ownerLogin}\n`; | ||
text += `Stargazers: ${stargazerCount}\n`; | ||
text += `Owner Avatar: ${avatarUrl}\n\n`; | ||
|
||
const contributions = repoData.contributions.nodes; | ||
text += "Contributions:\n"; | ||
for (const contribution of contributions) { | ||
const prId = contribution.pullRequest.id; | ||
const prTitle = contribution.pullRequest.title; | ||
const prState = contribution.pullRequest.state; | ||
text += `- Pull Request: ${prTitle}\n`; | ||
text += ` ID: ${prId}\n`; | ||
text += ` State: ${prState}\n`; | ||
} | ||
text += "\n"; | ||
} | ||
return text; | ||
}; |