From 2dfb10e57b51a08fcde2dc6638a81b5ec9bbc7ab Mon Sep 17 00:00:00 2001 From: doprz <52579214+doprz@users.noreply.github.com> Date: Thu, 21 Mar 2024 16:25:37 -0500 Subject: [PATCH] feat: use downloadBlob util (#186) * feat: use downloadBlob util * chore: lint * fix: revert saveCalAsPng * feat: refactor downloadBlob * chore: remove comments * chore: lint and remove extra async * refactor: cleanup --------- Co-authored-by: Razboy20 --- src/shared/types/MIMEType.ts | 3 +++ src/shared/util/downloadBlob.ts | 15 ++++++++++----- src/views/components/calendar/utils.ts | 21 ++++----------------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/shared/types/MIMEType.ts b/src/shared/types/MIMEType.ts index f5d1fa4b..358243f2 100644 --- a/src/shared/types/MIMEType.ts +++ b/src/shared/types/MIMEType.ts @@ -8,7 +8,10 @@ const MIMEType = { IMAGE: 'image/*', AUDIO: 'audio/*', VIDEO: 'video/*', + CALENDAR: 'text/calendar', ANY: '*/*', } as const satisfies Record; +export type MIMETypeKey = keyof typeof MIMEType; + export default MIMEType; diff --git a/src/shared/util/downloadBlob.ts b/src/shared/util/downloadBlob.ts index 9cb424d2..8e654a34 100644 --- a/src/shared/util/downloadBlob.ts +++ b/src/shared/util/downloadBlob.ts @@ -1,11 +1,16 @@ +import type { MIMETypeKey } from '../types/MIMEType'; +import MIMEType from '../types/MIMEType'; + /** - * Downloads a blob as a file. - * @param {Blob} blob - The blob to download. - * @param {string} fileName - The name of the file to be downloaded. - * @returns {Promise} - A promise that resolves when the download is complete. + * Downloads a blob by creating a temporary URL and triggering a download. + * @param blobPart The blob data to be downloaded. + * @param type The MIME type of the blob. + * @param fileName The name of the file to be downloaded. + * @returns A promise that resolves when the download is successful, or rejects with an error if the download fails. */ -export default function downloadBlob(blob: Blob, fileName: string): Promise { +export function downloadBlob(blobPart: BlobPart, type: MIMETypeKey, fileName: string): Promise { return new Promise((resolve, reject) => { + const blob: Blob = new Blob([blobPart], { type: MIMEType[type] }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); diff --git a/src/views/components/calendar/utils.ts b/src/views/components/calendar/utils.ts index 1076cd35..2535fc87 100644 --- a/src/views/components/calendar/utils.ts +++ b/src/views/components/calendar/utils.ts @@ -1,5 +1,6 @@ import { UserScheduleStore } from '@shared/storage/UserScheduleStore'; import type { UserSchedule } from '@shared/types/UserSchedule'; +import { downloadBlob } from '@shared/util/downloadBlob'; import type { Serialized } from 'chrome-extension-toolkit'; import { toPng } from 'html-to-image'; @@ -37,22 +38,6 @@ export const formatToHHMMSS = (minutes: number) => { return `${hours}${mins}00`; }; -/** - * Downloads an ICS file with the given data. - * - * @param data - The data to be included in the ICS file. - */ -const downloadICS = (data: BlobPart) => { - const blob: Blob = new Blob([data], { type: 'text/calendar' }); - const url = URL.createObjectURL(blob); - const link = document.createElement('a'); - link.href = url; - link.download = 'schedule.ics'; - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); -}; - /** * Saves the current schedule as a calendar file in the iCalendar format (ICS). * Fetches the current active schedule and converts it into an ICS string. @@ -98,11 +83,13 @@ export const saveAsCal = async () => { icsString += 'END:VCALENDAR'; - downloadICS(icsString); + downloadBlob(icsString, 'CALENDAR', 'schedule.ics'); }; /** * Saves the calendar as a PNG image. + * + * @param calendarRef - The reference to the calendar component. */ export const saveCalAsPng = (calendarRef: React.RefObject) => { if (calendarRef.current) {