From 54f6f521a3645a84c07bdf6b0eca4e7c4f2f9a1c Mon Sep 17 00:00:00 2001 From: "sa.cux" Date: Wed, 31 Jan 2024 14:46:39 +0100 Subject: [PATCH] created new and refactor utils functions --- src/utils/downloadBlob.ts | 8 +++++ src/utils/exportOptions.ts | 6 ++++ src/utils/exportRepositories.ts | 63 +++++++++++++++++++++++++++++++++ src/utils/generateText.ts | 32 +++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 src/utils/downloadBlob.ts create mode 100644 src/utils/exportOptions.ts create mode 100644 src/utils/exportRepositories.ts create mode 100644 src/utils/generateText.ts diff --git a/src/utils/downloadBlob.ts b/src/utils/downloadBlob.ts new file mode 100644 index 0000000..f142a55 --- /dev/null +++ b/src/utils/downloadBlob.ts @@ -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); +}; diff --git a/src/utils/exportOptions.ts b/src/utils/exportOptions.ts new file mode 100644 index 0000000..aed95a8 --- /dev/null +++ b/src/utils/exportOptions.ts @@ -0,0 +1,6 @@ +/* eslint-disable no-unused-vars */ +export enum ExportOptions { + Download = "download", + Clipboard = "clipboard", +} +/* eslint-enable no-unused-vars */ diff --git a/src/utils/exportRepositories.ts b/src/utils/exportRepositories.ts new file mode 100644 index 0000000..ffa74f4 --- /dev/null +++ b/src/utils/exportRepositories.ts @@ -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"); + } +}; diff --git a/src/utils/generateText.ts b/src/utils/generateText.ts new file mode 100644 index 0000000..17da36a --- /dev/null +++ b/src/utils/generateText.ts @@ -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; +};