{"version":3,"file":"FrameAuthShim.aCVQWxAV.mjs","names":[],"sources":["https:/framerusercontent.com/modules/oJX79GogYhZ6dEZO8kOx/taw2G2w1IxceB0suLJ7Z/FrameAuthShim.js"],"sourcesContent":["import{jsx as _jsx}from\"react/jsx-runtime\";import{useEffect}from\"react\";import{supabase,useSession,useProfile,signOut}from\"https://framerusercontent.com/modules/eLGn3D2Jxf8tLkdsWmPa/BwRKP0Yv04Vgdpxm0RMK/SupabaseAuth.js\";// Starts the \"who's online\" tracker on every page that uses this shim.\nimport\"https://framerusercontent.com/modules/MMRkEQeDu8awQX4gu3i2/Qldwy4XVCthsjnhx5XQ4/Presence.js\";// ============================================================\n// FRAMEAUTH SHIM\n//\n// A stand-in for the old FrameAuth SDK, running on Supabase. It offers the\n// same names the library, nav and downloads files used to import from\n// FrameAuth (useUser, buttonSignOut, showIfSignedIn and so on).\n//\n// It also:\n//   - adds the Supabase login to every call to the sideways-mux-token\n//     Worker, so the Worker knows who is asking\n//   - saves and loads watch progress (progressStore, useProgressSync), so\n//     it follows members across devices and feeds future reports\n//   - sends signed-in members to the welcome step (/content/welcome) the\n//     first time they open the library or their account, until they've\n//     finished or skipped it\n//   - starts the presence tracker (Presence.tsx), so admins can see who's\n//     online, away or watching right now\n// ============================================================\nconst WORKER_HOST=\"sideways-mux-token.old-boat-bc5d.workers.dev\";const isBrowser=typeof window!==\"undefined\";// Attach the login to Worker calls made from any file using this shim.\nif(isBrowser&&!window.__sidewaysWorkerAuth){window.__sidewaysWorkerAuth=true;const original=window.fetch.bind(window);window.fetch=async(input,init)=>{try{const url=typeof input===\"string\"?input:input?.url||String(input);if(url.indexOf(WORKER_HOST)!==-1){const{data}=await supabase.auth.getSession();const token=data?.session?.access_token;if(token){const headers=new Headers(init?.headers||(typeof input!==\"string\"?input.headers:undefined));headers.set(\"Authorization\",\"Bearer \"+token);headers.delete(\"X-Viewer-Id\");return original(input,{...init||{},headers});}}}catch{}return original(input,init);};}// ============================================================\n// WELCOME STEP\n// ============================================================\nconst WELCOME_PATH=\"/content/welcome\";const WELCOME_FROM=[\"/content/library\",\"/content/account\",\"/content/profile\"];if(isBrowser&&!window.__sidewaysWelcome){window.__sidewaysWelcome=true;const path=window.location.pathname.replace(/\\/+$/,\"\");if(WELCOME_FROM.includes(path)){supabase.auth.getSession().then(async({data})=>{const uid=data?.session?.user?.id;if(!uid)return;const{data:p}=await supabase.from(\"profiles\").select(\"onboarded_at\").eq(\"id\",uid).maybeSingle();if(p&&!p.onboarded_at){window.location.replace(WELCOME_PATH+\"?next=\"+encodeURIComponent(path+window.location.search));}});}}const CACHE_PREFIX=\"sideways-progress:\";let currentUser=\"\";let sent={};let queued={};let timer=null;function cacheKey(){return CACHE_PREFIX+(currentUser||\"anon\");}function readCache(){if(!isBrowser||!currentUser)return{};try{return JSON.parse(window.localStorage.getItem(cacheKey())||\"{}\")||{};}catch{return{};}}function writeCache(map){if(!isBrowser||!currentUser)return;try{window.localStorage.setItem(cacheKey(),JSON.stringify(map));}catch{}}function mergeMax(a,b){const out={...a};for(const k of Object.keys(b))out[k]=Math.max(out[k]||0,b[k]||0);return out;}async function flush(){if(timer)clearTimeout(timer);timer=null;if(!currentUser)return;const batch=queued;queued={};if(!Object.keys(batch).length)return;try{const{error}=await supabase.rpc(\"save_my_progress\",{p:batch});if(error)throw error;sent=mergeMax(sent,batch);}catch{queued=mergeMax(batch,queued)// try again next time\n;}}if(isBrowser&&!window.__sidewaysProgress){window.__sidewaysProgress=true;supabase.auth.getSession().then(({data})=>{currentUser=data?.session?.user?.id||\"\";});supabase.auth.onAuthStateChange((_e,session)=>{const id=session?.user?.id||\"\";if(id!==currentUser){currentUser=id;sent={};queued={};}});document.addEventListener(\"visibilitychange\",()=>{if(document.visibilityState===\"hidden\")flush();});window.addEventListener(\"pagehide\",()=>flush());}export const progressStore={read(){return readCache();},write(map){if(!currentUser)return;writeCache(mergeMax(readCache(),map));for(const k of Object.keys(map)){const v=Math.round(map[k]||0);if(v>(sent[k]||0)&&v>(queued[k]||0))queued[k]=v;}if(Object.keys(queued).length&&!timer)timer=setTimeout(flush,4e3);}};// Loads saved progress from Supabase once someone is signed in, and hands\n// it to the library. Call once, right after the library's progress state.\nexport function useProgressSync(setProgress){const{ready,signedIn,id}=useSession();useEffect(()=>{if(!ready)return;if(!signedIn||!id){setProgress(()=>({}));return;}currentUser=id;let cancelled=false;const local=readCache();if(Object.keys(local).length)setProgress(prev=>mergeMax(prev,local));supabase.rpc(\"get_my_progress\").then(({data,error})=>{if(cancelled||error||!Array.isArray(data))return;const saved={};for(const row of data)saved[row.key]=row.percent;sent=mergeMax(sent,saved);const merged=mergeMax(local,saved);writeCache(merged);setProgress(prev=>mergeMax(prev,merged));// Anything this browser knew about that the server didn't: send it.\nfor(const k of Object.keys(local)){if((local[k]||0)>(saved[k]||0))queued[k]=local[k];}if(Object.keys(queued).length)flush();});return()=>{cancelled=true;};},[ready,signedIn,id]);}// ============================================================\n// FRAMEAUTH-COMPATIBLE NAMES\n// ============================================================\n// Same shape FrameAuth's useUser gave: { user } or { user: null }.\nexport function useUser(){const{user,id,email,name,signedIn}=useSession();if(!signedIn||!user)return{user:null};return{user:{id,userId:id,email,name:name||email}};}export function buttonSignOut(Component,options){return props=>/*#__PURE__*/_jsx(Component,{...props,onClick:e=>{if(e&&typeof e.preventDefault===\"function\")e.preventDefault();flush();signOut(options?.redirectTo||\"/\");}});}export function showIfSignedIn(Component){return props=>{const{ready,signedIn}=useSession();return ready&&signedIn?/*#__PURE__*/_jsx(Component,{...props}):null;};}export function showIfSignedOut(Component){return props=>{const{ready,signedIn}=useSession();return ready&&!signedIn?/*#__PURE__*/_jsx(Component,{...props}):null;};}// FrameAuth tags now map to the full_access switch in Supabase.\nexport function showIfWithTag(Component){return props=>{const{profile}=useProfile();return profile?.full_access?/*#__PURE__*/_jsx(Component,{...props}):null;};}export function showIfWithoutTag(Component){return props=>{const{profile,loading}=useProfile();return!loading&&!profile?.full_access?/*#__PURE__*/_jsx(Component,{...props}):null;};}\nexport const __FramerMetadata__ = {\"exports\":{\"showIfWithoutTag\":{\"type\":\"reactHoc\",\"name\":\"showIfWithoutTag\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"useProgressSync\":{\"type\":\"function\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"progressStore\":{\"type\":\"variable\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"showIfWithTag\":{\"type\":\"reactHoc\",\"name\":\"showIfWithTag\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"showIfSignedIn\":{\"type\":\"reactHoc\",\"name\":\"showIfSignedIn\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"buttonSignOut\":{\"type\":\"reactHoc\",\"name\":\"buttonSignOut\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"showIfSignedOut\":{\"type\":\"reactHoc\",\"name\":\"showIfSignedOut\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"useUser\":{\"type\":\"function\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}\n//# sourceMappingURL=./FrameAuthShim.map"],"mappings":"4OAuBmrB,SAAS,GAAU,CAAC,OAAO,GAAc,GAAa,OAAQ,CAAC,SAAS,GAAW,CAAC,GAAG,CAAC,GAAW,CAAC,EAAY,MAAM,CAAC,EAAE,GAAG,CAAC,OAAO,KAAK,MAAM,EAAO,aAAa,QAAQ,EAAS,CAAC,GAAG,IAAI,GAAG,CAAC,CAAE,MAAM,CAAC,MAAM,CAAC,CAAE,CAAC,CAAC,SAAS,EAAW,EAAI,CAAI,MAAC,GAAW,CAAC,GAAmB,GAAG,CAAC,EAAO,aAAa,QAAQ,EAAS,EAAE,KAAK,UAAU,CAAG,CAAC,CAAE,MAAM,CAAC,CAAC,CAAC,SAAS,EAAS,EAAE,EAAE,CAAC,IAAM,EAAI,CAAC,GAAG,CAAC,EAAE,IAAI,IAAM,KAAK,OAAO,KAAK,CAAC,EAAE,EAAI,GAAG,KAAK,IAAI,EAAI,I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