Skip to content

Commit

Permalink
fix: consolidate functionality used for adding third party scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasGross committed Nov 27, 2024
1 parent ef1b759 commit 8e91f79
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 58 deletions.
4 changes: 3 additions & 1 deletion components/shared/publizonPlayer/PublizonPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
23 changes: 1 addition & 22 deletions components/shared/publizonPlayer/helper.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
type TAssetType = {
src: string
type: "script" | "link"
}
import { TAssetType } from "@/lib/helpers/helper.scripts"

export const assets: TAssetType[] = [
{
src: "https://play.pubhub.dk/1.3.0/js/player-kernel.min.js",
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()
}
}
4 changes: 3 additions & 1 deletion components/shared/publizonReader/PublizonReader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
35 changes: 1 addition & 34 deletions components/shared/publizonReader/helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
type TAssetType = {
src: string
type: "script" | "link"
}
import { TAssetType } from "@/lib/helpers/helper.scripts"

export const readerAssets: TAssetType[] = [
{
Expand All @@ -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()
}
}
34 changes: 34 additions & 0 deletions lib/helpers/helper.scripts.ts
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 8e91f79

Please sign in to comment.