11import { FeaturedVideo } from "@mui/icons-material" ;
2- import { Box , useTheme } from "@mui/material" ;
3- import { eventsAtom } from "@src/state/events" ;
2+ import { Box , ListSubheader , useTheme } from "@mui/material" ;
3+ import { eventsAtom , playwireAddInitializedUnit } from "@src/state/events" ;
44import { adsEnabled } from "@src/utils/env-tools" ;
55import log from "@src/utils/logger" ;
6- import { useAtomValue } from "jotai" ;
7- import React , { useEffect } from "react" ;
8- import useDevMode from "@src/hooks/dev-mode" ;
6+ import { useAtom } from "jotai" ;
97import md5 from "md5" ;
8+ import React , { useEffect } from "react" ;
9+ import { useTranslation } from "react-i18next" ;
1010
1111export enum UnitType {
12- RightRail = "right_rail" ,
1312 BottomRail = "bottom_rail" ,
14- LeftRail = "left_rail " ,
15- Skyscraper = "sky_atf " ,
13+ Skyscraper160x600 = "sky_atf " ,
14+ Skyscraper300x600 = "sky_btf " ,
1615}
1716
17+ export const adSpaceSize = {
18+ [ UnitType . BottomRail ] : { height : 50 , width : 320 } ,
19+ [ UnitType . Skyscraper160x600 ] : { height : 600 , width : 160 } ,
20+ [ UnitType . Skyscraper300x600 ] : { height : 600 , width : 300 } ,
21+ } ;
22+
1823interface AdSpaceProps {
1924 name : string ;
2025 unitType : UnitType ;
26+ withoutHeader ?: boolean ;
2127}
2228
23- const AdSpace : React . FC < AdSpaceProps > = ( { name, unitType } ) => {
29+ interface AdSpaceWrapperProps {
30+ children : React . ReactElement ;
31+ withoutHeader ?: boolean ;
32+ }
33+
34+ const AdSpaceWrapper : React . FC < AdSpaceWrapperProps > = ( { children, withoutHeader } ) => {
35+ const { t } = useTranslation ( ) ;
36+ return (
37+ < Box
38+ sx = { {
39+ alignItems : "center" ,
40+ display : "flex" ,
41+ flexDirection : "column" ,
42+ justifyContent : "center" ,
43+ textAlign : "center" ,
44+ } }
45+ >
46+ { withoutHeader ? null : < ListSubheader sx = { { userSelect : "none" } } > { t ( "misc.ad" ) } </ ListSubheader > }
47+ < Box > { children } </ Box >
48+ </ Box >
49+ ) ;
50+ } ;
51+
52+ const AdSpace : React . FC < AdSpaceProps > = ( { name, unitType, withoutHeader } ) => {
2453 const theme = useTheme ( ) ;
25- const isDevMode = useDevMode ( ) ;
2654
27- const events = useAtomValue ( eventsAtom ) ;
55+ const [ events , setEvents ] = useAtom ( eventsAtom ) ;
2856
2957 const selectorName = `dbu_${ md5 ( name + unitType . toString ( ) ) } ` ;
3058
59+ const size = unitType in adSpaceSize ? adSpaceSize [ unitType as keyof typeof adSpaceSize ] : null ;
60+
3161 useEffect ( ( ) => {
62+ if ( events . playwireInitializedUnits . indexOf ( name ) > - 1 ) {
63+ log . error ( `ramp: Unit ${ name } (${ unitType } ) has already been initialized...` ) ;
64+ return ;
65+ }
66+
67+ if ( DB_DISPLAY_AD_PLACEHOLDERS ) {
68+ setEvents ( playwireAddInitializedUnit ( name ) ) ;
69+ log . debug ( `not ramp: initialized unit ${ name } (${ unitType } )` ) ;
70+ }
71+
72+ if ( ! adsEnabled ) {
73+ return ;
74+ }
75+
3276 if ( ! events . playwireSetupHasFinished ) {
3377 return ;
3478 }
@@ -46,34 +90,46 @@ const AdSpace: React.FC<AdSpaceProps> = ({ name, unitType }) => {
4690 log . error ( "ramp: could not add unit" , { error } ) ;
4791 window . ramp . displayUnits ( ) ;
4892 }
93+
94+ setEvents ( playwireAddInitializedUnit ( name ) ) ;
95+ log . debug ( `ramp: initialized unit ${ name } (${ unitType } )` ) ;
4996 } ;
5097 initUnit ( ) ;
51- } , [ events . playwireSetupHasFinished , selectorName , unitType ] ) ;
98+ } , [ events . playwireSetupHasFinished , events . playwireInitializedUnits , selectorName , unitType , setEvents , name ] ) ;
5299
53- if ( ! adsEnabled ) {
54- return null ;
55- }
56-
57- if ( isDevMode ) {
100+ if ( DB_DISPLAY_AD_PLACEHOLDERS ) {
58101 return (
59- < Box
60- sx = { {
61- alignItems : "center" ,
62- border : `5px dashed ${ theme . palette . primary . main } ` ,
63- color : theme . palette . primary . main ,
64- display : "flex" ,
65- flexGrow : 10 ,
66- justifyContent : "center" ,
67- m : 1 ,
68- p : 1 ,
69- } }
70- >
71- < FeaturedVideo />
72- </ Box >
102+ < AdSpaceWrapper withoutHeader = { withoutHeader } >
103+ < Box
104+ id = { selectorName }
105+ sx = { {
106+ alignItems : "center" ,
107+ background : theme . palette . background . default ,
108+ border : `5px dashed ${ theme . palette . primary . main } ` ,
109+ color : theme . palette . primary . main ,
110+ display : "flex" ,
111+ height : `${ size ?. height } px` ,
112+ justifyContent : "center" ,
113+ m : 1 ,
114+ p : 1 ,
115+ width : `${ size ?. width } px` ,
116+ } }
117+ >
118+ < FeaturedVideo />
119+ </ Box >
120+ </ AdSpaceWrapper >
73121 ) ;
74122 }
75123
76- return < div id = { selectorName } /> ;
124+ if ( ! adsEnabled ) {
125+ return null ;
126+ }
127+
128+ return (
129+ < AdSpaceWrapper >
130+ < div id = { selectorName } />
131+ </ AdSpaceWrapper >
132+ ) ;
77133} ;
78134
79135export default React . memo ( AdSpace ) ;
0 commit comments