Skip to content

Commit

Permalink
Adding promotional poppup
Browse files Browse the repository at this point in the history
  • Loading branch information
eyal-blockfence committed Nov 27, 2023
1 parent a0c13e8 commit 3109c90
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ You must provide a valid `.env` file with the following fields:
API_SERVER=
API_KEY=
AMPLITUDE_KEY=<optional>
PROMOTIONAL_URL=<https://....>
```

## Release
Expand Down
31 changes: 29 additions & 2 deletions src/helpers/showPopup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getEnableHooks, isMutedAddresses } from '../shared/storage';
import { getEnableHooks, getPromotionCounter, isMutedAddresses, setPromotionCounter } from '../shared/storage';
import { TransactionEvent } from '../types/internal';
import { getActiveTabUrl } from './getActiveTab';

Expand All @@ -9,6 +9,8 @@ const EXTENSION_HEIGHT = 600;
const FOCUS_TIMEOUT = 20;
const RETRIES_COUNT = 25;

const REQUIRED_PROMOTION_COUNT = 5;

async function getPosition() {
const latestWindow = await chrome.windows.getLastFocused();

Expand Down Expand Up @@ -68,7 +70,6 @@ export const showPopup = async (chainId: string, event: TransactionEvent) => {
const searchParams = new URLSearchParams(data);
const popupUrl = `walletpopup.html?${searchParams.toString()}`;

// const currentWindow =
await chrome.windows.getCurrent();
const popupWindow = await chrome.windows.create({
url: popupUrl,
Expand All @@ -90,9 +91,35 @@ export const showPopup = async (chainId: string, event: TransactionEvent) => {
// Focus Blockfence window
chrome.windows.update(popupWindow.id, { focused: true });
}

// const currentWindow =
showPromotion();
}
};

export const showPromotion = async () => {
if (!process.env.PROMOTIONAL_URL) return;
const promotionCounter = await getPromotionCounter();
setPromotionCounter(promotionCounter + 1);

if (promotionCounter !== REQUIRED_PROMOTION_COUNT) {
return;
}

const popupUrl = `process.env.PROMOTIONAL_URL`;
const { top, left } = await getPosition();

await chrome.windows.create({
url: popupUrl,
type: 'popup',
top,
left,
width: 300,
height: EXTENSION_HEIGHT,
focused: true,
});
};

function closeWindowWithOther(targetWindowId: number, listeningWindowId: number) {
function handler(tabId: number, removeInfo: chrome.tabs.TabRemoveInfo) {
if (removeInfo.windowId === listeningWindowId) {
Expand Down
9 changes: 9 additions & 0 deletions src/shared/storage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ export const getPrefferedChinId = async () => {
export const setPreferredChainId = async (chainId: string) => {
await chrome.storage.local.set({ preferredChainId: chainId });
};

export const getPromotionCounter = async () => {
const storage = await chrome.storage.local.get({ counter: true });
return storage.counter || 0;
};

export const setPromotionCounter = async (counter: number) => {
await chrome.storage.local.set({ counter });
};

0 comments on commit 3109c90

Please sign in to comment.