PageSourceSearch

https://www.sheconomy.in/assets/PushNotificationsProvider-D4wg1VRn.js

js sheconomy.in collected 2026-09-24 13:17:31 UTC 6,863 bytes, 1 lines download raw bytes

1import{o as e}from"./rolldown-runtime-DAXXjFlN.js";import{Lt as t,n,pt as r}from"./dist-E2Iv6aiw.js";import{t as i}from"./jsx-runtime-CFwixLRt.js";import{n as a}from"./client-DVSqKt4u.js";import{t as o}from"./navigate-BoVRbIm3.js";import{n as s}from"./SupabaseProvider-DTg8Q1qf.js";import{i as c,t as l}from"./storage-B6DlP3K_.js";import{n as u,r as d,t as f}from"./push-config-SmdIclCV.js";import{n as p}from"./push-client.web-DZkvuulP.js";var m=e(t());n();async function h(e){let t=e.playerId.trim();if(!t)return{error:`Missing push device id`};let n={user_id:e.userId,player_id:t,platform:e.platform??`web`,provider:e.provider??`onesignal`,enabled:!0,updated_at:new Date().toISOString()},{data:r,error:i}=await a.from(`user_push_devices`).upsert(n,{onConflict:`user_id,provider,player_id`}).select(`id, platform, player_id`).maybeSingle();if(i){let r=await a.from(`user_push_devices`).insert(n).select(`id`).maybeSingle();if(!r.error)return{error:null};let o=await a.from(`user_push_devices`).update({platform:n.platform,enabled:!0,updated_at:n.updated_at}).eq(`user_id`,e.userId).eq(`provider`,n.provider).eq(`player_id`,t).select(`id`).maybeSingle();if(o.error||!o.data)return{error:i.message||r.error?.message||o.error?.message||`Failed to save push device`}}if(!r&&!i){let{data:r,error:i}=await a.from(`user_push_devices`).select(`id`).eq(`user_id`,e.userId).eq(`provider`,n.provider).eq(`player_id`,t).eq(`enabled`,!0).maybeSingle();if(i||!r)return{error:i?.message||`Push device was not saved`}}return{error:null}}async function g(e,t){let{error:n}=await a.from(`user_push_devices`).update({enabled:!1,updated_at:new Date().toISOString()}).eq(`user_id`,e).eq(`player_id`,t);return{error:n?.message??null}}var _=`sheconomy:push-permission-decided-v5`;async function v(){let e=await l(_);if(!e)return{decided:!1};try{return{decided:JSON.parse(e).decided===!0}}catch{return{decided:!1}}}async function y(){return!(await v()).decided}async function b(){await c(_,JSON.stringify({decided:!0}))}async function x(){await c(_,JSON.stringify({decided:!1}))}var S=i(),C=(0,m.createContext)(null);async function w(e=10,t=600){let n=null;for(let r=0;r<e&&!n;r+=1)r>0&&await new Promise(e=>setTimeout(e,t)),n=await p.getPlayerId();return n}async function T(){await p.canRequestPermission()||await b()}function E({children:e}){let{user:t}=s(),[n,i]=(0,m.useState)(()=>!!u()),[a,c]=(0,m.useState)(!1),[l,_]=(0,m.useState)(null),[v,x]=(0,m.useState)(!1),[E,D]=(0,m.useState)(r.OS!==`web`),[O,k]=(0,m.useState)(()=>!!u()),[A,j]=(0,m.useState)(!1),M=(0,m.useRef)(null),N=(0,m.useRef)(!1),P=(0,m.useCallback)(()=>{j(!1)},[]);(0,m.useEffect)(()=>{D(!0)},[]),(0,m.useEffect)(()=>{if(!E)return;let e=!1;return(async()=>{try{let t=await f();if(e)return;i(!!t.appId)}catch(t){console.warn(`[push] config resolve failed`,t),e||i(!!u())}finally{e||k(!0)}})(),()=>{e=!0}},[E]);let F=(0,m.useCallback)(async(e,t)=>{if(!t)return _(null),null;await p.optIn();try{await p.login(e)}catch{}let n=await w(p.platform===`web`?16:10,700);if(!n&&p.platform===`web`&&(console.info(`[push] web permission granted but no subscription id — repairing`),await p.requestPermission()&&(await p.login(e),n=await w(16,700))),_(n),n){let{error:t}=await h({userId:e,playerId:n,platform:p.platform,provider:`onesignal`});if(t)return console.warn(`[push] registerPushDevice failed`,t),_(null),null;console.info(`[push] device registered`,{platform:p.platform,playerId:n})}else console.warn(`[push] permission granted but no player/subscription id yet`);return n},[]),I=(0,m.useCallback)(async()=>{if(!n)return{ok:!1,error:`Push is not configured (missing OneSignal App ID).`};if(!t?.id)return{ok:!1,error:`Sign in to enable push notifications.`};x(!0);try{await p.init(),await p.login(t.id),N.current=!0;let e=await p.requestPermission();if(c(e),await b(),!e)return j(!1),{ok:!1,error:p.getLastError?.()||`Permission was denied. Enable notifications in system settings.`};let n=await F(t.id,!0);return j(!1),n?(c(!0),{ok:!0}):{ok:!1,error:p.getLastError?.()||`Could not save this browser as a push device. Sign in again, allow notifications, then retry.`}}catch(e){return{ok:!1,error:e instanceof Error?e.message:`Failed to enable push`}}finally{x(!1)}},[n,F,t?.id]),L=(0,m.useCallback)(async()=>
1{if(!n||!t?.id){_(null);return}try{await p.init(),await p.login(t.id),N.current=!0;let e=await p.getPermission();c(e),await F(t.id,e),e&&await T()}catch(e){console.warn(`[push] syncDevice failed`,e)}},[n,F,t?.id]);(0,m.useEffect)(()=>{if(!E||!O||!n)return;let e=setTimeout(()=>{p.init().catch(e=>{console.warn(`[push] early init failed`,e)})},400);return()=>clearTimeout(e)},[E,O,n]),(0,m.useEffect)(()=>{E&&(t?.id||(M.current=null,_(null),c(!1),j(!1),N.current&&(N.current=!1,p.logout())))},[E,t?.id]),(0,m.useEffect)(()=>{if(!E||!O||!n||!t?.id||M.current===t.id)return;let e=t.id,i=!1,a=setTimeout(()=>{(async()=>{try{if(i||(console.info(`[push] login subscribe start`,{platform:p.platform,userId:e}),await p.init(),i)||(await p.login(e),N.current=!0,i))return;let t=await p.getPermission();if(i)return;if(t){let t=await F(e,!0);c(!0),j(!1),await b(),i||(M.current=e),console.info(t?`[push] already permitted — registered device`:`[push] already permitted — registration pending, no prompt`);return}let n=await p.canRequestPermission();if(i)return;if(!n){await b(),i||(M.current=e),console.info(`[push] cannot request permission (denied or unsupported)`);return}let a=await y();if(i)return;if(!a){i||(M.current=e),console.info(`[push] auto-prompt already decided — skip`);return}if(j(!0),console.info(`[push] showing in-app subscribe prompt`),r.OS!==`web`){console.info(`[push] requesting OS permission`);let t=await p.requestPermission();if(i)return;c(t),await b(),t?(j(!1),await F(e,!0),console.info(`[push] permission granted — device registered`)):(j(!1),console.info(`[push] permission not granted — will not auto-ask again`))}i||(M.current=e)}catch(e){console.warn(`[push] auto-subscribe on login failed`,e)}})()},1200);return()=>{i=!0,clearTimeout(a)}},[E,O,n,F,t?.id]),(0,m.useEffect)(()=>{if(!(!E||!O||!n))return p.onNotificationClick(e=>{let t=d(e.url);if(t)try{o(t)}catch{}})},[E,O,n]);let R=(0,m.useCallback)(async()=>{if(!t?.id)return{ok:!1,error:`Not signed in`};x(!0);try{await p.optOut();let e=l??await p.getPlayerId();return e&&await g(t.id,e),c(!1),{ok:!0}}catch(e){return{ok:!1,error:e instanceof Error?e.message:`Failed to disable push`}}finally{x(!1)}},[l,t?.id]),z=(0,m.useMemo)(()=>({isConfigured:n,permissionGranted:a,isBusy:v,playerId:l,subscribePromptVisible:A,dismissSubscribePrompt:P,enablePush:I,disablePush:R,syncDevice:L}),[R,P,I,v,n,a,l,A,L]);return(0,S.jsx)(C.Provider,{value:z,children:e})}function D(){let e=(0,m.useContext)(C);if(!e)throw Error(`usePushNotifications must be used within PushNotificationsProvider`);return e}export{x as i,D as n,b as r,E as t};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.