Skip to content

Commit

Permalink
created new and refactor utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fuoridallarete committed Jan 31, 2024
1 parent c57b73f commit 54f6f52
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/utils/downloadBlob.ts
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);
};
6 changes: 6 additions & 0 deletions src/utils/exportOptions.ts
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 */
63 changes: 63 additions & 0 deletions src/utils/exportRepositories.ts
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");
}
};
32 changes: 32 additions & 0 deletions src/utils/generateText.ts
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;
};

0 comments on commit 54f6f52

Please sign in to comment.