@propknot/shared-ui 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -14496,7 +14496,9 @@ __export(index_exports, {
14496
14496
  setToastContext: () => setToastContext,
14497
14497
  showLocalNotification: () => showLocalNotification,
14498
14498
  showMessageNotification: () => showMessageNotification,
14499
+ subscribeToPushNotifications: () => subscribeToPushNotifications2,
14499
14500
  testNotificationCoverage: () => testNotificationCoverage,
14501
+ unsubscribeFromPushNotifications: () => unsubscribeFromPushNotifications,
14500
14502
  useAccessibleColors: () => useAccessibleColors,
14501
14503
  useAuth: () => useAuth,
14502
14504
  useContrastText: () => useContrastText,
@@ -19034,6 +19036,84 @@ var getNotificationPermission = () => {
19034
19036
  }
19035
19037
  return Notification.permission;
19036
19038
  };
19039
+ var urlBase64ToUint8Array = (base64String) => {
19040
+ const padding = "=".repeat((4 - base64String.length % 4) % 4);
19041
+ const base64 = (base64String + padding).replace(/-/g, "+").replace(/_/g, "/");
19042
+ const rawData = window.atob(base64);
19043
+ const outputArray = new Uint8Array(rawData.length);
19044
+ for (let i = 0; i < rawData.length; ++i) {
19045
+ outputArray[i] = rawData.charCodeAt(i);
19046
+ }
19047
+ return outputArray;
19048
+ };
19049
+ var subscribeToPushNotifications2 = async () => {
19050
+ if (!isPushNotificationSupported()) {
19051
+ throw new Error("Push notifications are not supported");
19052
+ }
19053
+ if (Notification.permission !== "granted") {
19054
+ throw new Error("Notification permission not granted");
19055
+ }
19056
+ try {
19057
+ const existingRegistrations = await navigator.serviceWorker.getRegistrations();
19058
+ for (let registration2 of existingRegistrations) {
19059
+ await registration2.unregister();
19060
+ }
19061
+ const registration = await navigator.serviceWorker.register("/sw.js", {
19062
+ scope: "/",
19063
+ updateViaCache: "none"
19064
+ // Don't cache the service worker
19065
+ });
19066
+ await navigator.serviceWorker.ready;
19067
+ let subscription = await registration.pushManager.getSubscription();
19068
+ if (!subscription) {
19069
+ const vapidPublicKey = process.env.REACT_APP_VAPID_PUBLIC_KEY || "BJ2eTitSHPWl9WBAk0F1Drwmv6bQB48aDpM1ilutZcdygYfpbK7ODrlMSzvplaHWVwPtvQ1x-Y2SK2LadSQZ2mc";
19070
+ const subscriptionOptions = {
19071
+ userVisibleOnly: true,
19072
+ applicationServerKey: urlBase64ToUint8Array(vapidPublicKey)
19073
+ };
19074
+ subscription = await registration.pushManager.subscribe(subscriptionOptions);
19075
+ }
19076
+ await api_default.post("/notifications/subscribe", {
19077
+ subscription: subscription.toJSON()
19078
+ });
19079
+ return subscription;
19080
+ } catch (error) {
19081
+ console.error("Error subscribing to push notifications:", error);
19082
+ console.error("Error name:", error.name);
19083
+ console.error("Error message:", error.message);
19084
+ console.error("Error stack:", error.stack);
19085
+ if (error.name === "AbortError") {
19086
+ throw new Error("Push subscription was aborted. This might be due to invalid VAPID keys or browser restrictions.");
19087
+ } else if (error.name === "NotAllowedError") {
19088
+ throw new Error("Push notifications permission was denied by the user.");
19089
+ } else if (error.name === "NotSupportedError") {
19090
+ throw new Error("Push notifications are not supported in this browser.");
19091
+ }
19092
+ throw error;
19093
+ }
19094
+ };
19095
+ var unsubscribeFromPushNotifications = async () => {
19096
+ if (!isPushNotificationSupported()) {
19097
+ return;
19098
+ }
19099
+ try {
19100
+ const registration = await navigator.serviceWorker.getRegistration();
19101
+ if (!registration) {
19102
+ return;
19103
+ }
19104
+ const subscription = await registration.pushManager.getSubscription();
19105
+ if (!subscription) {
19106
+ return;
19107
+ }
19108
+ await subscription.unsubscribe();
19109
+ await api_default.post("/notifications/unsubscribe", {
19110
+ endpoint: subscription.endpoint
19111
+ });
19112
+ } catch (error) {
19113
+ console.error("Error unsubscribing from push notifications:", error);
19114
+ throw error;
19115
+ }
19116
+ };
19037
19117
  var isPushSubscribed = async () => {
19038
19118
  if (!isPushNotificationSupported()) {
19039
19119
  return false;
@@ -19149,7 +19229,9 @@ var isTenantAuthenticated = () => {
19149
19229
  setToastContext,
19150
19230
  showLocalNotification,
19151
19231
  showMessageNotification,
19232
+ subscribeToPushNotifications,
19152
19233
  testNotificationCoverage,
19234
+ unsubscribeFromPushNotifications,
19153
19235
  useAccessibleColors,
19154
19236
  useAuth,
19155
19237
  useContrastText,
package/dist/index.mjs CHANGED
@@ -19046,6 +19046,84 @@ var getNotificationPermission = () => {
19046
19046
  }
19047
19047
  return Notification.permission;
19048
19048
  };
19049
+ var urlBase64ToUint8Array = (base64String) => {
19050
+ const padding = "=".repeat((4 - base64String.length % 4) % 4);
19051
+ const base64 = (base64String + padding).replace(/-/g, "+").replace(/_/g, "/");
19052
+ const rawData = window.atob(base64);
19053
+ const outputArray = new Uint8Array(rawData.length);
19054
+ for (let i = 0; i < rawData.length; ++i) {
19055
+ outputArray[i] = rawData.charCodeAt(i);
19056
+ }
19057
+ return outputArray;
19058
+ };
19059
+ var subscribeToPushNotifications2 = async () => {
19060
+ if (!isPushNotificationSupported()) {
19061
+ throw new Error("Push notifications are not supported");
19062
+ }
19063
+ if (Notification.permission !== "granted") {
19064
+ throw new Error("Notification permission not granted");
19065
+ }
19066
+ try {
19067
+ const existingRegistrations = await navigator.serviceWorker.getRegistrations();
19068
+ for (let registration2 of existingRegistrations) {
19069
+ await registration2.unregister();
19070
+ }
19071
+ const registration = await navigator.serviceWorker.register("/sw.js", {
19072
+ scope: "/",
19073
+ updateViaCache: "none"
19074
+ // Don't cache the service worker
19075
+ });
19076
+ await navigator.serviceWorker.ready;
19077
+ let subscription = await registration.pushManager.getSubscription();
19078
+ if (!subscription) {
19079
+ const vapidPublicKey = process.env.REACT_APP_VAPID_PUBLIC_KEY || "BJ2eTitSHPWl9WBAk0F1Drwmv6bQB48aDpM1ilutZcdygYfpbK7ODrlMSzvplaHWVwPtvQ1x-Y2SK2LadSQZ2mc";
19080
+ const subscriptionOptions = {
19081
+ userVisibleOnly: true,
19082
+ applicationServerKey: urlBase64ToUint8Array(vapidPublicKey)
19083
+ };
19084
+ subscription = await registration.pushManager.subscribe(subscriptionOptions);
19085
+ }
19086
+ await api_default.post("/notifications/subscribe", {
19087
+ subscription: subscription.toJSON()
19088
+ });
19089
+ return subscription;
19090
+ } catch (error) {
19091
+ console.error("Error subscribing to push notifications:", error);
19092
+ console.error("Error name:", error.name);
19093
+ console.error("Error message:", error.message);
19094
+ console.error("Error stack:", error.stack);
19095
+ if (error.name === "AbortError") {
19096
+ throw new Error("Push subscription was aborted. This might be due to invalid VAPID keys or browser restrictions.");
19097
+ } else if (error.name === "NotAllowedError") {
19098
+ throw new Error("Push notifications permission was denied by the user.");
19099
+ } else if (error.name === "NotSupportedError") {
19100
+ throw new Error("Push notifications are not supported in this browser.");
19101
+ }
19102
+ throw error;
19103
+ }
19104
+ };
19105
+ var unsubscribeFromPushNotifications = async () => {
19106
+ if (!isPushNotificationSupported()) {
19107
+ return;
19108
+ }
19109
+ try {
19110
+ const registration = await navigator.serviceWorker.getRegistration();
19111
+ if (!registration) {
19112
+ return;
19113
+ }
19114
+ const subscription = await registration.pushManager.getSubscription();
19115
+ if (!subscription) {
19116
+ return;
19117
+ }
19118
+ await subscription.unsubscribe();
19119
+ await api_default.post("/notifications/unsubscribe", {
19120
+ endpoint: subscription.endpoint
19121
+ });
19122
+ } catch (error) {
19123
+ console.error("Error unsubscribing from push notifications:", error);
19124
+ throw error;
19125
+ }
19126
+ };
19049
19127
  var isPushSubscribed = async () => {
19050
19128
  if (!isPushNotificationSupported()) {
19051
19129
  return false;
@@ -19160,7 +19238,9 @@ export {
19160
19238
  setToastContext,
19161
19239
  showLocalNotification,
19162
19240
  showMessageNotification,
19241
+ subscribeToPushNotifications2 as subscribeToPushNotifications,
19163
19242
  testNotificationCoverage,
19243
+ unsubscribeFromPushNotifications,
19164
19244
  useAccessibleColors,
19165
19245
  useAuth,
19166
19246
  useContrastText,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@propknot/shared-ui",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "files": [