Skip to content

Commit 3075fe7

Browse files
authored
feat: banner campaigns (#2547)
1 parent 4a49a88 commit 3075fe7

File tree

7 files changed

+133
-95
lines changed

7 files changed

+133
-95
lines changed

src/layouts/MainLayout.tsx

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,38 @@ import { AppFooter } from './AppFooter';
1010
import { AppHeader } from './AppHeader';
1111
import TopBarNotify from './TopBarNotify';
1212

13-
// const SwitchIcon = () => (
14-
// <svg
15-
// xmlns="http://www.w3.org/2000/svg"
16-
// fill="none"
17-
// viewBox="0 0 24 24"
18-
// strokeWidth="1.5"
19-
// stroke="currentColor"
20-
// style={{ marginLeft: '8px', width: '24px', height: '24px' }}
21-
// >
22-
// <path
23-
// strokeLinecap="round"
24-
// strokeLinejoin="round"
25-
// d="M7.5 21L3 16.5m0 0L7.5 12M3 16.5h13.5m0-13.5L21 7.5m0 0L16.5 12M21 7.5H7.5"
26-
// />
27-
// </svg>
28-
// );
13+
const getCampaignConfigs = (openSwitch: (token?: string, chainId?: number) => void) => ({
14+
[ChainId.base]: {
15+
notifyText: 'A new incentives campaign is live on the Base market',
16+
buttonText: 'Explore Base',
17+
buttonAction: {
18+
type: 'route' as const,
19+
value: '/markets/?marketName=proto_base_v3',
20+
},
21+
bannerVersion: 'base-incentives-v1',
22+
icon: '/icons/networks/base.svg',
23+
},
24+
25+
[ChainId.sonic]: {
26+
notifyText: 'Swaps are now live on Sonic',
27+
buttonText: 'Swap Now',
28+
buttonAction: {
29+
type: 'function' as const,
30+
value: () => openSwitch('', ChainId.sonic),
31+
},
32+
bannerVersion: 'sonic-incentives-v1',
33+
icon: '/icons/networks/sonic.svg',
34+
},
35+
});
2936

3037
export function MainLayout({ children }: { children: ReactNode }) {
31-
const APP_BANNER_VERSION = '8.0.0';
3238
const { openSwitch } = useModalContext();
3339

34-
const handleLearnMore = () => {
35-
openSwitch('', ChainId.sonic);
36-
};
40+
const campaignConfigs = getCampaignConfigs(openSwitch);
3741

3842
return (
3943
<>
40-
<TopBarNotify
41-
learnMoreLink={handleLearnMore}
42-
notifyText="Swaps are now live on Sonic"
43-
bannerVersion={APP_BANNER_VERSION}
44-
/>
44+
<TopBarNotify campaigns={campaignConfigs} />
4545

4646
<AppHeader />
4747
<Box component="main" sx={{ display: 'flex', flexDirection: 'column', flex: 1 }}>

src/layouts/TopBarNotify.tsx

Lines changed: 104 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,113 @@ import Box from '@mui/material/Box';
66
import Button from '@mui/material/Button';
77
import Toolbar from '@mui/material/Toolbar';
88
import Typography from '@mui/material/Typography';
9+
import { useRouter } from 'next/router';
910
import { ReactNode, useEffect, useState } from 'react';
1011
import { MarketLogo } from 'src/components/MarketSwitcher';
1112
import { Link } from 'src/components/primitives/Link';
1213
import { useRootStore } from 'src/store/root';
1314

14-
interface TopBarNotifyProps {
15+
export type ButtonAction =
16+
| { type: 'url'; value: string; target?: '_blank' | '_self' }
17+
| { type: 'function'; value: () => void }
18+
| { type: 'route'; value: string }
19+
| { type: 'modal'; value: string; params?: Record<string, unknown> };
20+
21+
interface CampaignConfig {
1522
notifyText: ReactNode;
1623
learnMoreLink?: string | (() => void);
1724
buttonText?: string;
25+
buttonAction?: ButtonAction;
1826
bannerVersion: string;
1927
icon?: string;
2028
customIcon?: ReactNode;
2129
}
2230

23-
export default function TopBarNotify({
24-
notifyText,
25-
learnMoreLink,
26-
buttonText,
27-
bannerVersion,
28-
icon,
29-
customIcon,
30-
}: TopBarNotifyProps) {
31+
interface NetworkCampaigns {
32+
[chainId: number]: CampaignConfig;
33+
}
34+
35+
interface TopBarNotifyProps {
36+
campaigns: NetworkCampaigns;
37+
}
38+
39+
export default function TopBarNotify({ campaigns }: TopBarNotifyProps) {
3140
const { breakpoints } = useTheme();
3241
const md = useMediaQuery(breakpoints.down('md'));
3342
const sm = useMediaQuery(breakpoints.down('sm'));
43+
const router = useRouter();
44+
45+
const currentChainId = useRootStore((store) => store.currentChainId);
46+
const mobileDrawerOpen = useRootStore((state) => state.mobileDrawerOpen);
47+
48+
const getCurrentCampaign = (): CampaignConfig | null => {
49+
return campaigns[currentChainId] || null;
50+
};
51+
52+
const currentCampaign = getCurrentCampaign();
3453

3554
const [showWarning, setShowWarning] = useState(() => {
36-
const storedBannerVersion = localStorage.getItem('bannerVersion');
37-
const warningBarOpen = localStorage.getItem('warningBarOpen');
55+
if (!currentCampaign) return false;
3856

39-
if (storedBannerVersion !== bannerVersion) {
57+
const storedBannerVersion = localStorage.getItem(`bannerVersion_${currentChainId}`);
58+
const warningBarOpen = localStorage.getItem(`warningBarOpen_${currentChainId}`);
59+
60+
if (storedBannerVersion !== currentCampaign.bannerVersion) {
4061
return true;
4162
}
4263

4364
return warningBarOpen !== 'false';
4465
});
4566

46-
const mobileDrawerOpen = useRootStore((state) => state.mobileDrawerOpen);
47-
4867
useEffect(() => {
49-
const storedBannerVersion = localStorage.getItem('bannerVersion');
68+
if (!currentCampaign) return;
5069

51-
if (storedBannerVersion !== bannerVersion) {
52-
localStorage.setItem('bannerVersion', bannerVersion);
53-
localStorage.setItem('warningBarOpen', 'true');
70+
const storedBannerVersion = localStorage.getItem(`bannerVersion_${currentChainId}`);
71+
72+
if (storedBannerVersion !== currentCampaign.bannerVersion) {
73+
localStorage.setItem(`bannerVersion_${currentChainId}`, currentCampaign.bannerVersion);
74+
localStorage.setItem(`warningBarOpen_${currentChainId}`, 'true');
5475
setShowWarning(true);
5576
}
56-
}, [bannerVersion]);
77+
}, [currentCampaign, currentChainId]);
78+
79+
// If no campaign is configured for the current network, don't show anything
80+
if (!currentCampaign) {
81+
return null;
82+
}
5783

5884
const handleClose = () => {
59-
localStorage.setItem('warningBarOpen', 'false');
85+
localStorage.setItem(`warningBarOpen_${currentChainId}`, 'false');
6086
setShowWarning(false);
6187
};
6288

89+
const handleButtonAction = () => {
90+
if (!currentCampaign.buttonAction) return;
91+
92+
switch (currentCampaign.buttonAction.type) {
93+
case 'url':
94+
if (currentCampaign.buttonAction.target === '_blank') {
95+
window.open(currentCampaign.buttonAction.value, '_blank');
96+
} else {
97+
window.location.href = currentCampaign.buttonAction.value;
98+
}
99+
break;
100+
case 'function':
101+
currentCampaign.buttonAction.value();
102+
break;
103+
case 'route':
104+
router.push(currentCampaign.buttonAction.value);
105+
break;
106+
// case 'modal':
107+
// console.log(
108+
// 'Modal action:',
109+
// currentCampaign.buttonAction.value,
110+
// currentCampaign.buttonAction.params
111+
// );
112+
break;
113+
}
114+
};
115+
63116
// Note: hide warnings when mobile menu is open
64117
if (mobileDrawerOpen) return null;
65118

@@ -92,20 +145,25 @@ export default function TopBarNotify({
92145
sx={{ display: 'flex', alignContent: 'center', alignItems: 'center' }}
93146
component="div"
94147
>
95-
<Trans>{notifyText}</Trans>
148+
<Trans>{currentCampaign.notifyText}</Trans>
96149

97-
{customIcon ? customIcon : null}
150+
{currentCampaign.customIcon ? currentCampaign.customIcon : null}
98151

99-
{icon && !sm ? <MarketLogo sx={{ ml: 2 }} size={32} logo={icon} /> : ''}
152+
{currentCampaign.icon && !sm ? (
153+
<MarketLogo sx={{ ml: 2 }} size={32} logo={currentCampaign.icon} />
154+
) : (
155+
''
156+
)}
100157

101-
{learnMoreLink && md ? (
102-
typeof learnMoreLink === 'string' ? (
158+
{currentCampaign.learnMoreLink && md ? (
159+
typeof currentCampaign.learnMoreLink === 'string' ? (
103160
<Link
104161
sx={{ color: 'white', textDecoration: 'underline', paddingLeft: 2 }}
105-
// target={'_blank'} Todo option to pass as prop
106-
href={learnMoreLink}
162+
href={currentCampaign.learnMoreLink}
107163
>
108-
<Trans>{buttonText ? buttonText : `Learn more`}</Trans>
164+
<Trans>
165+
{currentCampaign.buttonText ? currentCampaign.buttonText : `Learn more`}
166+
</Trans>
109167
</Link>
110168
) : (
111169
<Button
@@ -119,48 +177,32 @@ export default function TopBarNotify({
119177
padding: 0,
120178
marginLeft: 2,
121179
}}
122-
onClick={learnMoreLink}
180+
onClick={currentCampaign.learnMoreLink}
123181
>
124-
<Trans>{buttonText ? buttonText : `Swap Now`}</Trans>
182+
<Trans>
183+
{currentCampaign.buttonText ? currentCampaign.buttonText : `Learn more`}
184+
</Trans>
125185
</Button>
126186
)
127187
) : null}
128188
</Typography>
129189
</Box>
130190

131191
<Box>
132-
{!md && learnMoreLink ? (
133-
typeof learnMoreLink === 'string' ? (
134-
<Button
135-
component="a"
136-
// target={'_blank'} Todo option to pass as prop
137-
size="small"
138-
href={learnMoreLink}
139-
sx={{
140-
minWidth: '90px',
141-
marginLeft: 5,
142-
height: '24px',
143-
background: '#383D51',
144-
color: '#EAEBEF',
145-
}}
146-
>
147-
<Trans> {buttonText ? buttonText.toUpperCase() : `LEARN MORE`}</Trans>
148-
</Button>
149-
) : (
150-
<Button
151-
size="small"
152-
onClick={learnMoreLink}
153-
sx={{
154-
minWidth: '90px',
155-
marginLeft: 5,
156-
height: '24px',
157-
background: '#383D51',
158-
color: '#EAEBEF',
159-
}}
160-
>
161-
<Trans> {buttonText ? buttonText.toUpperCase() : `Swap Now`}</Trans>
162-
</Button>
163-
)
192+
{!md && currentCampaign.buttonText && currentCampaign.buttonAction ? (
193+
<Button
194+
size="small"
195+
onClick={handleButtonAction}
196+
sx={{
197+
minWidth: '90px',
198+
marginLeft: 5,
199+
height: '24px',
200+
background: '#383D51',
201+
color: '#EAEBEF',
202+
}}
203+
>
204+
<Trans>{currentCampaign.buttonText.toUpperCase()}</Trans>
205+
</Button>
164206
) : null}
165207
</Box>
166208
<Button

src/locales/el/messages.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/locales/en/messages.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/locales/en/messages.po

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,10 +1241,6 @@ msgstr "This represents the threshold at which a borrow position will be conside
12411241
msgid "Isolated"
12421242
msgstr "Isolated"
12431243

1244-
#: src/layouts/TopBarNotify.tsx
1245-
msgid "{notifyText}"
1246-
msgstr "{notifyText}"
1247-
12481244
#: src/components/transactions/Emode/EmodeActions.tsx
12491245
msgid "Switching E-Mode"
12501246
msgstr "Switching E-Mode"

src/locales/es/messages.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/locales/fr/messages.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)