From 7eb85d4dcb529c696a437fe363765b618d954ed9 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sat, 15 Apr 2023 20:59:43 +0300 Subject: [PATCH] Try Ramp as React component --- app/components/Main.tsx | 2 +- app/components/Ramp.tsx | 124 ++++++++++++++++++++++++++++++++ app/components/layout/index.tsx | 2 +- app/root.tsx | 121 +++++++------------------------ 4 files changed, 150 insertions(+), 99 deletions(-) create mode 100644 app/components/Ramp.tsx diff --git a/app/components/Main.tsx b/app/components/Main.tsx index 20ab091eb..9ee162ae6 100644 --- a/app/components/Main.tsx +++ b/app/components/Main.tsx @@ -21,7 +21,7 @@ export const Main = ({ }) => { const data = useMatches()[0]?.data as RootLoaderData | undefined; const user = useUser(); - const showLeaderboard = data?.gtagId && !user?.patronTier; + const showLeaderboard = data?.publisherId && !user?.patronTier; return (
+ new Promise((resolve, reject) => { + // possible that component was removed before first ad was created + if (!window.ramp.settings || !window.ramp.settings.slots) return; + + delete window.ramp.forcePath; + + let slotsToRemove = []; + Object.entries(window.ramp.settings.slots).forEach(([slotName, slot]) => { + if (oopUnits.includes(slot.type) || slot.videoType === "Bolt Player") { + slotsToRemove.push(slotName); + } + }); + + if (slotsToRemove.length > 0) { + window.ramp.destroyUnits(slotsToRemove).finally(() => resolve()); + } + }); + +export default class Ramp extends React.Component { + constructor(props) { + super(props); + + if (!props || !props.publisherId || !props.id) { + console.error("publisherId and id are required props."); + return; + } + this.init(props); + } + + init({ publisherId, id, forcePath }) { + if (forcePath) window.ramp.forcePath = forcePath; + + // make sure we only do this once per "app" load + if (!window._pwRampComponentLoaded) { + window._pwRampComponentLoaded = true; + + window.ramp.config = `https://config.playwire.com/${publisherId}/v2/websites/${id}/banner.json`; + const configScript = document.createElement("script"); + // configScript.src = `https://cdn.intergient.com/${publisherId}/${id}/ramp.js`; + configScript.src = "https://cdn.intergient.com/ramp_core.js"; + document.head.appendChild(configScript); + } + + this.displayTaglessUnits(); + } + + displayTaglessUnits() { + window.ramp.que.push(() => { + window.ramp + .addUnits([ + { type: "trendi_slideshow" }, + { type: "trendi_video" }, + { type: "site_skin" }, + { type: "flex_leaderboard" }, + { type: "top_rail" }, + { type: "right_rail" }, + { type: "bottom_rail" }, + { type: "left_rail" }, + // {type: 'behind_page'}, + // {type: 'in_image'}, + // {type: 'above_page'}, + // {type: 'in_content'}, + // {type: 'inimg'}, + // {type: 'skin'} + ]) + .finally(() => { + window.ramp.displayUnits(); + }); + }); + } + + componentDidUpdate(prevProps) { + if (this.props.forcePath && prevProps.forcePath !== this.props.forcePath) { + window.ramp.forcePath = this.props.forcePath; + window.ramp.que.push(() => { + window.ramp + .setPath(this.props.forcePath) + .then(() => { + return cleanUp(); + }) + .then(() => { + this.displayTaglessUnits(); + }); + }); + } + } + + componentWillUnmount() { + window.ramp.que.push(() => { + cleanUp(this.unitToAdd.selectorId); + }); + } + + render() { + return null; + } +} diff --git a/app/components/layout/index.tsx b/app/components/layout/index.tsx index 1a859b569..3433fbb71 100644 --- a/app/components/layout/index.tsx +++ b/app/components/layout/index.tsx @@ -50,7 +50,7 @@ export const Layout = React.memo(function Layout({ const showLeaderboard = data && - data.gtagId && + data.publisherId && !data?.user?.patronTier && !location.pathname.includes("plans"); diff --git a/app/root.tsx b/app/root.tsx index ded7aa37a..7ee0bd94d 100644 --- a/app/root.tsx +++ b/app/root.tsx @@ -14,6 +14,7 @@ import { type ShouldRevalidateFunction, useLoaderData, useMatches, + useLocation, } from "@remix-run/react"; import * as React from "react"; import commonStyles from "~/styles/common.css"; @@ -88,7 +89,6 @@ export interface RootLoaderData { | "discordName" | "patronTier" >; - gtagId?: string; publisherId?: string; websiteId?: string; } @@ -106,7 +106,6 @@ export const loader: LoaderFunction = async ({ request }) => { theme: themeSession.getTheme(), patrons: db.users.findAllPatrons(), baseUrl: process.env["BASE_URL"], - gtagId: process.env["GTAG_ID"], publisherId: process.env["PLAYWIRE_PUBLISHER_ID"], websiteId: process.env["PLAYWIRE_WEBSITE_ID"], user: user @@ -152,7 +151,6 @@ function Document({ - @@ -162,6 +160,7 @@ function Document({ {process.env.NODE_ENV === "development" && } + {children} @@ -281,100 +280,6 @@ function Fonts() { ); } -function PlaywireScripts({ data }: { data: RootLoaderData | undefined }) { - if ( - !data || - !data.gtagId || - !data.publisherId || - !data.websiteId || - data.user?.patronTier - ) { - return null; - } - - const units: Array<{ selectorId?: string; type: string }> = [ - { - selectorId: "top-leaderboard", - type: "leaderboard_atf", - }, - { - type: "bottom_rail", - }, - ]; - - return ( - <> - {/* Step 1. */} -