diff --git a/components/shared/publizonPlayer/PublizonPlayer.tsx b/components/shared/publizonPlayer/PublizonPlayer.tsx index 431495b8..723ab259 100644 --- a/components/shared/publizonPlayer/PublizonPlayer.tsx +++ b/components/shared/publizonPlayer/PublizonPlayer.tsx @@ -2,7 +2,9 @@ import React, { useEffect } from "react" -import { appendAsset, assets, removeAsset } from "./helper" +import { appendAsset, removeAsset } from "@/lib/helpers/helper.scripts" + +import { assets } from "./helper" // Define mutually exclusive types for identifier and orderId type ReaderType = diff --git a/components/shared/publizonPlayer/helper.ts b/components/shared/publizonPlayer/helper.ts index 9d9161ce..e4fa8f4c 100644 --- a/components/shared/publizonPlayer/helper.ts +++ b/components/shared/publizonPlayer/helper.ts @@ -1,7 +1,4 @@ -type TAssetType = { - src: string - type: "script" | "link" -} +import { TAssetType } from "@/lib/helpers/helper.scripts" export const assets: TAssetType[] = [ { @@ -9,21 +6,3 @@ export const assets: TAssetType[] = [ type: "script", }, ] - -export const appendAsset = ({ src, type }: TAssetType) => { - if (type === "script") { - const scriptElement = document.createElement("script") - scriptElement.src = src - scriptElement.defer = true - scriptElement.async = false - scriptElement.type = "module" - document.head.appendChild(scriptElement) - } -} - -export const removeAsset = ({ src, type }: TAssetType) => { - if (type === "script") { - const scriptElement = document.querySelector(`script[src="${src}"]`) - scriptElement?.remove() - } -} diff --git a/components/shared/publizonReader/PublizonReader.tsx b/components/shared/publizonReader/PublizonReader.tsx index 9b83dc23..b9ab5fe6 100644 --- a/components/shared/publizonReader/PublizonReader.tsx +++ b/components/shared/publizonReader/PublizonReader.tsx @@ -2,7 +2,9 @@ import React, { useEffect } from "react" -import { appendAsset, readerAssets, removeAsset } from "./helper" +import { appendAsset, removeAsset } from "@/lib/helpers/helper.scripts" + +import { readerAssets } from "./helper" // Define mutually exclusive types for identifier and orderId type ReaderType = diff --git a/components/shared/publizonReader/helper.ts b/components/shared/publizonReader/helper.ts index bf68e564..8eca21ac 100644 --- a/components/shared/publizonReader/helper.ts +++ b/components/shared/publizonReader/helper.ts @@ -1,7 +1,4 @@ -type TAssetType = { - src: string - type: "script" | "link" -} +import { TAssetType } from "@/lib/helpers/helper.scripts" export const readerAssets: TAssetType[] = [ { @@ -21,33 +18,3 @@ export const readerAssets: TAssetType[] = [ type: "link", }, ] - -export const appendAsset = ({ src, type }: TAssetType) => { - if (type === "script") { - const scriptElement = document.createElement("script") - scriptElement.src = src - scriptElement.defer = true - scriptElement.async = false - scriptElement.type = "module" - document.head.appendChild(scriptElement) - } - - if (type === "link") { - const linkElement = document.createElement("link") - linkElement.href = src - linkElement.rel = "stylesheet" - document.head.appendChild(linkElement) - } -} - -export const removeAsset = ({ src, type }: TAssetType) => { - if (type === "script") { - const scriptElement = document.querySelector(`script[src="${src}"]`) - scriptElement?.remove() - } - - if (type === "link") { - const linkElement = document.querySelector(`link[href="${src}"]`) - linkElement?.remove() - } -} diff --git a/lib/helpers/helper.scripts.ts b/lib/helpers/helper.scripts.ts new file mode 100644 index 00000000..60a2c830 --- /dev/null +++ b/lib/helpers/helper.scripts.ts @@ -0,0 +1,34 @@ +export type TAssetType = { + src: string + type: "script" | "link" +} + +export const appendAsset = ({ src, type }: TAssetType) => { + if (type === "script") { + const scriptElement = document.createElement("script") + scriptElement.src = src + scriptElement.defer = true + scriptElement.async = false + scriptElement.type = "module" + document.head.appendChild(scriptElement) + } + + if (type === "link") { + const linkElement = document.createElement("link") + linkElement.href = src + linkElement.rel = "stylesheet" + document.head.appendChild(linkElement) + } +} + +export const removeAsset = ({ src, type }: TAssetType) => { + if (type === "script") { + const scriptElement = document.querySelector(`script[src="${src}"]`) + scriptElement?.remove() + } + + if (type === "link") { + const linkElement = document.querySelector(`link[href="${src}"]`) + linkElement?.remove() + } +}