@zydon/common 2.7.2 → 2.7.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.
|
@@ -3,6 +3,6 @@ import '../../chunk-IHAHXNLH.js';
|
|
|
3
3
|
import { createContext, useState, useMemo, useEffect, useCallback, useContext } from 'react';
|
|
4
4
|
import { jsx } from 'react/jsx-runtime';
|
|
5
5
|
|
|
6
|
-
var j=createContext(void 0),ee=({children:l,originId:m,originType:M,bearerToken:p,vapidKey:C,serviceWorkerPath:S="/",serviceWorkerFile:T="service-worker.js",pollingInterval:N=5e3,apiAdapters:r,onMessageReceived:f,onError:
|
|
6
|
+
var j=createContext(void 0),ee=({children:l,originId:m,originType:M,bearerToken:p,vapidKey:C,serviceWorkerPath:S="/",serviceWorkerFile:T="service-worker.js",pollingInterval:N=5e3,apiAdapters:r,onMessageReceived:f,onError:o})=>{let[s,E]=useState(null),[x,k]=useState(null),[D,d]=useState(0),[L,h]=useState(!1),[B,b]=useState(!1),[I,F]=useState({subscriptions:[]}),[R,W]=useState(!1),z=useMemo(()=>!s||R,[s,R]);useEffect(()=>{if(!B||!s||!r.fetchMessages)return;let t=setInterval(async()=>{try{let a=(await r.fetchMessages(s))?.messages?.[0];a&&(k(a),d(i=>i+1),f?.(a));}catch(e){console.error("Erro ao buscar mensagens:",e);}},N);return ()=>clearInterval(t)},[B,s,r,N,f]);let v=useCallback(async t=>{try{let e=t?.toJSON().keys,a={origin_type:M,origin_id:m,endpoint:t?.endpoint??void 0,auth:e?.auth??void 0,p256dh:e?.p256dh??void 0},i=await r.registerClient(a);return E(i.id),h(!1),i.id}catch(e){return console.error("Erro ao registrar cliente:",e),h(!0),o?.(e),null}},[r,m,M,o]);useEffect(()=>{let t=!0,e=null;async function a$1(){if(!("serviceWorker"in navigator)||!("PushManager"in window)){console.warn("Push notifications n\xE3o suportadas, usando polling");let i=await v(null);t&&(E(i),b(!0));return}try{let i=await navigator.serviceWorker.register(`${S}${T}`);if(!t)return;e=async y=>{let P=y.data?.payload;if(P&&(k(P),d(g=>g+1),f?.(P)),y.data?.id?.value)try{await r.updateMessageStatus(y.data.id.value,{status:"DELIVERED"});}catch(g){console.error("Erro ao atualizar status da mensagem:",g),h(!0),o?.(g);}},navigator.serviceWorker.addEventListener("message",e);let K=await Notification.requestPermission(),H=null;K==="granted"?H=await i.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:a(C)}):(console.warn("Permiss\xE3o de notifica\xE7\xE3o negada, usando polling"),b(!0)),await v(H);}catch(i){console.error("Erro ao inicializar service worker:",i),b(!0),o?.(i);}}return p&&a$1(),()=>{t=!1,e&&"serviceWorker"in navigator&&navigator.serviceWorker.removeEventListener("message",e);}},[p,v,r,C,S,T,f,o]);let n=useCallback(async()=>{if(s){W(!0);try{let t=await r.listSubscriptions(s);F(t);}catch(t){console.error("Erro ao listar inscri\xE7\xF5es:",t),o?.(t);}finally{W(!1);}}},[s,r,o]);useEffect(()=>{s&&n();},[s]);let O=useCallback(async t=>{if(s)try{await r.subscribeTopic(s,t),await n();}catch(e){console.error("Erro ao inscrever em t\xF3pico:",e),o?.(e);}},[s,r,n,o]),V=useCallback(async t=>{if(s)try{await r.unsubscribeTopic(s,t),await n();}catch(e){console.error("Erro ao desinscrever de t\xF3pico:",e),o?.(e);}},[s,r,n,o]),_=useCallback(async t=>{if(!(!s||t.length===0))try{await r.subscribeTopicsBatch(s,t),await n();}catch(e){console.error("Erro ao inscrever em t\xF3picos:",e),o?.(e);}},[s,r,n,o]),$=useCallback(async t=>{if(!(!s||t.length===0))try{await r.unsubscribeTopicsBatch(s,t),await n();}catch(e){console.error("Erro ao desinscrever de t\xF3picos:",e),o?.(e);}},[s,r,n,o]),q=useCallback(()=>{d(0);},[]),G=useMemo(()=>({clientId:s,subscribedTopics:I,isRegisterError:L,isLoading:z,lastMessage:x,newMessageCount:D,subscribeToTopic:O,unsubscribeFromTopic:V,subscribeTopicsBatch:_,unsubscribeTopicsBatch:$,resetMessageCount:q}),[s,I,L,z,x,D,O,V,_,$,q]);return jsx(j.Provider,{value:G,children:l})};function se(){let l=useContext(j);if(!l)throw new Error("usePushNotifications precisa estar dentro de PushNotificationProvider");return l}
|
|
7
7
|
|
|
8
8
|
export { ee as PushNotificationProvider, se as usePushNotifications };
|