Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions packages/extension/src/libs/rate-state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,45 @@ export default class RateState {
this.storage = new BrowserStorage(InternalStorageNamespace.rateState);
}

async showPopup(): Promise<boolean> {
/**
*
* @param immediate
* @returns boolean
*
* allow popup to show immediately
*
*/
async showPopup(immediate: boolean = false): Promise<boolean> {
const state: IState | undefined = await this.storage.get(
StorageKeys.rateInfo,
);
const now = Date.now();
const popupTime = Date.now() + POPUP_TIME;

/**
* Case 1: if the user has already been asked after activity
* - set askedAfterActivity to true
* - set popupTime to now + 30 days
* Case 2: if the user has not rated (this means that the user got asked after activity)
* - askedAfterActivity is already true
* - set popupTime to now + 30 days
* Case 3: if the user has already rated
* - always return false
* Case 4: if no state exists
* - create a new state with askedAfterActivity = false
* - set popupTime to now + 30 days
* - return immediate
*/

if (state) {
if (!state.alreadyRated) {
const now = Date.now();
if (!state.askedAfterActivity) {
state.askedAfterActivity = true;

await this.storage.set(StorageKeys.rateInfo, state);
return true;
}
if (!state.alreadyRated) {
if (state.popupTime < now) {
const popupTime = Date.now() + POPUP_TIME;
state.popupTime = popupTime;

await this.storage.set(StorageKeys.rateInfo, state);
Expand All @@ -30,18 +58,18 @@ export default class RateState {
} else {
return false;
}

if (immediate) return false
}

// set value when state is not set
const popupTime = Date.now() + POPUP_TIME;
const newState: IState = {
popupTime,
alreadyRated: false,
askedAfterActivity: immediate,
};

this.storage.set(StorageKeys.rateInfo, newState);

return false;
return immediate;
}

async resetPopupTimer(): Promise<void> {
Expand All @@ -59,6 +87,7 @@ export default class RateState {
const newState: IState = {
alreadyRated: false,
popupTime,
askedAfterActivity: false,
};

await this.storage.set(StorageKeys.rateInfo, newState);
Expand Down
1 change: 1 addition & 0 deletions packages/extension/src/libs/rate-state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum StorageKeys {
export interface Rate {
alreadyRated: boolean;
popupTime: number;
askedAfterActivity: boolean;
}

export type IState = Rate;
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,19 @@ import { BitcoinNetwork } from '@/providers/bitcoin/types/bitcoin-network';
import BitcoinAPI from '@/providers/bitcoin/libs/api';
import { trackSendEvents } from '@/libs/metrics';
import { SendEventType } from '@/libs/metrics/types';
import RateState from '@/libs/rate-state';
import { useRateStore } from '@action/store/rate-store';

/** -------------------
* Rate
-------------------*/
const rateStore = useRateStore();
const { toggleRatePopup } = rateStore;

const KeyRing = new PublicKeyRing();
const route = useRoute();
const router = useRouter();
const rateState = new RateState();
const selectedNetwork: string = route.query.id as string;
const txData: VerifyTransactionParams = JSON.parse(
Buffer.from(route.query.txData as string, 'base64').toString('utf8'),
Expand Down Expand Up @@ -185,11 +194,13 @@ const sendAction = async () => {
if (getCurrentContext() === 'popup') {
setTimeout(() => {
isProcessing.value = false;
callToggleRate();
router.go(-2);
}, 4500);
} else {
setTimeout(() => {
isProcessing.value = false;
callToggleRate();
window.close();
}, 1500);
}
Expand All @@ -213,6 +224,16 @@ const sendAction = async () => {
errorMsg.value = JSON.stringify(error);
});
};

const callToggleRate = () => {
/**
* will only show the user if they haven't rated it
* and never been shown before
*/
rateState.showPopup(true).then(show => {
if (show) toggleRatePopup(true);
});
};
const isHasScroll = () => {
if (verifyScrollRef.value) {
return verifyScrollRef.value.$el.classList.contains('ps--active-y');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,19 @@ import { toBN } from 'web3-utils';
import { bufferToHex, toBase } from '@enkryptcom/utils';
import { trackSendEvents } from '@/libs/metrics';
import { SendEventType } from '@/libs/metrics/types';
import RateState from '@/libs/rate-state';
import { useRateStore } from '@action/store/rate-store';

/** -------------------
* Rate
-------------------*/
const rateStore = useRateStore();
const { toggleRatePopup } = rateStore;

const KeyRing = new PublicKeyRing();
const route = useRoute();
const router = useRouter();
const rateState = new RateState();
const selectedNetwork: string = route.query.id as string;
const txData: VerifyTransactionParams = JSON.parse(
Buffer.from(route.query.txData as string, 'base64').toString('utf8'),
Expand Down Expand Up @@ -141,6 +150,16 @@ const close = () => {
}
};

const callToggleRate = () => {
/**
* will only show the user if they haven't rated it
* and never been shown before
*/
rateState.showPopup(true).then(show => {
if (show) toggleRatePopup(true);
});
};

const sendAction = async () => {
isProcessing.value = true;
trackSendEvents(SendEventType.SendApprove, {
Expand Down Expand Up @@ -198,11 +217,13 @@ const sendAction = async () => {
if (getCurrentContext() === 'popup') {
setTimeout(() => {
isProcessing.value = false;
callToggleRate();
router.go(-2);
}, 4500);
} else {
setTimeout(() => {
isProcessing.value = false;
callToggleRate();
window.close();
}, 1500);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,21 @@ import KadenaAPI from '@/providers/kadena/libs/api';
import { KadenaNetwork } from '@/providers/kadena/types/kadena-network';
import { trackSendEvents } from '@/libs/metrics';
import { SendEventType } from '@/libs/metrics/types';
import RateState from '@/libs/rate-state';
import { useRateStore } from '@action/store/rate-store';

/** -------------------
* Rate
-------------------*/
const rateStore = useRateStore();
const { toggleRatePopup } = rateStore;

const isSendDone = ref(false);
const account = ref<EnkryptAccount>();
const chainId = ref<string>();
const kdaToken = ref<KDAToken>();
const KeyRing = new PublicKeyRing();
const rateState = new RateState();
const route = useRoute();
const router = useRouter();
const selectedNetwork: string = route.query.id as string;
Expand Down Expand Up @@ -197,11 +206,13 @@ const sendAction = async () => {
if (getCurrentContext() === 'popup') {
setTimeout(() => {
isProcessing.value = false;
callToggleRatePopup();
router.push({ name: 'activity', params: { id: network.value.name } });
}, 2500);
} else {
setTimeout(() => {
isProcessing.value = false;
callToggleRatePopup();
window.close();
}, 1500);
}
Expand All @@ -218,6 +229,16 @@ const sendAction = async () => {
}
};

const callToggleRatePopup = () => {
/**
* will only show the user if they haven't rated it
* and never been shown before
*/
rateState.showPopup(true).then(show => {
if (show) toggleRatePopup(true);
});
};

const isHasScroll = () => {
if (verifyScrollRef.value) {
return verifyScrollRef.value.$el.classList.contains('ps--active-y');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,19 @@ import CustomScrollbar from '@action/components/custom-scrollbar/index.vue';
import { BaseNetwork } from '@/types/base-network';
import { trackSendEvents } from '@/libs/metrics';
import { SendEventType } from '@/libs/metrics/types';
import RateState from '@/libs/rate-state';
import { useRateStore } from '@action/store/rate-store';

/** -------------------
* Rate
-------------------*/
const rateStore = useRateStore();
const { toggleRatePopup } = rateStore;

const isSendDone = ref(false);
const account = ref<EnkryptAccount>();
const KeyRing = new PublicKeyRing();
const rateState = new RateState();
const route = useRoute();
const router = useRouter();
const selectedNetwork: string = route.query.id as string;
Expand Down Expand Up @@ -207,11 +216,13 @@ const sendAction = async () => {
if (getCurrentContext() === 'popup') {
setTimeout(() => {
isProcessing.value = false;
callToggleRatePopup();
router.go(-2);
}, 2500);
} else {
setTimeout(() => {
isProcessing.value = false;
callToggleRatePopup();
window.close();
}, 1500);
}
Expand Down Expand Up @@ -240,6 +251,16 @@ const sendAction = async () => {
}
};

const callToggleRatePopup = () => {
/**
* will only show the user if they haven't rated it
* and never been shown before
*/
rateState.showPopup(true).then(show => {
if (show) toggleRatePopup(true);
});
};

const isHasScroll = () => {
if (verifyScrollRef.value) {
return verifyScrollRef.value.$el.classList.contains('ps--active-y');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,17 @@ import { getSimulationComputeUnits } from '@solana-developers/helpers';
import SolanaAPI from '@/providers/solana/libs/api';
import bs58 from 'bs58';
import { TransactionSigner } from '../../libs/signer';
import RateState from '@/libs/rate-state';
import { useRateStore } from '@action/store/rate-store';

/** -------------------
* Rate
-------------------*/
const rateStore = useRateStore();
const { toggleRatePopup } = rateStore;

const KeyRing = new PublicKeyRing();
const rateState = new RateState();
const route = useRoute();
const router = useRouter();
const selectedNetwork: string = route.query.id as string;
Expand Down Expand Up @@ -227,14 +236,23 @@ const sendAction = async () => {
},
);
isSendDone.value = true;
/**
* will only show the user if they haven't rated it
* and never been shown before
*/
rateState.showPopup(true).then(show => {
if (show) toggleRatePopup(true);
});
if (getCurrentContext() === 'popup') {
setTimeout(() => {
isProcessing.value = false;
callToggleRatePopup();
router.go(-2);
}, 4500);
} else {
setTimeout(() => {
isProcessing.value = false;
callToggleRatePopup();
window.close();
}, 1500);
}
Expand Down Expand Up @@ -275,6 +293,17 @@ const sendAction = async () => {
console.error('ERROR', errror);
});
};

const callToggleRatePopup = () => {
/**
* will only show the user if they haven't rated it
* and never been shown before
*/
rateState.showPopup(true).then(show => {
if (show) toggleRatePopup(true);
});
};

const isHasScroll = () => {
if (verifyScrollRef.value) {
return verifyScrollRef.value.$el.classList.contains('ps--active-y');
Expand Down
Loading
Loading