1import{j as e}from"./vendor-query-BdFmonBs.js";import{r,N as P}from"./vendor-react-WIyK4ETu.js";import{Q as A}from"./index-3j4JlLKc.js";import{u as E,a as L,s as p,ab as n,bq as T,az as M,br as Q,bs as z,bt as G,C as j,e as v,f as y,g as N,h as b,B as w,J as U,bu as $}from"./index-BFSEBbff.js";import{C as q}from"./copy-BD--T96x.js";import"./vendor-supabase-CIugC5X6.js";const B="foliaire-mobile";function D(l,o){const s=new URLSearchParams({v:"1",secret:l,pid:o});return`${B}://pair?${s.toString()}`}function W(){const{user:l,loading:o}=E(),{currentProfile:s,loading:C}=L(),[t,c]=r.useState(null),[_,h]=r.useState(!0),[m,x]=r.useState(!1),[S,u]=r.useState(!1),f=r.useCallback(async()=>{if(s!=null&&s.id){h(!0);try{const{data:a,error:i}=await p.from("profile_secrets").select("upload_secret").eq("profile_id",s.id).maybeSingle();if(i)throw i;if(a!=null&&a.upload_secret)c(a.upload_secret);else{const{data:d,error:g}=await p.rpc("generate_upload_secret",{p_profile_id:s.id});if(g)throw g;d&&d.length>0&&c(d[0].upload_secret)}}catch(a){n({title:"Failed to load mobile pairing secret: "+((a==null?void 0:a.message)||"Unknown error"),variant:"destructive"})}finally{h(!1)}}},[s==null?void 0:s.id]);r.useEffect(()=>{f()},[f]);const R=async()=>{if(s!=null&&s.id){x(!0);try{const{data:a,error:i}=await p.rpc("generate_upload_secret",{p_profile_id:s.id});if(i)throw i;a&&a.length>0&&(c(a[0].upload_secret),n({title:"New pairing secret generated. Previously paired devices will need to re-pair."}))}catch(a){n({title:"Failed to regenerate: "+((a==null?void 0:a.message)||"Unknown error"),variant:"destructive"})}finally{x(!1)}}},k=async()=>{if(t)try{await navigator.clipboard.writeText(t),u(!0),n({title:"Secret copied to clipboard"}),setTimeout(()=>u(!1),2e3)}catch{n({title:"Failed to copy",variant:"destructive"})}};if(o||C)return e.jsx("div",{className:"p-8 text-muted-foreground",children:"Loadingâ¦"});if(!l)return e.jsx(P,{to:"/auth",replace:!0});if(!s)return e.jsx("div",{className:"max-w-2xl mx-auto p-8",children:e.jsxs(T,{children:[e.jsx(M,{className:"h-4 w-4"}),e.jsx(Q,{children:"No profile selected"}),e.jsx(z,{children:"Select a profile first to pair a mobile device."})]})});const F=s.display_name||s.name||s.username||"this profile";return e.jsxs("div",{className:"max-w-2xl mx-auto p-6 space-y-6",children:[e.jsxs("header",{className:"flex items-center gap-3",children:[e.jsx(G,{className:"h-7 w-7"}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-semibold",children:"Mobile App"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Pair Folià ire for iPhone with ",e.jsx("strong",{children:F}),"."]})]})]}),e.jsxs(j,{children:[e.jsxs(v,{children:[e.jsx(y,{children:"Scan to pair"}),e.jsxs(N,{children:["Open ",e.jsx("strong",{children:"Folià ire"})," on your iPhone, tap ",e.jsx("strong",{children:"Add Profile from Folià ire"}),", and scan this QR code."]})]}),e.jsx(b,{className:"flex flex-col items-center gap-4",children:_?e.jsx("div",{className:"text-muted-foreground py-12",children:"Generating secretâ¦"}):t?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"bg-white p-4 rounded-lg border",children:e.jsx(A,{value:D(t,s.id),size:240,level:"M",includeMargin:!1})}),e.jsx("p",{className:"text-xs text-muted-foreground text-center max-w-sm",children:"The QR encodes a one-time pairing link. The secret never leaves your device until you tap scan on iPhone."})]}):e.jsx("div",{className:"text-muted-foreground py-12",children:"No secret available."})})]}),t&&e.jsxs(j,{children:[e.jsxs(v,{children:[e.jsx(y,{children:"Paste instead"}),e.jsx(N,{children:"If the QR doesn't work, copy this secret and paste it into the Folià ire iPhone app."})]}),e.jsxs(b,{className:"space-y-3",children:[e.jsxs("div",{className:"flex gap-2",children:[e.jsx("code",{className:"flex-1 bg-muted px-3 py-2 rounded text-sm font-mono break-all",children:t}),e.jsx(w,{variant:"outline",size:"icon",onClick:k,"aria-label":"Copy secret",children:S?e.jsx(U,{className:"h-4 w-4"}):e.jsx(q,{className:"h-4 w-4"})})]}),e.jsx("div",{className:"flex justify-end",children:e.jsxs(w,{variant:"ghost",size:"sm",onClick:R,disabled:m,children:[e.jsx($,{className:`h-4 w-4 mr-2 ${m?"animate-spin":""}`}),"Regenerate"]})})]})]})]})}export{W as default};
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.