@propknot/shared-ui 1.0.1 → 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
@@ -14473,7 +14473,9 @@ __export(index_exports, {
14473
14473
  getNotificationPermission: () => getNotificationPermission,
14474
14474
  getRelativeLuminance: () => getRelativeLuminance,
14475
14475
  getStatusContrastColors: () => getStatusContrastColors,
14476
+ hasNotificationPermission: () => hasNotificationPermission,
14476
14477
  isColorDark: () => isColorDark,
14478
+ isNotificationSupported: () => isNotificationSupported,
14477
14479
  isPushNotificationSupported: () => isPushNotificationSupported,
14478
14480
  isPushSubscribed: () => isPushSubscribed,
14479
14481
  isTenantAuthenticated: () => isTenantAuthenticated,
@@ -14487,13 +14489,16 @@ __export(index_exports, {
14487
14489
  notifyMessage: () => notifyMessage,
14488
14490
  notifySuccess: () => notifySuccess,
14489
14491
  notifyTicketUpdate: () => notifyTicketUpdate,
14492
+ requestNotificationPermission: () => requestNotificationPermission2,
14490
14493
  rgbToHex: () => rgbToHex,
14491
14494
  sendTestNotification: () => sendTestNotification,
14492
14495
  setSocketContext: () => setSocketContext,
14493
14496
  setToastContext: () => setToastContext,
14494
14497
  showLocalNotification: () => showLocalNotification,
14495
14498
  showMessageNotification: () => showMessageNotification,
14499
+ subscribeToPushNotifications: () => subscribeToPushNotifications2,
14496
14500
  testNotificationCoverage: () => testNotificationCoverage,
14501
+ unsubscribeFromPushNotifications: () => unsubscribeFromPushNotifications,
14497
14502
  useAccessibleColors: () => useAccessibleColors,
14498
14503
  useAuth: () => useAuth,
14499
14504
  useContrastText: () => useContrastText,
@@ -19011,6 +19016,15 @@ var notificationManager = new NotificationManager2();
19011
19016
  var showMessageNotification = (senderName, messageContent, propertyAddress) => {
19012
19017
  notificationManager.showMessageNotification(senderName, messageContent, propertyAddress);
19013
19018
  };
19019
+ var requestNotificationPermission2 = () => {
19020
+ return notificationManager.requestPermission();
19021
+ };
19022
+ var hasNotificationPermission = () => {
19023
+ return notificationManager.hasPermission();
19024
+ };
19025
+ var isNotificationSupported = () => {
19026
+ return NotificationManager2.isSupported();
19027
+ };
19014
19028
 
19015
19029
  // src/utils/pushNotifications.js
19016
19030
  var isPushNotificationSupported = () => {
@@ -19022,6 +19036,84 @@ var getNotificationPermission = () => {
19022
19036
  }
19023
19037
  return Notification.permission;
19024
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
+ };
19025
19117
  var isPushSubscribed = async () => {
19026
19118
  if (!isPushNotificationSupported()) {
19027
19119
  return false;
@@ -19114,7 +19206,9 @@ var isTenantAuthenticated = () => {
19114
19206
  getNotificationPermission,
19115
19207
  getRelativeLuminance,
19116
19208
  getStatusContrastColors,
19209
+ hasNotificationPermission,
19117
19210
  isColorDark,
19211
+ isNotificationSupported,
19118
19212
  isPushNotificationSupported,
19119
19213
  isPushSubscribed,
19120
19214
  isTenantAuthenticated,
@@ -19128,13 +19222,16 @@ var isTenantAuthenticated = () => {
19128
19222
  notifyMessage,
19129
19223
  notifySuccess,
19130
19224
  notifyTicketUpdate,
19225
+ requestNotificationPermission,
19131
19226
  rgbToHex,
19132
19227
  sendTestNotification,
19133
19228
  setSocketContext,
19134
19229
  setToastContext,
19135
19230
  showLocalNotification,
19136
19231
  showMessageNotification,
19232
+ subscribeToPushNotifications,
19137
19233
  testNotificationCoverage,
19234
+ unsubscribeFromPushNotifications,
19138
19235
  useAccessibleColors,
19139
19236
  useAuth,
19140
19237
  useContrastText,
package/dist/index.mjs CHANGED
@@ -19026,6 +19026,15 @@ var notificationManager = new NotificationManager2();
19026
19026
  var showMessageNotification = (senderName, messageContent, propertyAddress) => {
19027
19027
  notificationManager.showMessageNotification(senderName, messageContent, propertyAddress);
19028
19028
  };
19029
+ var requestNotificationPermission2 = () => {
19030
+ return notificationManager.requestPermission();
19031
+ };
19032
+ var hasNotificationPermission = () => {
19033
+ return notificationManager.hasPermission();
19034
+ };
19035
+ var isNotificationSupported = () => {
19036
+ return NotificationManager2.isSupported();
19037
+ };
19029
19038
 
19030
19039
  // src/utils/pushNotifications.js
19031
19040
  var isPushNotificationSupported = () => {
@@ -19037,6 +19046,84 @@ var getNotificationPermission = () => {
19037
19046
  }
19038
19047
  return Notification.permission;
19039
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
+ };
19040
19127
  var isPushSubscribed = async () => {
19041
19128
  if (!isPushNotificationSupported()) {
19042
19129
  return false;
@@ -19128,7 +19215,9 @@ export {
19128
19215
  getNotificationPermission,
19129
19216
  getRelativeLuminance,
19130
19217
  getStatusContrastColors,
19218
+ hasNotificationPermission,
19131
19219
  isColorDark,
19220
+ isNotificationSupported,
19132
19221
  isPushNotificationSupported,
19133
19222
  isPushSubscribed,
19134
19223
  isTenantAuthenticated,
@@ -19142,13 +19231,16 @@ export {
19142
19231
  notifyMessage,
19143
19232
  notifySuccess,
19144
19233
  notifyTicketUpdate,
19234
+ requestNotificationPermission2 as requestNotificationPermission,
19145
19235
  rgbToHex,
19146
19236
  sendTestNotification,
19147
19237
  setSocketContext,
19148
19238
  setToastContext,
19149
19239
  showLocalNotification,
19150
19240
  showMessageNotification,
19241
+ subscribeToPushNotifications2 as subscribeToPushNotifications,
19151
19242
  testNotificationCoverage,
19243
+ unsubscribeFromPushNotifications,
19152
19244
  useAccessibleColors,
19153
19245
  useAuth,
19154
19246
  useContrastText,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@propknot/shared-ui",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "files": [