Skip to content

Commit

Permalink
Merge pull request #598 from near/DEC-1587-2
Browse files Browse the repository at this point in the history
Notifications - improve local storage
  • Loading branch information
marcinbodnar authored Sep 27, 2023
2 parents 86ed387 + 49dea50 commit 3d75436
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const HomePage: NextPageWithLayout = () => {
const tosAccepted =
tosData.agreementsForUser[tosData.agreementsForUser.length - 1].value === tosData.latestTosVersion;
// check if user has already turned on notifications
const { showOnTS } = getNotificationLocalStorage();
const { showOnTS } = getNotificationLocalStorage() || {};

if ((tosAccepted && !showOnTS) || (tosAccepted && showOnTS < Date.now())) {
setTimeout(() => {
Expand Down
101 changes: 92 additions & 9 deletions src/utils/notificationsLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@ import {
} from './notificationsHelpers';

export const NOTIFICATIONS_STORAGE = 'push-notifications-v0';
const LS_ACCOUNT_ID = 'near-social-vm:v01::accountId:';

const getLSAccountId = (): string => {
return localStorage.getItem(LS_ACCOUNT_ID) || '';
};

export const setHandleOnCancel = () => {
const accountIdLS = getLSAccountId();
const localStorageByAccountId = getNotificationLocalStorage();

localStorage.setItem(
NOTIFICATIONS_STORAGE,
JSON.stringify({
...getNotificationLocalStorage(),
...getNotificationLocalStorageFull(),
[accountIdLS]: {
...localStorageByAccountId,
showOnTS: Date.now() + 86400000, // 14 days
notNowTS: Date.now(),
bannerNotNowTS: undefined,
},
showOnTS: Date.now() + 86400000, // 14 days
notNowTS: Date.now(),
bannerNotNowTS: undefined,
Expand All @@ -20,20 +34,37 @@ export const setHandleOnCancel = () => {
};

export const setHandleOnCancelBanner = () => {
const accountIdLS = getLSAccountId();
const localStorageByAccountId = getNotificationLocalStorage();

localStorage.setItem(
NOTIFICATIONS_STORAGE,
JSON.stringify({
...getNotificationLocalStorage(),
...getNotificationLocalStorageFull(),
[accountIdLS]: {
...localStorageByAccountId,
bannerNotNowTS: Date.now(),
},
bannerNotNowTS: Date.now(),
}),
);
};

export const setProcessSuccess = () => {
const accountIdLS = getLSAccountId();
const localStorageByAccountId = getNotificationLocalStorage();

localStorage.setItem(
NOTIFICATIONS_STORAGE,
JSON.stringify({
...getNotificationLocalStorage(),
...getNotificationLocalStorageFull(),
[accountIdLS]: {
...localStorageByAccountId,
permission: true,
subscribeStarted: false,
subscribeError: '',
isPermisionGranted: true,
},
permission: true,
subscribeStarted: false,
subscribeError: '',
Expand All @@ -43,10 +74,19 @@ export const setProcessSuccess = () => {
};

export const setProcessError = (error: unknown) => {
const accountIdLS = getLSAccountId();
const localStorageByAccountId = getNotificationLocalStorage();

localStorage.setItem(
NOTIFICATIONS_STORAGE,
JSON.stringify({
...getNotificationLocalStorage(),
...getNotificationLocalStorageFull(),
[accountIdLS]: {
...localStorageByAccountId,
permission: false,
subscribeStarted: false,
subscribeError: error,
},
permission: false,
subscribeStarted: false,
subscribeError: error,
Expand All @@ -55,41 +95,84 @@ export const setProcessError = (error: unknown) => {
};

export const setProcessEnded = () => {
const accountIdLS = getLSAccountId();
const localStorageByAccountId = getNotificationLocalStorage();

localStorage.setItem(
NOTIFICATIONS_STORAGE,
JSON.stringify({
...getNotificationLocalStorage(),
...getNotificationLocalStorageFull(),
[accountIdLS]: {
...localStorageByAccountId,
subscribeStarted: false,
},
subscribeStarted: false,
}),
);
};

export const setProcessStarted = () => {
const accountIdLS = getLSAccountId();
const localStorageByAccountId = getNotificationLocalStorage();

localStorage.setItem(
NOTIFICATIONS_STORAGE,
JSON.stringify({
...getNotificationLocalStorage(),
...getNotificationLocalStorageFull(),
[accountIdLS]: {
...localStorageByAccountId,
permission: false,
subscribeStarted: true,
},
permission: false,
subscribeStarted: true,
}),
);
};

export const setClearData = () => {
localStorage.setItem(NOTIFICATIONS_STORAGE, JSON.stringify({}));
const accountIdLS = getLSAccountId();

localStorage.setItem(
NOTIFICATIONS_STORAGE,
JSON.stringify({
...getNotificationLocalStorageFull(),
[accountIdLS]: {},
}),
);
};

export const setNotificationsSessionStorage = () => {
const accountIdLS = getLSAccountId();
const localStorageByAccountId = getNotificationLocalStorage();

localStorage.setItem(
NOTIFICATIONS_STORAGE,
JSON.stringify({
...getNotificationLocalStorage(),
...getNotificationLocalStorageFull(),
[accountIdLS]: {
...localStorageByAccountId,
isNotificationSupported: isNotificationSupported(),
isPushManagerSupported: isPushManagerSupported(),
isPermisionGranted: isPermisionGranted(),
},
isNotificationSupported: isNotificationSupported(),
isPushManagerSupported: isPushManagerSupported(),
isPermisionGranted: isPermisionGranted(),
}),
);
};

export const getNotificationLocalStorage = () =>
export const getNotificationLocalStorageFull = () =>
isLocalStorageSupported() && JSON.parse(localStorage.getItem(NOTIFICATIONS_STORAGE) || '{}');

export const getNotificationLocalStorage = () => {
if (!isLocalStorageSupported()) {
return {};
}

const accountIdLS = getLSAccountId();

const notificationsLS = isLocalStorageSupported() && JSON.parse(localStorage.getItem(NOTIFICATIONS_STORAGE) || '{}');
return notificationsLS[accountIdLS];
};

0 comments on commit 3d75436

Please sign in to comment.