Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit c31cf25

Browse files
committed
improve pw unit logic
1 parent 018f8bd commit c31cf25

File tree

12 files changed

+227
-89
lines changed

12 files changed

+227
-89
lines changed

src/@types/globals.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
declare const DB_BUILD_TIME: number;
22
declare const DB_ENABLE_ADS: boolean;
33
declare const DB_DEVMODE: boolean;
4+
declare const DB_DISPLAY_AD_PLACEHOLDERS: boolean;
45
declare const DB_GA4_MEASUREMENT_ID: string | null;
56
declare const DB_PW_PUBLISHER_ID: string | null;
67
declare const DB_PW_WEBSITE_ID: string | null;

src/@types/playwire.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface Window {
99
passiveMode: boolean;
1010
addUnits: (units: PlaywireUnit[]) => Promise<void>;
1111
displayUnits: () => void;
12+
destroyUnits: (what: string) => void;
1213
};
1314

1415
_pwGA4PageviewId: string;

src/components/AdSpace.tsx

Lines changed: 88 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,78 @@
11
import { 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";
44
import { adsEnabled } from "@src/utils/env-tools";
55
import 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";
97
import md5 from "md5";
8+
import React, { useEffect } from "react";
9+
import { useTranslation } from "react-i18next";
1010

1111
export 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+
1823
interface 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

79135
export default React.memo(AdSpace);

src/components/AdSpaceFloating.tsx

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,66 @@
11
import { Box, useTheme } from "@mui/material";
2+
import { Breakpoint } from "@mui/system";
23
import AdSpace, { UnitType } from "@src/components/AdSpace";
3-
import useIsMobile from "@src/hooks/is-mobile";
44
import useWindowSize from "@src/hooks/window-size";
55
import { adsEnabled } from "@src/utils/env-tools";
66
import React from "react";
77

8-
export const adSpaceRightSideMinSize = 300;
9-
export const adSpaceMobileBannerHeight = 96;
8+
interface AdSpaceFloatingProps {
9+
name: string;
10+
unitType: UnitType;
11+
fromBreakpoint?: Breakpoint;
12+
untilBreakpoint?: Breakpoint;
13+
left?: number;
14+
right?: number;
15+
top?: number;
16+
bottom?: number;
17+
withoutHeader?: boolean;
18+
}
1019

11-
const AdSpaceFloating = () => {
20+
const AdSpaceFloating: React.FC<AdSpaceFloatingProps> = ({
21+
name,
22+
unitType,
23+
fromBreakpoint,
24+
untilBreakpoint,
25+
left,
26+
right,
27+
top,
28+
bottom,
29+
withoutHeader,
30+
}) => {
1231
const theme = useTheme();
13-
const isMobile = useIsMobile();
1432
const { width } = useWindowSize();
1533

16-
if (!adsEnabled) {
34+
if (!adsEnabled && !DB_DISPLAY_AD_PLACEHOLDERS) {
1735
return null;
1836
}
1937

20-
const rightSideSpace = (width - theme.breakpoints.values.xl) * 0.5;
21-
const rightSideHasEnoughSpace = rightSideSpace > adSpaceRightSideMinSize;
38+
const canRenderUsingFromRule = fromBreakpoint ? width >= theme.breakpoints.values[fromBreakpoint] : true;
39+
const canRenderUsingUntilRule = untilBreakpoint ? width <= theme.breakpoints.values[untilBreakpoint] : true;
2240

23-
const baseStyle = {
24-
background: theme.palette.background.default,
25-
display: "flex",
26-
position: "fixed",
27-
};
28-
29-
if (!isMobile && !rightSideHasEnoughSpace) {
41+
if (!canRenderUsingFromRule || !canRenderUsingUntilRule) {
3042
return null;
3143
}
3244

33-
const style = {
34-
...baseStyle,
35-
...(isMobile
36-
? {
37-
bottom: 0,
38-
height: `${adSpaceMobileBannerHeight}px`,
39-
left: 0,
40-
right: 0,
41-
}
42-
: {
43-
bottom: 0,
44-
right: 0,
45-
top: 64,
46-
width: `${rightSideSpace - adSpaceRightSideMinSize * 0.5}px`,
47-
}),
48-
};
49-
5045
return (
51-
<Box sx={style}>
52-
<AdSpace name={"AdSpaceFloating"} unitType={isMobile ? UnitType.BottomRail : UnitType.Skyscraper} />
46+
<Box
47+
sx={{
48+
bottom,
49+
display: "flex",
50+
justifyContent: "center",
51+
left,
52+
pointerEvents: "none",
53+
position: "fixed",
54+
right,
55+
top,
56+
zIndex: 99999,
57+
}}
58+
>
59+
<AdSpace
60+
name={name}
61+
unitType={unitType}
62+
withoutHeader={withoutHeader}
63+
/>
5364
</Box>
5465
);
5566
};

src/components/BuildMenu.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { Bookmark, BookmarkBorder, ContentCopy, Undo } from "@mui/icons-material";
22
import { Fab, IconButton, useTheme } from "@mui/material";
3-
import { adSpaceMobileBannerHeight } from "@src/components/AdSpaceFloating";
3+
import { adSpaceSize, UnitType } from "@src/components/AdSpace";
44
import InputDialog from "@src/components/InputDialog";
5+
import { playwireUnitMobileBottomRail } from "@src/constants";
56
import useIsMobile from "@src/hooks/is-mobile";
67
import { buildModelView, lastSelectedBuildModelView } from "@src/state/build";
8+
import { eventsAtom, playwireIsUnitInitialized } from "@src/state/events";
79
import {
810
addFavorite,
911
favoritesAtom,
@@ -41,6 +43,8 @@ const BuildMenu: React.FC = () => {
4143
const isFavorite = isBuildInFavorites(favorites, buildId);
4244
const isCopyToClipboardEnabled = navigator.clipboard !== undefined;
4345

46+
const events = useAtomValue(eventsAtom);
47+
4448
if (buildIdRegex.exec(location.pathname) === null) {
4549
return null;
4650
}
@@ -95,7 +99,14 @@ const BuildMenu: React.FC = () => {
9599
color="primary"
96100
onClick={handleCopyToClipboardClicked}
97101
sx={{
98-
bottom: adsEnabled ? `${adSpaceMobileBannerHeight}px` : theme.spacing(2),
102+
bottom:
103+
(adsEnabled || DB_DISPLAY_AD_PLACEHOLDERS) &&
104+
playwireIsUnitInitialized(events, playwireUnitMobileBottomRail)
105+
? `${
106+
adSpaceSize[UnitType.BottomRail].height +
107+
parseInt(theme.spacing(3).slice(0, -2))
108+
}px`
109+
: theme.spacing(2), // TODO: edit this
99110
position: "fixed",
100111
right: theme.spacing(3),
101112
}}

src/components/Layout.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,21 @@ import {
3131
Typography,
3232
useTheme,
3333
} from "@mui/material";
34-
import AdSpace, { UnitType } from "@src/components/AdSpace";
35-
import AdSpaceFloating, { adSpaceRightSideMinSize } from "@src/components/AdSpaceFloating";
34+
import { UnitType } from "@src/components/AdSpace";
35+
import AdSpaceFloating from "@src/components/AdSpaceFloating";
3636
import BuildMenu from "@src/components/BuildMenu";
3737
import LinkBox from "@src/components/LinkBox";
3838
import SomethingWentWrong from "@src/components/SometingWentWrong";
3939
import Spacer from "@src/components/Spacer";
4040
import { drawerWidth } from "@src/components/theme";
41-
import { crowdinLink, discordServerUrl, githubUrl, xTwitterUrl } from "@src/constants";
41+
import {
42+
crowdinLink,
43+
discordServerUrl,
44+
githubUrl,
45+
playwireUnitMobileBottomRail,
46+
playwireUnitRightSide,
47+
xTwitterUrl,
48+
} from "@src/constants";
4249
import dauntlessBuilderData from "@src/data/Data";
4350
import useDevMode from "@src/hooks/dev-mode";
4451
import useIsMobile from "@src/hooks/is-mobile";
@@ -263,7 +270,23 @@ const Layout: React.FC<LayoutProps> = ({ children }) => {
263270
</ErrorBoundary>
264271
</Container>
265272

266-
<AdSpaceFloating />
273+
<AdSpaceFloating
274+
bottom={0}
275+
fromBreakpoint={"xl"}
276+
name={playwireUnitRightSide}
277+
right={0}
278+
unitType={UnitType.Skyscraper160x600}
279+
withoutHeader
280+
/>
281+
<AdSpaceFloating
282+
bottom={0}
283+
left={0}
284+
name={playwireUnitMobileBottomRail}
285+
right={0}
286+
unitType={UnitType.BottomRail}
287+
untilBreakpoint={"md"}
288+
withoutHeader
289+
/>
267290
</Box>
268291
);
269292
};

0 commit comments

Comments
 (0)