Skip to content

Commit

Permalink
feat(pwa-helper): user toast on sw signals
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Feb 3, 2023
1 parent e5b7d69 commit 2569728
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions ui/pwa-helper/src/signal/sw-user-notify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {replaceNumber, localeContextConsumer} from '@alwatr/i18n';
import {eventListener} from '@alwatr/signal';
import {snackbarSignalTrigger} from '@alwatr/ui-kit/snackbar/show-snackbar.js';
import {delay} from '@alwatr/util';

// FIXME: i18n

eventListener.subscribe('service_worker_registered', async () => {
if (localStorage.getItem('notify_new_version') !== null) {
if (localeContextConsumer.getValue() == null) {
await localeContextConsumer.untilChange();
}
localStorage.removeItem('notify_new_version');
snackbarSignalTrigger.request({
message: `به نسخه ${replaceNumber(_ALWATR_VERSION_)} خوش‌آمدید.`,
});
}
});

eventListener.subscribe('service_worker_installed', () => void snackbarSignalTrigger.request({
message: 'برنامه نصب شد و اکنون به صورت آفلاین در دسترس است.',
}));

eventListener.subscribe('service_worker_updated', async () => {
localStorage.setItem('notify_new_version', '');
const response = await snackbarSignalTrigger.requestWithResponse({
message: 'نسخه جدید این برنامه نصب و هم‌اکنون در دسترس است.',
actionLabel: 'به‌روزرسانی',
duration: -1,
});
if (response.actionButton) {
await delay(500);
window.location.reload();
}
});

0 comments on commit 2569728

Please sign in to comment.