@saas-support/react 0.3.0 → 0.3.1
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/react.cjs +2 -2
- package/dist/react.js +2 -2
- package/package.json +1 -1
package/dist/react.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react/jsx-runtime"),t=require("react"),L=require("./index.cjs"),ss=require("react-dom"),D=t.createContext(null);function w(){const e=t.useContext(D);if(!e)throw new Error("useSaaSContext must be used within a <SaaSProvider>");return e}function es({publishableKey:e,apiKey:r,baseUrl:a,appearance:c,children:o}){const[d]=t.useState(()=>new L.SaaSSupport({publishableKey:e,apiKey:r,baseUrl:a})),[i,u]=t.useState(null),[l,n]=t.useState(!1),[
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react/jsx-runtime"),t=require("react"),L=require("./index.cjs"),ss=require("react-dom"),D=t.createContext(null);function w(){const e=t.useContext(D);if(!e)throw new Error("useSaaSContext must be used within a <SaaSProvider>");return e}function es({publishableKey:e,apiKey:r,baseUrl:a,appearance:c,children:o}){const[d]=t.useState(()=>new L.SaaSSupport({publishableKey:e,apiKey:r,baseUrl:a})),[i,u]=t.useState(null),[l,n]=t.useState(!1),[m,p]=t.useState(null);return t.useEffect(()=>{let x=!1;d.load().then(async()=>{if(x)return;const h=await d.auth.getUser(),g=await d.auth.getSettings();u(h),p(g),n(!0)});const y=d.auth.onAuthStateChange(h=>{x||u(h)});return()=>{x=!0,y(),d.destroy()}},[d]),s.jsx(D.Provider,{value:{client:d,user:i,isLoaded:l,appearance:c,settings:m},children:o})}const as={colorPrimary:"#6366f1",colorPrimaryHover:"#4f46e5",colorBackground:"#ffffff",colorText:"#1a1a2e",colorTextSecondary:"#6b7280",colorInputBackground:"#f9fafb",colorInputBorder:"#d1d5db",colorError:"#ef4444",colorSuccess:"#22c55e",colorWarning:"#f59e0b",fontFamily:'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',borderRadius:"8px"},rs={colorPrimary:"#818cf8",colorPrimaryHover:"#6366f1",colorBackground:"#1e1e2e",colorText:"#e2e8f0",colorTextSecondary:"#94a3b8",colorInputBackground:"#2a2a3e",colorInputBorder:"#3f3f5e",colorError:"#f87171",colorSuccess:"#4ade80",colorWarning:"#fbbf24",fontFamily:'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',borderRadius:"8px"};function z(e){const r=(e==null?void 0:e.baseTheme)==="dark"?rs:as,a=e==null?void 0:e.variables;return{colorPrimary:(a==null?void 0:a.colorPrimary)??r.colorPrimary,colorPrimaryHover:a!=null&&a.colorPrimary?ts(a.colorPrimary,10):r.colorPrimaryHover,colorBackground:(a==null?void 0:a.colorBackground)??r.colorBackground,colorText:(a==null?void 0:a.colorText)??r.colorText,colorTextSecondary:r.colorTextSecondary,colorInputBackground:(a==null?void 0:a.colorInputBackground)??r.colorInputBackground,colorInputBorder:(a==null?void 0:a.colorInputBorder)??r.colorInputBorder,colorError:(a==null?void 0:a.colorError)??r.colorError,colorSuccess:(a==null?void 0:a.colorSuccess)??r.colorSuccess,colorWarning:(a==null?void 0:a.colorWarning)??r.colorWarning,fontFamily:(a==null?void 0:a.fontFamily)??r.fontFamily,borderRadius:(a==null?void 0:a.borderRadius)??r.borderRadius}}function ts(e,r){const a=parseInt(e.replace("#",""),16),c=Math.max(0,(a>>16)-Math.round(2.55*r)),o=Math.max(0,(a>>8&255)-Math.round(2.55*r)),d=Math.max(0,(a&255)-Math.round(2.55*r));return`#${(c<<16|o<<8|d).toString(16).padStart(6,"0")}`}function q(e){return`
|
|
2
2
|
:host {
|
|
3
3
|
all: initial;
|
|
4
4
|
font-family: ${e.fontFamily};
|
|
@@ -725,4 +725,4 @@
|
|
|
725
725
|
font-size: 13px;
|
|
726
726
|
padding: 6px 12px;
|
|
727
727
|
}
|
|
728
|
-
`}function P({children:e,appearance:r}){const a=t.useRef(null),[c,o]=t.useState(null);t.useEffect(()=>{var x;if(!a.current||a.current.shadowRoot){o(((x=a.current)==null?void 0:x.shadowRoot)??null);return}const i=a.current.attachShadow({mode:"open"}),u=z(r),l=document.createElement("style");l.textContent=q(u),i.appendChild(l);const n=document.createElement("div");i.appendChild(n),o(i)},[]),t.useEffect(()=>{if(!c)return;const i=c.querySelector("style");if(i){const u=z(r);i.textContent=q(u)}},[r,c]);const d=(c==null?void 0:c.querySelector("div"))??null;return s.jsx("div",{ref:a,style:{display:"contents"},children:d&&ss.createPortal(e,d)})}function M(){const{client:e,user:r,isLoaded:a}=w();return{isLoaded:a,isSignedIn:!!r,user:r,signOut:t.useCallback(()=>e.auth.signOut(),[e]),getToken:t.useCallback(()=>e.auth.getToken(),[e])}}function ns(){const{user:e,isLoaded:r}=w();return{user:e,isLoaded:r}}function F(){const{client:e}=w(),[r,a]=t.useState(!1),[c,o]=t.useState(null),d=t.useCallback(async(l,n)=>{a(!0),o(null);try{return await e.auth.signIn(l,n)}catch(x){return o(x instanceof Error?x.message:"Sign in failed"),null}finally{a(!1)}},[e]),i=t.useCallback(async l=>{a(!0),o(null);try{return await e.auth.signInWithOAuth(l)}catch(n){return o(n instanceof Error?n.message:"OAuth sign in failed"),null}finally{a(!1)}},[e]),u=t.useCallback(async(l,n)=>{a(!0),o(null);try{return await e.auth.submitMfaCode(l,n)}catch(x){return o(x instanceof Error?x.message:"MFA verification failed"),null}finally{a(!1)}},[e]);return{signIn:d,signInWithOAuth:i,submitMfaCode:u,isLoading:r,error:c,setError:o}}function R(){const{client:e}=w(),[r,a]=t.useState(!1),[c,o]=t.useState(null);return{signUp:t.useCallback(async(i,u)=>{a(!0),o(null);try{return await e.auth.signUp(i,u)}catch(l){return o(l instanceof Error?l.message:"Sign up failed"),null}finally{a(!1)}},[e]),isLoading:r,error:c,setError:o}}function O(){const{client:e}=w(),[r,a]=t.useState([]),[c,o]=t.useState(null),[d,i]=t.useState([]),[u,l]=t.useState(!1),[n,x]=t.useState(null),p=t.useCallback(async()=>{l(!0),x(null);try{const h=await e.auth.listOrgs();a(h)}catch(h){x(h instanceof Error?h.message:"Failed to load organizations")}finally{l(!1)}},[e]);t.useEffect(()=>{p()},[p]);const m=t.useCallback(async h=>{try{const g=await e.auth.getOrg(h);o(g);const f=await e.auth.listMembers(h);i(f)}catch(g){x(g instanceof Error?g.message:"Failed to load organization")}},[e]),y=t.useCallback(async(h,g)=>{try{const f=await e.auth.createOrg(h,g);return a(b=>[...b,f]),f}catch(f){return x(f instanceof Error?f.message:"Failed to create organization"),null}},[e]);return{orgs:r,selectedOrg:c,members:d,isLoading:u,error:n,refresh:p,selectOrg:m,createOrg:y}}function A(){const{client:e,user:r}=w(),[a,c]=t.useState(!1),[o,d]=t.useState(null),[i,u]=t.useState(null),l=t.useCallback(async x=>{c(!0),d(null),u(null);try{const p=await e.auth.updateProfile(x);return u("Profile updated"),p}catch(p){return d(p instanceof Error?p.message:"Failed to update profile"),null}finally{c(!1)}},[e]),n=t.useCallback(async(x,p)=>{c(!0),d(null),u(null);try{return await e.auth.changePassword(x,p),u("Password changed successfully"),!0}catch(m){return d(m instanceof Error?m.message:"Failed to change password"),!1}finally{c(!1)}},[e]);return{user:r,updateProfile:l,changePassword:n,isLoading:a,error:o,success:i,setError:d,setSuccess:u}}const W='<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92a5.06 5.06 0 0 1-2.2 3.32v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.1z" fill="#4285F4"/><path d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z" fill="#34A853"/><path d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z" fill="#FBBC05"/><path d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z" fill="#EA4335"/></svg>',H='<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M12 0C5.37 0 0 5.37 0 12c0 5.31 3.435 9.795 8.205 11.385.6.105.825-.255.825-.57 0-.285-.015-1.23-.015-2.235-3.015.555-3.795-.735-4.035-1.41-.135-.345-.72-1.41-1.23-1.695-.42-.225-1.02-.78-.015-.795.945-.015 1.62.87 1.845 1.23 1.08 1.815 2.805 1.305 3.495.99.105-.78.42-1.305.765-1.605-2.67-.3-5.46-1.335-5.46-5.925 0-1.305.465-2.385 1.23-3.225-.12-.3-.54-1.53.12-3.18 0 0 1.005-.315 3.3 1.23.96-.27 1.98-.405 3-.405s2.04.135 3 .405c2.295-1.56 3.3-1.23 3.3-1.23.66 1.65.24 2.88.12 3.18.765.84 1.23 1.905 1.23 3.225 0 4.605-2.805 5.625-5.475 5.925.435.375.81 1.095.81 2.22 0 1.605-.015 2.895-.015 3.3 0 .315.225.69.825.57A12.02 12.02 0 0 0 24 12c0-6.63-5.37-12-12-12z" fill="currentColor"/></svg>';function os({appearance:e,signUpUrl:r,onSignUp:a}){const{appearance:c,settings:o}=w(),{signIn:d,signInWithOAuth:i,submitMfaCode:u,isLoading:l,error:n,setError:x}=F(),p=e??c,[m,y]=t.useState(""),[h,g]=t.useState(""),[f,b]=t.useState(!1),[j,v]=t.useState(""),[N,k]=t.useState(""),T=t.useCallback(async I=>{if(I.preventDefault(),f){await u(j,N);return}const C=await d(m,h);C&&L.isMfaRequired(C)&&(v(C.mfaToken),b(!0),x(null))},[m,h,f,j,N,d,u,x]),$=t.useCallback(async I=>{await i(I)},[i]),S=(o==null?void 0:o.googleEnabled)||(o==null?void 0:o.githubEnabled);return s.jsx(P,{appearance:p,children:s.jsxs("div",{className:"ss-card",children:[s.jsx("h2",{className:"ss-title",children:"Sign in"}),!f&&s.jsxs(s.Fragment,{children:[(o==null?void 0:o.googleEnabled)&&s.jsxs("button",{type:"button",className:"ss-btn-social",onClick:()=>$("google"),disabled:l,children:[s.jsx("span",{dangerouslySetInnerHTML:{__html:W}}),"Continue with Google"]}),(o==null?void 0:o.githubEnabled)&&s.jsxs("button",{type:"button",className:"ss-btn-social",onClick:()=>$("github"),disabled:l,children:[s.jsx("span",{dangerouslySetInnerHTML:{__html:H}}),"Continue with GitHub"]}),S&&s.jsx("div",{className:"ss-divider",children:"or"})]}),n&&s.jsx("div",{className:"ss-global-error",children:n}),s.jsxs("form",{onSubmit:T,children:[f?s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-mfa-code",children:"Authentication code"}),s.jsx("input",{id:"ss-mfa-code",className:"ss-input",type:"text",inputMode:"numeric",autoComplete:"one-time-code",placeholder:"Enter 6-digit code",value:N,onChange:I=>k(I.target.value),autoFocus:!0})]}):s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-email",children:"Email"}),s.jsx("input",{id:"ss-email",className:"ss-input",type:"email",autoComplete:"email",placeholder:"you@example.com",value:m,onChange:I=>y(I.target.value),required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-password",children:"Password"}),s.jsx("input",{id:"ss-password",className:"ss-input",type:"password",autoComplete:"current-password",placeholder:"Enter your password",value:h,onChange:I=>g(I.target.value),required:!0})]})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:l,children:[l&&s.jsx("span",{className:"ss-spinner"}),f?"Verify":"Continue"]})]}),f&&s.jsx("div",{className:"ss-footer",children:s.jsx("span",{className:"ss-link",onClick:()=>{b(!1),k(""),x(null)},children:"Back to sign in"})}),!f&&s.jsxs("div",{className:"ss-footer",children:["Don't have an account?"," ",a?s.jsx("span",{className:"ss-link",onClick:a,children:"Sign up"}):r?s.jsx("a",{className:"ss-link",href:r,children:"Sign up"}):s.jsx("span",{className:"ss-link",children:"Sign up"})]})]})})}function ls({appearance:e,signInUrl:r,onSignIn:a}){const{appearance:c,settings:o}=w(),{signUp:d,isLoading:i,error:u,setError:l}=R(),{signInWithOAuth:n}=F(),x=e??c,[p,m]=t.useState(""),[y,h]=t.useState(""),[g,f]=t.useState(""),[b,j]=t.useState(null),v=t.useCallback(async $=>{if($.preventDefault(),j(null),y!==g){j("Passwords do not match");return}const S=(o==null?void 0:o.passwordMinLength)??8;if(y.length<S){j(`Password must be at least ${S} characters`);return}await d(p,y)},[p,y,g,o,d]),N=t.useCallback(async $=>{await n($)},[n]),k=(o==null?void 0:o.googleEnabled)||(o==null?void 0:o.githubEnabled),T=b||u;return s.jsx(P,{appearance:x,children:s.jsxs("div",{className:"ss-card",children:[s.jsx("h2",{className:"ss-title",children:"Create account"}),(o==null?void 0:o.googleEnabled)&&s.jsxs("button",{type:"button",className:"ss-btn-social",onClick:()=>N("google"),disabled:i,children:[s.jsx("span",{dangerouslySetInnerHTML:{__html:W}}),"Continue with Google"]}),(o==null?void 0:o.githubEnabled)&&s.jsxs("button",{type:"button",className:"ss-btn-social",onClick:()=>N("github"),disabled:i,children:[s.jsx("span",{dangerouslySetInnerHTML:{__html:H}}),"Continue with GitHub"]}),k&&s.jsx("div",{className:"ss-divider",children:"or"}),T&&s.jsx("div",{className:"ss-global-error",children:T}),s.jsxs("form",{onSubmit:v,children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-signup-email",children:"Email"}),s.jsx("input",{id:"ss-signup-email",className:"ss-input",type:"email",autoComplete:"email",placeholder:"you@example.com",value:p,onChange:$=>m($.target.value),required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-signup-password",children:"Password"}),s.jsx("input",{id:"ss-signup-password",className:"ss-input",type:"password",autoComplete:"new-password",placeholder:"Create a password",value:y,onChange:$=>{h($.target.value),j(null)},required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-signup-confirm",children:"Confirm password"}),s.jsx("input",{id:"ss-signup-confirm",className:"ss-input",type:"password",autoComplete:"new-password",placeholder:"Confirm your password",value:g,onChange:$=>{f($.target.value),j(null)},required:!0})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:i,children:[i&&s.jsx("span",{className:"ss-spinner"}),"Create account"]})]}),s.jsxs("div",{className:"ss-footer",children:["Already have an account?"," ",a?s.jsx("span",{className:"ss-link",onClick:a,children:"Sign in"}):r?s.jsx("a",{className:"ss-link",href:r,children:"Sign in"}):s.jsx("span",{className:"ss-link",children:"Sign in"})]})]})})}function is({appearance:e}){const{appearance:r}=w(),{user:a,signOut:c}=M(),o=e??r,[d,i]=t.useState(!1),[u,l]=t.useState(!1),n=t.useRef(null),x=t.useCallback(m=>{n.current&&!n.current.contains(m.target)&&i(!1)},[]);if(t.useEffect(()=>{if(d){const m=setTimeout(()=>{document.addEventListener("click",x)},0);return()=>{clearTimeout(m),document.removeEventListener("click",x)}}},[d,x]),!a)return null;const p=(a.name||a.email).charAt(0).toUpperCase();return s.jsx(P,{appearance:o,children:s.jsxs("div",{className:"ss-user-btn",ref:n,children:[s.jsx("button",{type:"button",className:"ss-avatar",onClick:()=>i(!d),"aria-label":"User menu",children:a.avatarUrl?s.jsx("img",{src:a.avatarUrl,alt:"",style:{width:"100%",height:"100%",borderRadius:"50%",objectFit:"cover"}}):p}),d&&s.jsxs("div",{className:"ss-dropdown",children:[s.jsxs("div",{className:"ss-dropdown-header",children:[a.name&&s.jsx("div",{style:{fontWeight:600,fontSize:"14px",color:"inherit"},children:a.name}),s.jsx("div",{className:"ss-dropdown-email",children:a.email})]}),s.jsx("button",{type:"button",className:"ss-dropdown-item",onClick:()=>{i(!1),l(!0)},children:"Profile"}),s.jsx("button",{type:"button",className:"ss-dropdown-item ss-dropdown-item-danger",onClick:async()=>{i(!1),await c()},children:"Sign out"})]}),u&&s.jsx(cs,{onClose:()=>l(!1)})]})})}function cs({onClose:e}){const{user:r,updateProfile:a,changePassword:c,isLoading:o,error:d,success:i,setError:u,setSuccess:l}=A(),[n,x]=t.useState((r==null?void 0:r.name)??""),[p,m]=t.useState((r==null?void 0:r.avatarUrl)??""),[y,h]=t.useState(""),[g,f]=t.useState(""),[b,j]=t.useState(""),[v,N]=t.useState(null),k=t.useCallback(async S=>{S.preventDefault(),u(null),l(null),await a({name:n,avatarUrl:p})},[n,p,a,u,l]),T=t.useCallback(async S=>{if(S.preventDefault(),N(null),u(null),l(null),g!==b){N("Passwords do not match");return}if(g.length<8){N("Password must be at least 8 characters");return}await c(y,g)&&(h(""),f(""),j(""))},[y,g,b,c,u,l]),$=(r==null?void 0:r.provider)==="email";return s.jsx("div",{className:"ss-modal-overlay",onClick:S=>{S.target===S.currentTarget&&e()},children:s.jsxs("div",{className:"ss-modal",children:[s.jsxs("div",{className:"ss-modal-header",children:[s.jsx("span",{className:"ss-modal-title",children:"Profile"}),s.jsx("button",{type:"button",className:"ss-modal-close",onClick:e,children:"✕"})]}),d&&s.jsx("div",{className:"ss-global-error",children:d}),i&&s.jsx("div",{className:"ss-success-msg",children:i}),s.jsx("div",{className:"ss-avatar-preview",children:p?s.jsx("img",{src:p,alt:""}):(n||(r==null?void 0:r.email)||"?").charAt(0).toUpperCase()}),s.jsxs("form",{onSubmit:k,children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Name"}),s.jsx("input",{className:"ss-input",type:"text",placeholder:"Your name",value:n,onChange:S=>x(S.target.value)})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Avatar URL"}),s.jsx("input",{className:"ss-input",type:"url",placeholder:"https://example.com/avatar.jpg",value:p,onChange:S=>m(S.target.value)})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Email"}),s.jsx("input",{className:"ss-input ss-input-readonly",type:"email",value:(r==null?void 0:r.email)??"",disabled:!0,readOnly:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Provider"}),s.jsx("input",{className:"ss-input ss-input-readonly",type:"text",value:(r==null?void 0:r.provider)??"",disabled:!0,readOnly:!0})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:o,children:[o&&s.jsx("span",{className:"ss-spinner"}),"Save changes"]})]}),$&&s.jsxs("div",{className:"ss-modal-section",children:[s.jsx("div",{className:"ss-modal-section-title",children:"Change password"}),v&&s.jsx("div",{className:"ss-global-error",children:v}),s.jsxs("form",{onSubmit:T,children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Current password"}),s.jsx("input",{className:"ss-input",type:"password",value:y,onChange:S=>h(S.target.value),required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"New password"}),s.jsx("input",{className:"ss-input",type:"password",value:g,onChange:S=>f(S.target.value),required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Confirm new password"}),s.jsx("input",{className:"ss-input",type:"password",value:b,onChange:S=>j(S.target.value),required:!0})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:o,children:[o&&s.jsx("span",{className:"ss-spinner"}),"Update password"]})]})]})]})})}function ds({appearance:e}){const{appearance:r}=w(),{user:a,signOut:c}=M(),{updateProfile:o,changePassword:d,isLoading:i,error:u,success:l,setError:n,setSuccess:x}=A(),p=e??r,[m,y]=t.useState((a==null?void 0:a.name)??""),[h,g]=t.useState((a==null?void 0:a.avatarUrl)??""),[f,b]=t.useState(""),[j,v]=t.useState(""),[N,k]=t.useState(""),[T,$]=t.useState(null),S=t.useCallback(async E=>{E.preventDefault(),n(null),x(null),await o({name:m,avatarUrl:h})},[m,h,o,n,x]),I=t.useCallback(async E=>{if(E.preventDefault(),$(null),n(null),x(null),j!==N){$("Passwords do not match");return}if(j.length<8){$("Password must be at least 8 characters");return}await d(f,j)&&(b(""),v(""),k(""))},[f,j,N,d,n,x]);if(!a)return null;const C=a.provider==="email";return s.jsx(P,{appearance:p,children:s.jsxs("div",{className:"ss-card ss-card-wide",children:[s.jsx("h2",{className:"ss-title",children:"Profile"}),u&&s.jsx("div",{className:"ss-global-error",children:u}),l&&s.jsx("div",{className:"ss-success-msg",children:l}),s.jsx("div",{className:"ss-avatar-preview",style:{margin:"0 auto 16px"},children:h?s.jsx("img",{src:h,alt:""}):(m||a.email).charAt(0).toUpperCase()}),s.jsxs("form",{onSubmit:S,children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Name"}),s.jsx("input",{className:"ss-input",type:"text",placeholder:"Your name",value:m,onChange:E=>y(E.target.value)})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Avatar URL"}),s.jsx("input",{className:"ss-input",type:"url",placeholder:"https://example.com/avatar.jpg",value:h,onChange:E=>g(E.target.value)})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Email"}),s.jsx("input",{className:"ss-input ss-input-readonly",type:"email",value:a.email,disabled:!0,readOnly:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Provider"}),s.jsx("input",{className:"ss-input ss-input-readonly",type:"text",value:a.provider,disabled:!0,readOnly:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Email verified"}),s.jsx("input",{className:"ss-input ss-input-readonly",type:"text",value:a.emailVerified?"Yes":"No",disabled:!0,readOnly:!0})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:i,children:[i&&s.jsx("span",{className:"ss-spinner"}),"Save changes"]})]}),C&&s.jsxs("div",{className:"ss-modal-section",children:[s.jsx("div",{className:"ss-modal-section-title",children:"Change password"}),T&&s.jsx("div",{className:"ss-global-error",children:T}),s.jsxs("form",{onSubmit:I,children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Current password"}),s.jsx("input",{className:"ss-input",type:"password",value:f,onChange:E=>b(E.target.value),required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"New password"}),s.jsx("input",{className:"ss-input",type:"password",value:j,onChange:E=>v(E.target.value),required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Confirm new password"}),s.jsx("input",{className:"ss-input",type:"password",value:N,onChange:E=>k(E.target.value),required:!0})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:i,children:[i&&s.jsx("span",{className:"ss-spinner"}),"Update password"]})]})]}),s.jsx("div",{style:{marginTop:"24px"},children:s.jsx("button",{type:"button",className:"ss-btn ss-btn-danger",onClick:()=>c(),children:"Sign out"})})]})})}function us({appearance:e,onOrgChange:r}){const{appearance:a}=w(),{orgs:c,selectedOrg:o,selectOrg:d,createOrg:i,isLoading:u}=O(),l=e??a,[n,x]=t.useState(!1),[p,m]=t.useState(!1),[y,h]=t.useState(""),[g,f]=t.useState(""),[b,j]=t.useState(null),[v,N]=t.useState(!1),k=t.useRef(null),T=t.useCallback(C=>{k.current&&!k.current.contains(C.target)&&(x(!1),m(!1))},[]);t.useEffect(()=>{if(n){const C=setTimeout(()=>{document.addEventListener("click",T)},0);return()=>{clearTimeout(C),document.removeEventListener("click",T)}}},[n,T]);const $=t.useCallback(C=>{h(C),f(C.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,""))},[]),S=t.useCallback(async C=>{C.preventDefault(),j(null),N(!0);try{const E=await i(y,g);E&&(await d(E.id),r==null||r(E),m(!1),h(""),f(""),x(!1))}catch(E){j(E instanceof Error?E.message:"Failed to create organization")}finally{N(!1)}},[y,g,i,d,r]);if(u)return null;const I=(o==null?void 0:o.name)??(c.length===0?"No organization":"Select organization");return s.jsx(P,{appearance:l,children:s.jsxs("div",{className:"ss-user-btn",ref:k,children:[s.jsxs("button",{type:"button",className:"ss-btn ss-btn-org-switcher",onClick:()=>x(!n),children:[I,s.jsx("span",{className:"ss-chevron",children:n?"▲":"▼"})]}),n&&s.jsxs("div",{className:"ss-dropdown ss-dropdown-org",children:[c.map(C=>s.jsx("button",{type:"button",className:`ss-dropdown-item ${(o==null?void 0:o.id)===C.id?"ss-dropdown-item-active":""}`,onClick:async()=>{x(!1),m(!1),await d(C.id),r==null||r(C)},children:C.name},C.id)),s.jsx("div",{className:"ss-dropdown-divider"}),p?s.jsxs("div",{className:"ss-inline-form",children:[b&&s.jsx("div",{className:"ss-global-error",style:{marginBottom:"8px",fontSize:"12px"},children:b}),s.jsxs("form",{onSubmit:S,children:[s.jsx("div",{className:"ss-field",children:s.jsx("input",{className:"ss-input",type:"text",placeholder:"Organization name",value:y,onChange:C=>$(C.target.value),required:!0,autoFocus:!0})}),s.jsx("div",{className:"ss-field",children:s.jsx("input",{className:"ss-input",type:"text",placeholder:"org-slug",value:g,onChange:C=>f(C.target.value),required:!0})}),s.jsxs("div",{className:"ss-btn-group",style:{marginTop:"8px"},children:[s.jsx("button",{type:"button",className:"ss-btn ss-btn-sm ss-btn-current",onClick:()=>{m(!1),j(null)},children:"Cancel"}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-sm ss-btn-primary",disabled:v,children:[v&&s.jsx("span",{className:"ss-spinner"}),"Create"]})]})]})]}):s.jsx("button",{type:"button",className:"ss-dropdown-item",onClick:()=>m(!0),style:{fontWeight:500},children:"+ Create organization"})]})]})})}function ps(e,r="USD"){return new Intl.NumberFormat("en-US",{style:"currency",currency:r,minimumFractionDigits:e%100===0?0:2}).format(e/100)}function xs({plans:e,currentPlanId:r,onSelectPlan:a,interval:c,appearance:o}){const{appearance:d}=w(),i=o??d,u=c?e.filter(l=>l.interval===c||l.isFree):e;return s.jsx(P,{appearance:i,children:s.jsx("div",{className:"ss-pricing-grid",children:u.map(l=>{const n=l.id===r;return s.jsxs("div",{className:`ss-pricing-card ${n?"ss-pricing-card-current":""}`,children:[s.jsxs("div",{className:"ss-pricing-header",children:[s.jsx("h3",{className:"ss-pricing-name",children:l.name}),l.trialDays>0&&s.jsxs("span",{className:"ss-badge ss-badge-trialing",children:[l.trialDays,"-day trial"]}),l.isFree&&s.jsx("span",{className:"ss-badge ss-badge-active",children:"Free"})]}),s.jsx("div",{className:"ss-pricing-price",children:l.isFree?s.jsx("span",{className:"ss-pricing-amount",children:"Free"}):s.jsxs(s.Fragment,{children:[s.jsx("span",{className:"ss-pricing-amount",children:ps(l.amountCents,l.currency)}),s.jsxs("span",{className:"ss-pricing-interval",children:["/",l.interval]})]})}),l.description&&s.jsx("p",{className:"ss-pricing-desc",children:l.description}),l.features.length>0&&s.jsx("ul",{className:"ss-pricing-features",children:l.features.map((x,p)=>s.jsxs("li",{className:"ss-pricing-feature",children:[s.jsx("span",{className:"ss-check",children:"✓"})," ",x]},p))}),s.jsx("button",{type:"button",className:`ss-btn ${n?"ss-btn-current":"ss-btn-primary"}`,disabled:n,onClick:()=>a(l.id),children:n?"Current plan":"Select plan"})]},l.id)})})})}function ms(){const{client:e}=w();return{billing:e.billing}}function B(e){const{client:r}=w();return t.useMemo(()=>{if(e){const a=new L.Transport("https://api.saas-support.com/v1",{type:"portalToken",token:e});return new L.BillingClient(a)}return r.billing},[r,e])}function _(e,r){const a=B(r),[c,o]=t.useState(null),[d,i]=t.useState(!0),[u,l]=t.useState(null),n=t.useCallback(async()=>{i(!0),l(null);try{const x=await a.getCustomer(e);o(x)}catch(x){l(x instanceof Error?x.message:"Failed to load subscription")}finally{i(!1)}},[a,e]);return t.useEffect(()=>{n()},[n]),{customer:c,isLoading:d,error:u,refresh:n}}function Q(e,r){const a=B(r),[c,o]=t.useState([]),[d,i]=t.useState(!0),[u,l]=t.useState(null),n=t.useCallback(async()=>{i(!0),l(null);try{const x=await a.getInvoices(e);o(x)}catch(x){l(x instanceof Error?x.message:"Failed to load invoices")}finally{i(!1)}},[a,e]);return t.useEffect(()=>{n()},[n]),{invoices:c,isLoading:d,error:u,refresh:n}}function G(e,r){const a=B(r),[c,o]=t.useState([]),[d,i]=t.useState(!0),[u,l]=t.useState(null),n=t.useCallback(async()=>{i(!0),l(null);try{const x=await a.getCurrentUsage(e);o(x)}catch(x){l(x instanceof Error?x.message:"Failed to load usage")}finally{i(!1)}},[a,e]);return t.useEffect(()=>{n()},[n]),{usage:c,isLoading:d,error:u,refresh:n}}const gs={active:"ss-badge-active"};function V({customerId:e,portalToken:r,onChangePlan:a,onCancel:c,appearance:o}){const{appearance:d}=w(),{customer:i,isLoading:u,error:l}=_(e,r),n=o??d;return s.jsx(P,{appearance:n,children:s.jsxs("div",{className:"ss-card",children:[s.jsx("h2",{className:"ss-title",children:"Subscription"}),u&&s.jsxs("div",{className:"ss-loading",children:[s.jsx("span",{className:"ss-spinner"})," Loading..."]}),l&&s.jsx("div",{className:"ss-global-error",children:l}),i&&!u&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Customer"}),s.jsx("input",{className:"ss-input",value:i.name||i.email,disabled:!0,readOnly:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Status"}),s.jsx("div",{children:s.jsx("span",{className:`ss-badge ${gs.active}`,children:"Active"})})]}),s.jsxs("div",{className:"ss-btn-group",children:[a&&s.jsx("button",{type:"button",className:"ss-btn ss-btn-primary",onClick:a,children:"Change plan"}),c&&s.jsx("button",{type:"button",className:"ss-btn ss-btn-danger",onClick:c,children:"Cancel subscription"})]})]})]})})}const bs={paid:"ss-badge-active",open:"ss-badge-trialing",draft:"ss-badge-paused",void:"ss-badge-canceled",uncollectible:"ss-badge-past-due"};function hs(e){return new Date(e).toLocaleDateString("en-US",{year:"numeric",month:"short",day:"numeric"})}function fs(e){return new Intl.NumberFormat("en-US",{style:"currency",currency:"USD",minimumFractionDigits:2}).format(e/100)}function J({customerId:e,portalToken:r,appearance:a}){const{appearance:c}=w(),{invoices:o,isLoading:d,error:i}=Q(e,r),u=a??c,l=[...o].sort((n,x)=>new Date(x.createdAt).getTime()-new Date(n.createdAt).getTime());return s.jsx(P,{appearance:u,children:s.jsxs("div",{className:"ss-card ss-card-wide",children:[s.jsx("h2",{className:"ss-title",children:"Invoices"}),d&&s.jsxs("div",{className:"ss-loading",children:[s.jsx("span",{className:"ss-spinner"})," Loading..."]}),i&&s.jsx("div",{className:"ss-global-error",children:i}),!d&&l.length===0&&s.jsx("p",{className:"ss-empty",children:"No invoices yet."}),!d&&l.length>0&&s.jsxs("table",{className:"ss-table",children:[s.jsx("thead",{children:s.jsxs("tr",{children:[s.jsx("th",{className:"ss-th",children:"Date"}),s.jsx("th",{className:"ss-th",children:"Amount"}),s.jsx("th",{className:"ss-th",children:"Status"}),s.jsx("th",{className:"ss-th",children:"PDF"})]})}),s.jsx("tbody",{children:l.map(n=>s.jsxs("tr",{className:"ss-tr",children:[s.jsx("td",{className:"ss-td",children:hs(n.createdAt)}),s.jsx("td",{className:"ss-td",children:fs(n.amountCents)}),s.jsx("td",{className:"ss-td",children:s.jsx("span",{className:`ss-badge ${bs[n.status]||""}`,children:n.status})}),s.jsx("td",{className:"ss-td",children:n.pdfUrl?s.jsx("a",{className:"ss-link",href:n.pdfUrl,target:"_blank",rel:"noopener noreferrer",children:"Download"}):"—"})]},n.id))})]})]})})}function Y({customerId:e,limits:r,portalToken:a,appearance:c}){const{appearance:o}=w(),{usage:d,isLoading:i,error:u}=G(e,a),l=c??o;return s.jsx(P,{appearance:l,children:s.jsxs("div",{className:"ss-card",children:[s.jsx("h2",{className:"ss-title",children:"Usage"}),i&&s.jsxs("div",{className:"ss-loading",children:[s.jsx("span",{className:"ss-spinner"})," Loading..."]}),u&&s.jsx("div",{className:"ss-global-error",children:u}),!i&&d.length===0&&s.jsx("p",{className:"ss-empty",children:"No usage data."}),!i&&d.map(n=>{const x=r==null?void 0:r[n.metric],p=x?Math.min(100,n.total/x*100):null;return s.jsxs("div",{className:"ss-usage-item",children:[s.jsxs("div",{className:"ss-usage-header",children:[s.jsx("span",{className:"ss-usage-metric",children:n.metric}),s.jsxs("span",{className:"ss-usage-value",children:[n.total.toLocaleString(),x?` / ${x.toLocaleString()}`:""]})]}),p!==null&&s.jsx("div",{className:"ss-progress-bar",children:s.jsx("div",{className:`ss-progress-fill ${p>90?"ss-progress-danger":""}`,style:{width:`${p}%`}})})]},n.metric)})]})})}function ys({customerId:e,portalToken:r,limits:a,onChangePlan:c,onCancel:o,appearance:d}){const{appearance:i}=w(),u=d??i,[l,n]=t.useState("subscription"),x=[{id:"subscription",label:"Subscription"},{id:"invoices",label:"Invoices"},{id:"usage",label:"Usage"}];return s.jsx(P,{appearance:u,children:s.jsxs("div",{className:"ss-card ss-card-wide",children:[s.jsx("h2",{className:"ss-title",children:"Billing"}),s.jsx("div",{className:"ss-tab-group",children:x.map(p=>s.jsx("button",{type:"button",className:`ss-tab ${l===p.id?"ss-tab-active":""}`,onClick:()=>n(p.id),children:p.label},p.id))}),s.jsxs("div",{className:"ss-tab-content",children:[l==="subscription"&&s.jsx(V,{customerId:e,portalToken:r,onChangePlan:c,onCancel:o}),l==="invoices"&&s.jsx(J,{customerId:e,portalToken:r}),l==="usage"&&s.jsx(Y,{customerId:e,portalToken:r,limits:a})]})]})})}function js({customerId:e,portalToken:r,onApplied:a,appearance:c}){const{appearance:o}=w(),d=B(r),i=c??o,[u,l]=t.useState(""),[n,x]=t.useState(!1),[p,m]=t.useState(null),[y,h]=t.useState(null),g=t.useCallback(async f=>{if(f.preventDefault(),!!u.trim()){x(!0),m(null),h(null);try{const b=await d.applyCoupon(e,u.trim());h(`Coupon applied! ${b.discountType==="percent"?`${b.amount}% off`:`$${(b.amount/100).toFixed(2)} off`}`),l(""),a==null||a(b)}catch(b){m(b instanceof Error?b.message:"Invalid coupon code")}finally{x(!1)}}},[d,e,u,a]);return s.jsx(P,{appearance:i,children:s.jsxs("div",{className:"ss-card",children:[s.jsx("h2",{className:"ss-title",children:"Apply coupon"}),p&&s.jsx("div",{className:"ss-global-error",children:p}),y&&s.jsx("div",{className:"ss-success-msg",children:y}),s.jsxs("form",{onSubmit:g,children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-coupon-code",children:"Coupon code"}),s.jsx("input",{id:"ss-coupon-code",className:"ss-input",type:"text",placeholder:"Enter coupon code",value:u,onChange:f=>l(f.target.value),required:!0})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:n||!u.trim(),children:[n&&s.jsx("span",{className:"ss-spinner"}),"Apply"]})]})]})})}function vs(){const{client:e}=w();return{report:e.report}}function U(){const{client:e}=w(),[r,a]=t.useState(null),[c,o]=t.useState(!1),[d,i]=t.useState(null),u=t.useCallback(async l=>{o(!0),i(null);try{const n=await e.report.executeQuery(l);return a(n),n}catch(n){return i(n instanceof Error?n.message:"Query failed"),null}finally{o(!1)}},[e]);return{result:r,execute:u,isLoading:c,error:d}}function K(e){const{client:r}=w(),[a,c]=t.useState(null),[o,d]=t.useState(!0),[i,u]=t.useState(null),l=t.useCallback(async()=>{d(!0),u(null);try{const n=await r.report.listQueries(e);c(n)}catch(n){u(n instanceof Error?n.message:"Failed to load queries")}finally{d(!1)}},[r,e]);return t.useEffect(()=>{l()},[l]),{queries:(a==null?void 0:a.data)??[],meta:a==null?void 0:a.meta,isLoading:o,error:i,refresh:l}}function Ns(e){const{client:r}=w(),[a,c]=t.useState(null),[o,d]=t.useState(!0),[i,u]=t.useState(null),l=t.useCallback(async()=>{d(!0),u(null);try{const n=await r.report.getDashboard(e);c(n)}catch(n){u(n instanceof Error?n.message:"Failed to load dashboard")}finally{d(!1)}},[r,e]);return t.useEffect(()=>{l()},[l]),{dashboard:a,isLoading:o,error:i,refresh:l}}function ws(e,r,a="https://api.saas-support.com/v1"){const c=t.useMemo(()=>{const p=new L.Transport(a,{type:"embedToken",token:e});return new L.ReportClient(p)},[e,a]),[o,d]=t.useState(null),[i,u]=t.useState(!0),[l,n]=t.useState(null),x=t.useCallback(async()=>{u(!0),n(null);try{const p=await c.getDashboard(r);d(p)}catch(p){n(p instanceof Error?p.message:"Failed to load dashboard")}finally{u(!1)}},[c,r]);return t.useEffect(()=>{x()},[x]),{dashboard:o,reportClient:c,isLoading:i,error:l,refresh:x}}function Ss({onResult:e,mode:r="both",placeholder:a,appearance:c}){const{appearance:o}=w(),{execute:d,isLoading:i,error:u}=U(),l=c??o,[n,x]=t.useState(""),[p,m]=t.useState(r==="sql"?"sql":"nl"),y=t.useCallback(async h=>{if(h.preventDefault(),!n.trim())return;const f=await d(p==="sql"?{sql:n}:{naturalLanguage:n});f&&(e==null||e(f))},[n,p,d,e]);return s.jsx(P,{appearance:l,children:s.jsxs("div",{className:"ss-card ss-card-wide",children:[r==="both"&&s.jsxs("div",{className:"ss-tab-group ss-tab-group-sm",children:[s.jsx("button",{type:"button",className:`ss-tab ${p==="nl"?"ss-tab-active":""}`,onClick:()=>m("nl"),children:"Natural Language"}),s.jsx("button",{type:"button",className:`ss-tab ${p==="sql"?"ss-tab-active":""}`,onClick:()=>m("sql"),children:"SQL"})]}),u&&s.jsx("div",{className:"ss-global-error",children:u}),s.jsxs("form",{onSubmit:y,children:[s.jsx("div",{className:"ss-field",children:s.jsx("textarea",{className:"ss-input ss-query-textarea",placeholder:a??(p==="sql"?"SELECT ...":"Ask a question about your data..."),value:n,onChange:h=>x(h.target.value),rows:3})}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:i||!n.trim(),children:[i&&s.jsx("span",{className:"ss-spinner"}),"Run query"]})]})]})})}function X({columns:e,rows:r,sortable:a=!0,maxRows:c,appearance:o}){const{appearance:d}=w(),i=o??d,[u,l]=t.useState(null),[n,x]=t.useState("asc"),p=t.useMemo(()=>u?[...r].sort((g,f)=>{const b=g[u],j=f[u];if(b==null&&j==null)return 0;if(b==null)return 1;if(j==null)return-1;if(typeof b=="number"&&typeof j=="number")return n==="asc"?b-j:j-b;const v=String(b),N=String(j);return n==="asc"?v.localeCompare(N):N.localeCompare(v)}):r,[r,u,n]),m=c?p.slice(0,c):p,y=g=>{a&&(u===g?x(f=>f==="asc"?"desc":"asc"):(l(g),x("asc")))};function h(g){return g==null?"":typeof g=="object"?JSON.stringify(g):String(g)}return s.jsx(P,{appearance:i,children:s.jsxs("div",{className:"ss-table-container",children:[s.jsxs("table",{className:"ss-table",children:[s.jsx("thead",{children:s.jsx("tr",{children:e.map(g=>s.jsxs("th",{className:`ss-th ${a?"ss-th-sortable":""} ${u===g?n==="asc"?"ss-sorted-asc":"ss-sorted-desc":""}`,onClick:()=>y(g),children:[g,u===g&&s.jsx("span",{className:"ss-sort-indicator",children:n==="asc"?" ▲":" ▼"})]},g))})}),s.jsx("tbody",{children:m.map((g,f)=>s.jsx("tr",{className:"ss-tr",children:e.map(b=>s.jsx("td",{className:"ss-td",children:h(g[b])},b))},f))})]}),c&&r.length>c&&s.jsxs("div",{className:"ss-table-footer",children:["Showing ",c," of ",r.length," rows"]})]})})}function ks(e,r="#6366f1"){const a=parseInt(r.replace("#","").slice(0,2),16),c=[];for(let o=0;o<e;o++){const d=(a+o*Math.floor(360/Math.max(e,1)))%360;c.push(`hsl(${d}, 65%, 55%)`)}return c}function Cs({labels:e,values:r,w:a,h:c,colors:o}){const d=Math.max(...r,1),i=40,u=a-i*2,l=c-i*2,n=Math.max(1,u/e.length-4);return s.jsxs("g",{children:[s.jsx("line",{x1:i,y1:c-i,x2:a-i,y2:c-i,stroke:"#ccc",strokeWidth:1}),r.map((x,p)=>{const m=x/d*l,y=i+u/e.length*p+2,h=c-i-m;return s.jsxs("g",{children:[s.jsx("rect",{x:y,y:h,width:n,height:m,fill:o[p%o.length],rx:2}),s.jsx("text",{x:y+n/2,y:c-i+14,textAnchor:"middle",fontSize:10,fill:"#666",children:e[p].length>8?e[p].slice(0,8)+"...":e[p]})]},p)})]})}function $s({labels:e,values:r,w:a,h:c,colors:o}){const d=Math.max(...r,1),i=40,u=a-i*2,l=c-i*2,n=e.length>1?u/(e.length-1):0,x=r.map((p,m)=>{const y=i+n*m,h=c-i-p/d*l;return`${y},${h}`});return s.jsxs("g",{children:[[.25,.5,.75,1].map(p=>{const m=c-i-p*l;return s.jsx("line",{x1:i,y1:m,x2:a-i,y2:m,stroke:"#eee",strokeWidth:1},p)}),s.jsx("polyline",{points:x.join(" "),fill:"none",stroke:o[0],strokeWidth:2}),r.map((p,m)=>{const y=i+n*m,h=c-i-p/d*l;return s.jsx("circle",{cx:y,cy:h,r:4,fill:o[0]},m)})]})}function Es({labels:e,values:r,w:a,h:c,colors:o}){const d=r.reduce((m,y)=>m+y,0)||1,i=a/2,u=c/2-20,l=Math.min(a,c)/2-40,n=2*Math.PI*l;let x=0;const p=r.map((m,y)=>{const h=m/d,g=h*n,f={dash:g,offset:x,color:o[y%o.length],label:e[y],pct:h};return x+=g,f});return s.jsxs("g",{children:[p.map((m,y)=>s.jsx("circle",{cx:i,cy:u,r:l,fill:"none",stroke:m.color,strokeWidth:l*.6,strokeDasharray:`${m.dash} ${n-m.dash}`,strokeDashoffset:-m.offset,transform:`rotate(-90 ${i} ${u})`},y)),s.jsx("g",{transform:`translate(${i-e.length*30}, ${c-20})`,children:p.slice(0,6).map((m,y)=>s.jsxs("g",{transform:`translate(${y*60}, 0)`,children:[s.jsx("rect",{width:10,height:10,fill:m.color,rx:2}),s.jsx("text",{x:14,y:9,fontSize:9,fill:"#666",children:m.label.length>6?m.label.slice(0,6)+"..":m.label})]},y))})]})}function Z({type:e,data:r,title:a,width:c=400,height:o=300,appearance:d}){const{appearance:i}=w(),u=d??i,l=t.useMemo(()=>ks(r.labels.length),[r.labels.length]);return s.jsx(P,{appearance:u,children:s.jsxs("div",{className:"ss-chart-container",children:[a&&s.jsx("h3",{className:"ss-chart-title",children:a}),s.jsxs("svg",{viewBox:`0 0 ${c} ${o}`,width:"100%",style:{maxWidth:c},children:[e==="bar"&&s.jsx(Cs,{labels:r.labels,values:r.values,w:c,h:o,colors:l}),e==="line"&&s.jsx($s,{labels:r.labels,values:r.values,w:c,h:o,colors:l}),e==="pie"&&s.jsx(Es,{labels:r.labels,values:r.values,w:c,h:o,colors:l})]})]})})}function Ps({dashboardId:e,embedToken:r,baseUrl:a,refreshInterval:c,appearance:o}){const d=w(),i=o??(d==null?void 0:d.appearance),u=t.useMemo(()=>{if(r){const b=a??"https://api.saas-support.com/v1",j=new L.Transport(b,{type:"embedToken",token:r});return new L.ReportClient(j)}return d.client.report},[r,a,d]),[l,n]=t.useState([]),[x,p]=t.useState({}),[m,y]=t.useState(!0),[h,g]=t.useState(null),f=t.useCallback(async()=>{y(!0),g(null);try{const b=await u.getDashboard(e),j=JSON.parse(b.layoutJson||"[]");n(j);const v={};for(const N of j)try{const k=await u.listQueries({search:N.queryId,perPage:1});if(k.data.length>0&&k.data[0].generatedSql){const T=await u.executeQuery({sql:k.data[0].generatedSql});v[N.queryId]=T}}catch{}p(v)}catch(b){g(b instanceof Error?b.message:"Failed to load dashboard")}finally{y(!1)}},[u,e]);return t.useEffect(()=>{f()},[f]),t.useEffect(()=>{if(!c||c<=0)return;const b=setInterval(f,c*1e3);return()=>clearInterval(b)},[c,f]),s.jsx(P,{appearance:i,children:s.jsxs("div",{className:"ss-dashboard-grid",children:[m&&s.jsxs("div",{className:"ss-loading",children:[s.jsx("span",{className:"ss-spinner"})," Loading dashboard..."]}),h&&s.jsx("div",{className:"ss-global-error",children:h}),!m&&l.map((b,j)=>{const v=x[b.queryId];if(!v)return null;const N=v.columns.length>=2?{labels:v.rows.map(k=>String(k[v.columns[0]]??"")),values:v.rows.map(k=>Number(k[v.columns[1]]??0))}:{labels:[],values:[]};return s.jsxs("div",{className:"ss-widget",children:[b.title&&s.jsx("h4",{className:"ss-widget-header",children:b.title}),b.chartType==="table"?s.jsx(X,{columns:v.columns,rows:v.rows,maxRows:50}):s.jsx(Z,{type:b.chartType||"bar",data:N,width:b.w,height:b.h})]},j)})]})})}function Ts(e){return new Date(e).toLocaleDateString("en-US",{year:"numeric",month:"short",day:"numeric"})}function Is({onSelectQuery:e,onRunQuery:r,appearance:a}){const{appearance:c}=w(),{queries:o,isLoading:d,error:i}=K(),{execute:u,isLoading:l}=U(),n=a??c,x=t.useCallback(async p=>{if(!p.generatedSql)return;const m=await u({sql:p.generatedSql});m&&(r==null||r(m))},[u,r]);return s.jsx(P,{appearance:n,children:s.jsxs("div",{className:"ss-card ss-card-wide",children:[s.jsx("h2",{className:"ss-title",children:"Saved Queries"}),d&&s.jsxs("div",{className:"ss-loading",children:[s.jsx("span",{className:"ss-spinner"})," Loading..."]}),i&&s.jsx("div",{className:"ss-global-error",children:i}),!d&&o.length===0&&s.jsx("p",{className:"ss-empty",children:"No saved queries."}),!d&&o.map(p=>s.jsxs("div",{className:"ss-saved-query-card",onClick:()=>e==null?void 0:e(p),children:[s.jsxs("div",{className:"ss-saved-query-header",children:[s.jsx("span",{className:"ss-saved-query-name",children:p.name}),p.chartType&&s.jsx("span",{className:"ss-badge",children:p.chartType})]}),p.naturalLanguage&&s.jsx("p",{className:"ss-saved-query-desc",children:p.naturalLanguage}),s.jsxs("div",{className:"ss-saved-query-footer",children:[s.jsx("span",{className:"ss-saved-query-date",children:Ts(p.createdAt)}),s.jsx("button",{type:"button",className:"ss-btn ss-btn-sm ss-btn-primary",disabled:l||!p.generatedSql,onClick:m=>{m.stopPropagation(),x(p)},children:l?s.jsx("span",{className:"ss-spinner"}):"Run"})]})]},p.id))]})})}function Ls({embedToken:e,dashboardId:r,baseUrl:a="https://api.saas-support.com/v1",refreshInterval:c,appearance:o}){const d=t.useMemo(()=>{const g=new L.Transport(a,{type:"embedToken",token:e});return new L.ReportClient(g)},[e,a]),[i,u]=t.useState([]),[l,n]=t.useState(!0),[x,p]=t.useState(null),m=t.useMemo(()=>z(o),[o]),y=t.useMemo(()=>q(m),[m]),h=t.useCallback(async()=>{n(!0),p(null);try{const g=await d.getDashboard(r),f=JSON.parse(g.layoutJson||"[]"),b=await Promise.all(f.map(async j=>{try{const v=await d.listQueries({search:j.queryId,perPage:1});if(v.data.length>0&&v.data[0].generatedSql){const N=await d.executeQuery({sql:v.data[0].generatedSql});return{...j,result:N}}}catch{}return j}));u(b)}catch(g){p(g instanceof Error?g.message:"Failed to load dashboard")}finally{n(!1)}},[d,r]);return t.useEffect(()=>{h()},[h]),t.useEffect(()=>{if(!c||c<=0)return;const g=setInterval(h,c*1e3);return()=>clearInterval(g)},[c,h]),s.jsx("div",{ref:g=>{if(!g||g.shadowRoot)return;const f=g.attachShadow({mode:"open"}),b=document.createElement("style");b.textContent=y,f.appendChild(b);const j=document.createElement("div");f.appendChild(j)},style:{display:"contents"},children:s.jsxs("div",{className:"ss-dashboard-grid",children:[l&&s.jsx("div",{className:"ss-loading",children:"Loading dashboard..."}),x&&s.jsx("div",{className:"ss-global-error",children:x}),!l&&i.map((g,f)=>{if(!g.result)return null;const{columns:b,rows:j}=g.result;return s.jsxs("div",{className:"ss-widget",children:[g.title&&s.jsx("h4",{className:"ss-widget-header",children:g.title}),s.jsxs("table",{className:"ss-table",children:[s.jsx("thead",{children:s.jsx("tr",{children:b.map(v=>s.jsx("th",{className:"ss-th",children:v},v))})}),s.jsx("tbody",{children:j.slice(0,50).map((v,N)=>s.jsx("tr",{className:"ss-tr",children:b.map(k=>s.jsx("td",{className:"ss-td",children:String(v[k]??"")},k))},N))})]})]},f)})]})})}exports.SaaSError=L.SaaSError;exports.SaaSSupport=L.SaaSSupport;exports.isMfaRequired=L.isMfaRequired;exports.Chart=Z;exports.CouponInput=js;exports.DashboardView=Ps;exports.DataTable=X;exports.InvoiceHistory=J;exports.OrgSwitcher=us;exports.PaymentPortal=ys;exports.PricingTable=xs;exports.QueryInput=Ss;exports.ReportEmbed=Ls;exports.SaaSContext=D;exports.SaaSProvider=es;exports.SavedQueryList=Is;exports.SignIn=os;exports.SignUp=ls;exports.SubscriptionStatus=V;exports.UsageDisplay=Y;exports.UserButton=is;exports.UserProfile=ds;exports.useAuth=M;exports.useBilling=ms;exports.useDashboard=Ns;exports.useEmbedDashboard=ws;exports.useInvoices=Q;exports.useOrg=O;exports.useProfile=A;exports.useQuery=U;exports.useReport=vs;exports.useSaaSContext=w;exports.useSavedQueries=K;exports.useSignIn=F;exports.useSignUp=R;exports.useSubscription=_;exports.useUsage=G;exports.useUser=ns;
|
|
728
|
+
`}function P({children:e,appearance:r}){const a=t.useRef(null),[c,o]=t.useState(null);t.useEffect(()=>{var m;if(!a.current||a.current.shadowRoot){o(((m=a.current)==null?void 0:m.shadowRoot)??null);return}const i=a.current.attachShadow({mode:"open"}),u=z(r),l=document.createElement("style");l.textContent=q(u),i.appendChild(l);const n=document.createElement("div");i.appendChild(n),o(i)},[]),t.useEffect(()=>{if(!c)return;const i=c.querySelector("style");if(i){const u=z(r);i.textContent=q(u)}},[r,c]);const d=(c==null?void 0:c.querySelector("div"))??null;return s.jsx("div",{ref:a,style:{display:"contents"},children:d&&ss.createPortal(e,d)})}function M(){const{client:e,user:r,isLoaded:a}=w();return{isLoaded:a,isSignedIn:!!r,user:r,signOut:t.useCallback(()=>e.auth.signOut(),[e]),getToken:t.useCallback(()=>e.auth.getToken(),[e])}}function ns(){const{user:e,isLoaded:r}=w();return{user:e,isLoaded:r}}function F(){const{client:e}=w(),[r,a]=t.useState(!1),[c,o]=t.useState(null),d=t.useCallback(async(l,n)=>{a(!0),o(null);try{return await e.auth.signIn(l,n)}catch(m){return o(m instanceof Error?m.message:"Sign in failed"),null}finally{a(!1)}},[e]),i=t.useCallback(async l=>{a(!0),o(null);try{return await e.auth.signInWithOAuth(l)}catch(n){return o(n instanceof Error?n.message:"OAuth sign in failed"),null}finally{a(!1)}},[e]),u=t.useCallback(async(l,n)=>{a(!0),o(null);try{return await e.auth.submitMfaCode(l,n)}catch(m){return o(m instanceof Error?m.message:"MFA verification failed"),null}finally{a(!1)}},[e]);return{signIn:d,signInWithOAuth:i,submitMfaCode:u,isLoading:r,error:c,setError:o}}function R(){const{client:e}=w(),[r,a]=t.useState(!1),[c,o]=t.useState(null);return{signUp:t.useCallback(async(i,u)=>{a(!0),o(null);try{return await e.auth.signUp(i,u)}catch(l){return o(l instanceof Error?l.message:"Sign up failed"),null}finally{a(!1)}},[e]),isLoading:r,error:c,setError:o}}function O(){const{client:e}=w(),[r,a]=t.useState([]),[c,o]=t.useState(null),[d,i]=t.useState([]),[u,l]=t.useState(!1),[n,m]=t.useState(null),p=t.useCallback(async()=>{l(!0),m(null);try{const h=await e.auth.listOrgs();a(h)}catch(h){m(h instanceof Error?h.message:"Failed to load organizations")}finally{l(!1)}},[e]);t.useEffect(()=>{p()},[p]);const x=t.useCallback(async h=>{try{const g=await e.auth.getOrg(h);o(g);const f=await e.auth.listMembers(h);i(f)}catch(g){m(g instanceof Error?g.message:"Failed to load organization")}},[e]),y=t.useCallback(async(h,g)=>{try{const f=await e.auth.createOrg(h,g);return a(b=>[...b,f]),f}catch(f){return m(f instanceof Error?f.message:"Failed to create organization"),null}},[e]);return{orgs:r,selectedOrg:c,members:d,isLoading:u,error:n,refresh:p,selectOrg:x,createOrg:y}}function A(){const{client:e,user:r}=w(),[a,c]=t.useState(!1),[o,d]=t.useState(null),[i,u]=t.useState(null),l=t.useCallback(async m=>{c(!0),d(null),u(null);try{const p=await e.auth.updateProfile(m);return u("Profile updated"),p}catch(p){return d(p instanceof Error?p.message:"Failed to update profile"),null}finally{c(!1)}},[e]),n=t.useCallback(async(m,p)=>{c(!0),d(null),u(null);try{return await e.auth.changePassword(m,p),u("Password changed successfully"),!0}catch(x){return d(x instanceof Error?x.message:"Failed to change password"),!1}finally{c(!1)}},[e]);return{user:r,updateProfile:l,changePassword:n,isLoading:a,error:o,success:i,setError:d,setSuccess:u}}const W='<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92a5.06 5.06 0 0 1-2.2 3.32v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.1z" fill="#4285F4"/><path d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z" fill="#34A853"/><path d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z" fill="#FBBC05"/><path d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z" fill="#EA4335"/></svg>',H='<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M12 0C5.37 0 0 5.37 0 12c0 5.31 3.435 9.795 8.205 11.385.6.105.825-.255.825-.57 0-.285-.015-1.23-.015-2.235-3.015.555-3.795-.735-4.035-1.41-.135-.345-.72-1.41-1.23-1.695-.42-.225-1.02-.78-.015-.795.945-.015 1.62.87 1.845 1.23 1.08 1.815 2.805 1.305 3.495.99.105-.78.42-1.305.765-1.605-2.67-.3-5.46-1.335-5.46-5.925 0-1.305.465-2.385 1.23-3.225-.12-.3-.54-1.53.12-3.18 0 0 1.005-.315 3.3 1.23.96-.27 1.98-.405 3-.405s2.04.135 3 .405c2.295-1.56 3.3-1.23 3.3-1.23.66 1.65.24 2.88.12 3.18.765.84 1.23 1.905 1.23 3.225 0 4.605-2.805 5.625-5.475 5.925.435.375.81 1.095.81 2.22 0 1.605-.015 2.895-.015 3.3 0 .315.225.69.825.57A12.02 12.02 0 0 0 24 12c0-6.63-5.37-12-12-12z" fill="currentColor"/></svg>';function os({appearance:e,signUpUrl:r,onSignUp:a}){const{appearance:c,settings:o}=w(),{signIn:d,signInWithOAuth:i,submitMfaCode:u,isLoading:l,error:n,setError:m}=F(),p=e??c,[x,y]=t.useState(""),[h,g]=t.useState(""),[f,b]=t.useState(!1),[j,v]=t.useState(""),[N,k]=t.useState(""),T=t.useCallback(async I=>{if(I.preventDefault(),f){await u(j,N);return}const C=await d(x,h);C&&L.isMfaRequired(C)&&(v(C.mfaToken),b(!0),m(null))},[x,h,f,j,N,d,u,m]),$=t.useCallback(async I=>{await i(I)},[i]),S=(o==null?void 0:o.googleEnabled)||(o==null?void 0:o.githubEnabled);return s.jsx(P,{appearance:p,children:s.jsxs("div",{className:"ss-card",children:[s.jsx("h2",{className:"ss-title",children:"Sign in"}),!f&&s.jsxs(s.Fragment,{children:[(o==null?void 0:o.googleEnabled)&&s.jsxs("button",{type:"button",className:"ss-btn-social",onClick:()=>$("google"),disabled:l,children:[s.jsx("span",{dangerouslySetInnerHTML:{__html:W}}),"Continue with Google"]}),(o==null?void 0:o.githubEnabled)&&s.jsxs("button",{type:"button",className:"ss-btn-social",onClick:()=>$("github"),disabled:l,children:[s.jsx("span",{dangerouslySetInnerHTML:{__html:H}}),"Continue with GitHub"]}),S&&s.jsx("div",{className:"ss-divider",children:"or"})]}),n&&s.jsx("div",{className:"ss-global-error",children:n}),s.jsxs("form",{onSubmit:T,children:[f?s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-mfa-code",children:"Authentication code"}),s.jsx("input",{id:"ss-mfa-code",className:"ss-input",type:"text",inputMode:"numeric",autoComplete:"one-time-code",placeholder:"Enter 6-digit code",value:N,onChange:I=>k(I.target.value),autoFocus:!0})]}):s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-email",children:"Email"}),s.jsx("input",{id:"ss-email",className:"ss-input",type:"email",autoComplete:"email",placeholder:"you@example.com",value:x,onChange:I=>y(I.target.value),required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-password",children:"Password"}),s.jsx("input",{id:"ss-password",className:"ss-input",type:"password",autoComplete:"current-password",placeholder:"Enter your password",value:h,onChange:I=>g(I.target.value),required:!0})]})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:l,children:[l&&s.jsx("span",{className:"ss-spinner"}),f?"Verify":"Continue"]})]}),f&&s.jsx("div",{className:"ss-footer",children:s.jsx("span",{className:"ss-link",onClick:()=>{b(!1),k(""),m(null)},children:"Back to sign in"})}),!f&&s.jsxs("div",{className:"ss-footer",children:["Don't have an account?"," ",a?s.jsx("span",{className:"ss-link",onClick:a,children:"Sign up"}):r?s.jsx("a",{className:"ss-link",href:r,children:"Sign up"}):s.jsx("span",{className:"ss-link",children:"Sign up"})]})]})})}function ls({appearance:e,signInUrl:r,onSignIn:a}){const{appearance:c,settings:o}=w(),{signUp:d,isLoading:i,error:u,setError:l}=R(),{signInWithOAuth:n}=F(),m=e??c,[p,x]=t.useState(""),[y,h]=t.useState(""),[g,f]=t.useState(""),[b,j]=t.useState(null),v=t.useCallback(async $=>{if($.preventDefault(),j(null),y!==g){j("Passwords do not match");return}const S=(o==null?void 0:o.passwordMinLength)??8;if(y.length<S){j(`Password must be at least ${S} characters`);return}await d(p,y)},[p,y,g,o,d]),N=t.useCallback(async $=>{await n($)},[n]),k=(o==null?void 0:o.googleEnabled)||(o==null?void 0:o.githubEnabled),T=b||u;return s.jsx(P,{appearance:m,children:s.jsxs("div",{className:"ss-card",children:[s.jsx("h2",{className:"ss-title",children:"Create account"}),(o==null?void 0:o.googleEnabled)&&s.jsxs("button",{type:"button",className:"ss-btn-social",onClick:()=>N("google"),disabled:i,children:[s.jsx("span",{dangerouslySetInnerHTML:{__html:W}}),"Continue with Google"]}),(o==null?void 0:o.githubEnabled)&&s.jsxs("button",{type:"button",className:"ss-btn-social",onClick:()=>N("github"),disabled:i,children:[s.jsx("span",{dangerouslySetInnerHTML:{__html:H}}),"Continue with GitHub"]}),k&&s.jsx("div",{className:"ss-divider",children:"or"}),T&&s.jsx("div",{className:"ss-global-error",children:T}),s.jsxs("form",{onSubmit:v,children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-signup-email",children:"Email"}),s.jsx("input",{id:"ss-signup-email",className:"ss-input",type:"email",autoComplete:"email",placeholder:"you@example.com",value:p,onChange:$=>x($.target.value),required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-signup-password",children:"Password"}),s.jsx("input",{id:"ss-signup-password",className:"ss-input",type:"password",autoComplete:"new-password",placeholder:"Create a password",value:y,onChange:$=>{h($.target.value),j(null)},required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-signup-confirm",children:"Confirm password"}),s.jsx("input",{id:"ss-signup-confirm",className:"ss-input",type:"password",autoComplete:"new-password",placeholder:"Confirm your password",value:g,onChange:$=>{f($.target.value),j(null)},required:!0})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:i,children:[i&&s.jsx("span",{className:"ss-spinner"}),"Create account"]})]}),s.jsxs("div",{className:"ss-footer",children:["Already have an account?"," ",a?s.jsx("span",{className:"ss-link",onClick:a,children:"Sign in"}):r?s.jsx("a",{className:"ss-link",href:r,children:"Sign in"}):s.jsx("span",{className:"ss-link",children:"Sign in"})]})]})})}function is({appearance:e}){const{appearance:r}=w(),{user:a,signOut:c}=M(),o=e??r,[d,i]=t.useState(!1),[u,l]=t.useState(!1),n=t.useRef(null),m=t.useCallback(x=>{n.current&&!x.composedPath().includes(n.current)&&i(!1)},[]);if(t.useEffect(()=>{if(d){const x=setTimeout(()=>{document.addEventListener("click",m)},0);return()=>{clearTimeout(x),document.removeEventListener("click",m)}}},[d,m]),!a)return null;const p=(a.name||a.email).charAt(0).toUpperCase();return s.jsx(P,{appearance:o,children:s.jsxs("div",{className:"ss-user-btn",ref:n,children:[s.jsx("button",{type:"button",className:"ss-avatar",onClick:()=>i(!d),"aria-label":"User menu",children:a.avatarUrl?s.jsx("img",{src:a.avatarUrl,alt:"",style:{width:"100%",height:"100%",borderRadius:"50%",objectFit:"cover"}}):p}),d&&s.jsxs("div",{className:"ss-dropdown",children:[s.jsxs("div",{className:"ss-dropdown-header",children:[a.name&&s.jsx("div",{style:{fontWeight:600,fontSize:"14px",color:"inherit"},children:a.name}),s.jsx("div",{className:"ss-dropdown-email",children:a.email})]}),s.jsx("button",{type:"button",className:"ss-dropdown-item",onClick:()=>{i(!1),l(!0)},children:"Profile"}),s.jsx("button",{type:"button",className:"ss-dropdown-item ss-dropdown-item-danger",onClick:async()=>{i(!1),await c()},children:"Sign out"})]}),u&&s.jsx(cs,{onClose:()=>l(!1)})]})})}function cs({onClose:e}){const{user:r,updateProfile:a,changePassword:c,isLoading:o,error:d,success:i,setError:u,setSuccess:l}=A(),[n,m]=t.useState((r==null?void 0:r.name)??""),[p,x]=t.useState((r==null?void 0:r.avatarUrl)??""),[y,h]=t.useState(""),[g,f]=t.useState(""),[b,j]=t.useState(""),[v,N]=t.useState(null),k=t.useCallback(async S=>{S.preventDefault(),u(null),l(null),await a({name:n,avatarUrl:p})},[n,p,a,u,l]),T=t.useCallback(async S=>{if(S.preventDefault(),N(null),u(null),l(null),g!==b){N("Passwords do not match");return}if(g.length<8){N("Password must be at least 8 characters");return}await c(y,g)&&(h(""),f(""),j(""))},[y,g,b,c,u,l]),$=(r==null?void 0:r.provider)==="email";return s.jsx("div",{className:"ss-modal-overlay",onClick:S=>{S.target===S.currentTarget&&e()},children:s.jsxs("div",{className:"ss-modal",children:[s.jsxs("div",{className:"ss-modal-header",children:[s.jsx("span",{className:"ss-modal-title",children:"Profile"}),s.jsx("button",{type:"button",className:"ss-modal-close",onClick:e,children:"✕"})]}),d&&s.jsx("div",{className:"ss-global-error",children:d}),i&&s.jsx("div",{className:"ss-success-msg",children:i}),s.jsx("div",{className:"ss-avatar-preview",children:p?s.jsx("img",{src:p,alt:""}):(n||(r==null?void 0:r.email)||"?").charAt(0).toUpperCase()}),s.jsxs("form",{onSubmit:k,children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Name"}),s.jsx("input",{className:"ss-input",type:"text",placeholder:"Your name",value:n,onChange:S=>m(S.target.value)})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Avatar URL"}),s.jsx("input",{className:"ss-input",type:"url",placeholder:"https://example.com/avatar.jpg",value:p,onChange:S=>x(S.target.value)})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Email"}),s.jsx("input",{className:"ss-input ss-input-readonly",type:"email",value:(r==null?void 0:r.email)??"",disabled:!0,readOnly:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Provider"}),s.jsx("input",{className:"ss-input ss-input-readonly",type:"text",value:(r==null?void 0:r.provider)??"",disabled:!0,readOnly:!0})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:o,children:[o&&s.jsx("span",{className:"ss-spinner"}),"Save changes"]})]}),$&&s.jsxs("div",{className:"ss-modal-section",children:[s.jsx("div",{className:"ss-modal-section-title",children:"Change password"}),v&&s.jsx("div",{className:"ss-global-error",children:v}),s.jsxs("form",{onSubmit:T,children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Current password"}),s.jsx("input",{className:"ss-input",type:"password",value:y,onChange:S=>h(S.target.value),required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"New password"}),s.jsx("input",{className:"ss-input",type:"password",value:g,onChange:S=>f(S.target.value),required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Confirm new password"}),s.jsx("input",{className:"ss-input",type:"password",value:b,onChange:S=>j(S.target.value),required:!0})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:o,children:[o&&s.jsx("span",{className:"ss-spinner"}),"Update password"]})]})]})]})})}function ds({appearance:e}){const{appearance:r}=w(),{user:a,signOut:c}=M(),{updateProfile:o,changePassword:d,isLoading:i,error:u,success:l,setError:n,setSuccess:m}=A(),p=e??r,[x,y]=t.useState((a==null?void 0:a.name)??""),[h,g]=t.useState((a==null?void 0:a.avatarUrl)??""),[f,b]=t.useState(""),[j,v]=t.useState(""),[N,k]=t.useState(""),[T,$]=t.useState(null),S=t.useCallback(async E=>{E.preventDefault(),n(null),m(null),await o({name:x,avatarUrl:h})},[x,h,o,n,m]),I=t.useCallback(async E=>{if(E.preventDefault(),$(null),n(null),m(null),j!==N){$("Passwords do not match");return}if(j.length<8){$("Password must be at least 8 characters");return}await d(f,j)&&(b(""),v(""),k(""))},[f,j,N,d,n,m]);if(!a)return null;const C=a.provider==="email";return s.jsx(P,{appearance:p,children:s.jsxs("div",{className:"ss-card ss-card-wide",children:[s.jsx("h2",{className:"ss-title",children:"Profile"}),u&&s.jsx("div",{className:"ss-global-error",children:u}),l&&s.jsx("div",{className:"ss-success-msg",children:l}),s.jsx("div",{className:"ss-avatar-preview",style:{margin:"0 auto 16px"},children:h?s.jsx("img",{src:h,alt:""}):(x||a.email).charAt(0).toUpperCase()}),s.jsxs("form",{onSubmit:S,children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Name"}),s.jsx("input",{className:"ss-input",type:"text",placeholder:"Your name",value:x,onChange:E=>y(E.target.value)})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Avatar URL"}),s.jsx("input",{className:"ss-input",type:"url",placeholder:"https://example.com/avatar.jpg",value:h,onChange:E=>g(E.target.value)})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Email"}),s.jsx("input",{className:"ss-input ss-input-readonly",type:"email",value:a.email,disabled:!0,readOnly:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Provider"}),s.jsx("input",{className:"ss-input ss-input-readonly",type:"text",value:a.provider,disabled:!0,readOnly:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Email verified"}),s.jsx("input",{className:"ss-input ss-input-readonly",type:"text",value:a.emailVerified?"Yes":"No",disabled:!0,readOnly:!0})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:i,children:[i&&s.jsx("span",{className:"ss-spinner"}),"Save changes"]})]}),C&&s.jsxs("div",{className:"ss-modal-section",children:[s.jsx("div",{className:"ss-modal-section-title",children:"Change password"}),T&&s.jsx("div",{className:"ss-global-error",children:T}),s.jsxs("form",{onSubmit:I,children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Current password"}),s.jsx("input",{className:"ss-input",type:"password",value:f,onChange:E=>b(E.target.value),required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"New password"}),s.jsx("input",{className:"ss-input",type:"password",value:j,onChange:E=>v(E.target.value),required:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Confirm new password"}),s.jsx("input",{className:"ss-input",type:"password",value:N,onChange:E=>k(E.target.value),required:!0})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:i,children:[i&&s.jsx("span",{className:"ss-spinner"}),"Update password"]})]})]}),s.jsx("div",{style:{marginTop:"24px"},children:s.jsx("button",{type:"button",className:"ss-btn ss-btn-danger",onClick:()=>c(),children:"Sign out"})})]})})}function us({appearance:e,onOrgChange:r}){const{appearance:a}=w(),{orgs:c,selectedOrg:o,selectOrg:d,createOrg:i,isLoading:u}=O(),l=e??a,[n,m]=t.useState(!1),[p,x]=t.useState(!1),[y,h]=t.useState(""),[g,f]=t.useState(""),[b,j]=t.useState(null),[v,N]=t.useState(!1),k=t.useRef(null),T=t.useCallback(C=>{k.current&&!C.composedPath().includes(k.current)&&(m(!1),x(!1))},[]);t.useEffect(()=>{if(n){const C=setTimeout(()=>{document.addEventListener("click",T)},0);return()=>{clearTimeout(C),document.removeEventListener("click",T)}}},[n,T]);const $=t.useCallback(C=>{h(C),f(C.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,""))},[]),S=t.useCallback(async C=>{C.preventDefault(),j(null),N(!0);try{const E=await i(y,g);E&&(await d(E.id),r==null||r(E),x(!1),h(""),f(""),m(!1))}catch(E){j(E instanceof Error?E.message:"Failed to create organization")}finally{N(!1)}},[y,g,i,d,r]);if(u)return null;const I=(o==null?void 0:o.name)??(c.length===0?"No organization":"Select organization");return s.jsx(P,{appearance:l,children:s.jsxs("div",{className:"ss-user-btn",ref:k,children:[s.jsxs("button",{type:"button",className:"ss-btn ss-btn-org-switcher",onClick:()=>m(!n),children:[I,s.jsx("span",{className:"ss-chevron",children:n?"▲":"▼"})]}),n&&s.jsxs("div",{className:"ss-dropdown ss-dropdown-org",children:[c.map(C=>s.jsx("button",{type:"button",className:`ss-dropdown-item ${(o==null?void 0:o.id)===C.id?"ss-dropdown-item-active":""}`,onClick:async()=>{m(!1),x(!1),await d(C.id),r==null||r(C)},children:C.name},C.id)),s.jsx("div",{className:"ss-dropdown-divider"}),p?s.jsxs("div",{className:"ss-inline-form",children:[b&&s.jsx("div",{className:"ss-global-error",style:{marginBottom:"8px",fontSize:"12px"},children:b}),s.jsxs("form",{onSubmit:S,children:[s.jsx("div",{className:"ss-field",children:s.jsx("input",{className:"ss-input",type:"text",placeholder:"Organization name",value:y,onChange:C=>$(C.target.value),required:!0,autoFocus:!0})}),s.jsx("div",{className:"ss-field",children:s.jsx("input",{className:"ss-input",type:"text",placeholder:"org-slug",value:g,onChange:C=>f(C.target.value),required:!0})}),s.jsxs("div",{className:"ss-btn-group",style:{marginTop:"8px"},children:[s.jsx("button",{type:"button",className:"ss-btn ss-btn-sm ss-btn-current",onClick:()=>{x(!1),j(null)},children:"Cancel"}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-sm ss-btn-primary",disabled:v,children:[v&&s.jsx("span",{className:"ss-spinner"}),"Create"]})]})]})]}):s.jsx("button",{type:"button",className:"ss-dropdown-item",onClick:()=>x(!0),style:{fontWeight:500},children:"+ Create organization"})]})]})})}function ps(e,r="USD"){return new Intl.NumberFormat("en-US",{style:"currency",currency:r,minimumFractionDigits:e%100===0?0:2}).format(e/100)}function ms({plans:e,currentPlanId:r,onSelectPlan:a,interval:c,appearance:o}){const{appearance:d}=w(),i=o??d,u=c?e.filter(l=>l.interval===c||l.isFree):e;return s.jsx(P,{appearance:i,children:s.jsx("div",{className:"ss-pricing-grid",children:u.map(l=>{const n=l.id===r;return s.jsxs("div",{className:`ss-pricing-card ${n?"ss-pricing-card-current":""}`,children:[s.jsxs("div",{className:"ss-pricing-header",children:[s.jsx("h3",{className:"ss-pricing-name",children:l.name}),l.trialDays>0&&s.jsxs("span",{className:"ss-badge ss-badge-trialing",children:[l.trialDays,"-day trial"]}),l.isFree&&s.jsx("span",{className:"ss-badge ss-badge-active",children:"Free"})]}),s.jsx("div",{className:"ss-pricing-price",children:l.isFree?s.jsx("span",{className:"ss-pricing-amount",children:"Free"}):s.jsxs(s.Fragment,{children:[s.jsx("span",{className:"ss-pricing-amount",children:ps(l.amountCents,l.currency)}),s.jsxs("span",{className:"ss-pricing-interval",children:["/",l.interval]})]})}),l.description&&s.jsx("p",{className:"ss-pricing-desc",children:l.description}),l.features.length>0&&s.jsx("ul",{className:"ss-pricing-features",children:l.features.map((m,p)=>s.jsxs("li",{className:"ss-pricing-feature",children:[s.jsx("span",{className:"ss-check",children:"✓"})," ",m]},p))}),s.jsx("button",{type:"button",className:`ss-btn ${n?"ss-btn-current":"ss-btn-primary"}`,disabled:n,onClick:()=>a(l.id),children:n?"Current plan":"Select plan"})]},l.id)})})})}function xs(){const{client:e}=w();return{billing:e.billing}}function B(e){const{client:r}=w();return t.useMemo(()=>{if(e){const a=new L.Transport("https://api.saas-support.com/v1",{type:"portalToken",token:e});return new L.BillingClient(a)}return r.billing},[r,e])}function _(e,r){const a=B(r),[c,o]=t.useState(null),[d,i]=t.useState(!0),[u,l]=t.useState(null),n=t.useCallback(async()=>{i(!0),l(null);try{const m=await a.getCustomer(e);o(m)}catch(m){l(m instanceof Error?m.message:"Failed to load subscription")}finally{i(!1)}},[a,e]);return t.useEffect(()=>{n()},[n]),{customer:c,isLoading:d,error:u,refresh:n}}function Q(e,r){const a=B(r),[c,o]=t.useState([]),[d,i]=t.useState(!0),[u,l]=t.useState(null),n=t.useCallback(async()=>{i(!0),l(null);try{const m=await a.getInvoices(e);o(m)}catch(m){l(m instanceof Error?m.message:"Failed to load invoices")}finally{i(!1)}},[a,e]);return t.useEffect(()=>{n()},[n]),{invoices:c,isLoading:d,error:u,refresh:n}}function G(e,r){const a=B(r),[c,o]=t.useState([]),[d,i]=t.useState(!0),[u,l]=t.useState(null),n=t.useCallback(async()=>{i(!0),l(null);try{const m=await a.getCurrentUsage(e);o(m)}catch(m){l(m instanceof Error?m.message:"Failed to load usage")}finally{i(!1)}},[a,e]);return t.useEffect(()=>{n()},[n]),{usage:c,isLoading:d,error:u,refresh:n}}const gs={active:"ss-badge-active"};function V({customerId:e,portalToken:r,onChangePlan:a,onCancel:c,appearance:o}){const{appearance:d}=w(),{customer:i,isLoading:u,error:l}=_(e,r),n=o??d;return s.jsx(P,{appearance:n,children:s.jsxs("div",{className:"ss-card",children:[s.jsx("h2",{className:"ss-title",children:"Subscription"}),u&&s.jsxs("div",{className:"ss-loading",children:[s.jsx("span",{className:"ss-spinner"})," Loading..."]}),l&&s.jsx("div",{className:"ss-global-error",children:l}),i&&!u&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Customer"}),s.jsx("input",{className:"ss-input",value:i.name||i.email,disabled:!0,readOnly:!0})]}),s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",children:"Status"}),s.jsx("div",{children:s.jsx("span",{className:`ss-badge ${gs.active}`,children:"Active"})})]}),s.jsxs("div",{className:"ss-btn-group",children:[a&&s.jsx("button",{type:"button",className:"ss-btn ss-btn-primary",onClick:a,children:"Change plan"}),c&&s.jsx("button",{type:"button",className:"ss-btn ss-btn-danger",onClick:c,children:"Cancel subscription"})]})]})]})})}const bs={paid:"ss-badge-active",open:"ss-badge-trialing",draft:"ss-badge-paused",void:"ss-badge-canceled",uncollectible:"ss-badge-past-due"};function hs(e){return new Date(e).toLocaleDateString("en-US",{year:"numeric",month:"short",day:"numeric"})}function fs(e){return new Intl.NumberFormat("en-US",{style:"currency",currency:"USD",minimumFractionDigits:2}).format(e/100)}function J({customerId:e,portalToken:r,appearance:a}){const{appearance:c}=w(),{invoices:o,isLoading:d,error:i}=Q(e,r),u=a??c,l=[...o].sort((n,m)=>new Date(m.createdAt).getTime()-new Date(n.createdAt).getTime());return s.jsx(P,{appearance:u,children:s.jsxs("div",{className:"ss-card ss-card-wide",children:[s.jsx("h2",{className:"ss-title",children:"Invoices"}),d&&s.jsxs("div",{className:"ss-loading",children:[s.jsx("span",{className:"ss-spinner"})," Loading..."]}),i&&s.jsx("div",{className:"ss-global-error",children:i}),!d&&l.length===0&&s.jsx("p",{className:"ss-empty",children:"No invoices yet."}),!d&&l.length>0&&s.jsxs("table",{className:"ss-table",children:[s.jsx("thead",{children:s.jsxs("tr",{children:[s.jsx("th",{className:"ss-th",children:"Date"}),s.jsx("th",{className:"ss-th",children:"Amount"}),s.jsx("th",{className:"ss-th",children:"Status"}),s.jsx("th",{className:"ss-th",children:"PDF"})]})}),s.jsx("tbody",{children:l.map(n=>s.jsxs("tr",{className:"ss-tr",children:[s.jsx("td",{className:"ss-td",children:hs(n.createdAt)}),s.jsx("td",{className:"ss-td",children:fs(n.amountCents)}),s.jsx("td",{className:"ss-td",children:s.jsx("span",{className:`ss-badge ${bs[n.status]||""}`,children:n.status})}),s.jsx("td",{className:"ss-td",children:n.pdfUrl?s.jsx("a",{className:"ss-link",href:n.pdfUrl,target:"_blank",rel:"noopener noreferrer",children:"Download"}):"—"})]},n.id))})]})]})})}function Y({customerId:e,limits:r,portalToken:a,appearance:c}){const{appearance:o}=w(),{usage:d,isLoading:i,error:u}=G(e,a),l=c??o;return s.jsx(P,{appearance:l,children:s.jsxs("div",{className:"ss-card",children:[s.jsx("h2",{className:"ss-title",children:"Usage"}),i&&s.jsxs("div",{className:"ss-loading",children:[s.jsx("span",{className:"ss-spinner"})," Loading..."]}),u&&s.jsx("div",{className:"ss-global-error",children:u}),!i&&d.length===0&&s.jsx("p",{className:"ss-empty",children:"No usage data."}),!i&&d.map(n=>{const m=r==null?void 0:r[n.metric],p=m?Math.min(100,n.total/m*100):null;return s.jsxs("div",{className:"ss-usage-item",children:[s.jsxs("div",{className:"ss-usage-header",children:[s.jsx("span",{className:"ss-usage-metric",children:n.metric}),s.jsxs("span",{className:"ss-usage-value",children:[n.total.toLocaleString(),m?` / ${m.toLocaleString()}`:""]})]}),p!==null&&s.jsx("div",{className:"ss-progress-bar",children:s.jsx("div",{className:`ss-progress-fill ${p>90?"ss-progress-danger":""}`,style:{width:`${p}%`}})})]},n.metric)})]})})}function ys({customerId:e,portalToken:r,limits:a,onChangePlan:c,onCancel:o,appearance:d}){const{appearance:i}=w(),u=d??i,[l,n]=t.useState("subscription"),m=[{id:"subscription",label:"Subscription"},{id:"invoices",label:"Invoices"},{id:"usage",label:"Usage"}];return s.jsx(P,{appearance:u,children:s.jsxs("div",{className:"ss-card ss-card-wide",children:[s.jsx("h2",{className:"ss-title",children:"Billing"}),s.jsx("div",{className:"ss-tab-group",children:m.map(p=>s.jsx("button",{type:"button",className:`ss-tab ${l===p.id?"ss-tab-active":""}`,onClick:()=>n(p.id),children:p.label},p.id))}),s.jsxs("div",{className:"ss-tab-content",children:[l==="subscription"&&s.jsx(V,{customerId:e,portalToken:r,onChangePlan:c,onCancel:o}),l==="invoices"&&s.jsx(J,{customerId:e,portalToken:r}),l==="usage"&&s.jsx(Y,{customerId:e,portalToken:r,limits:a})]})]})})}function js({customerId:e,portalToken:r,onApplied:a,appearance:c}){const{appearance:o}=w(),d=B(r),i=c??o,[u,l]=t.useState(""),[n,m]=t.useState(!1),[p,x]=t.useState(null),[y,h]=t.useState(null),g=t.useCallback(async f=>{if(f.preventDefault(),!!u.trim()){m(!0),x(null),h(null);try{const b=await d.applyCoupon(e,u.trim());h(`Coupon applied! ${b.discountType==="percent"?`${b.amount}% off`:`$${(b.amount/100).toFixed(2)} off`}`),l(""),a==null||a(b)}catch(b){x(b instanceof Error?b.message:"Invalid coupon code")}finally{m(!1)}}},[d,e,u,a]);return s.jsx(P,{appearance:i,children:s.jsxs("div",{className:"ss-card",children:[s.jsx("h2",{className:"ss-title",children:"Apply coupon"}),p&&s.jsx("div",{className:"ss-global-error",children:p}),y&&s.jsx("div",{className:"ss-success-msg",children:y}),s.jsxs("form",{onSubmit:g,children:[s.jsxs("div",{className:"ss-field",children:[s.jsx("label",{className:"ss-label",htmlFor:"ss-coupon-code",children:"Coupon code"}),s.jsx("input",{id:"ss-coupon-code",className:"ss-input",type:"text",placeholder:"Enter coupon code",value:u,onChange:f=>l(f.target.value),required:!0})]}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:n||!u.trim(),children:[n&&s.jsx("span",{className:"ss-spinner"}),"Apply"]})]})]})})}function vs(){const{client:e}=w();return{report:e.report}}function U(){const{client:e}=w(),[r,a]=t.useState(null),[c,o]=t.useState(!1),[d,i]=t.useState(null),u=t.useCallback(async l=>{o(!0),i(null);try{const n=await e.report.executeQuery(l);return a(n),n}catch(n){return i(n instanceof Error?n.message:"Query failed"),null}finally{o(!1)}},[e]);return{result:r,execute:u,isLoading:c,error:d}}function K(e){const{client:r}=w(),[a,c]=t.useState(null),[o,d]=t.useState(!0),[i,u]=t.useState(null),l=t.useCallback(async()=>{d(!0),u(null);try{const n=await r.report.listQueries(e);c(n)}catch(n){u(n instanceof Error?n.message:"Failed to load queries")}finally{d(!1)}},[r,e]);return t.useEffect(()=>{l()},[l]),{queries:(a==null?void 0:a.data)??[],meta:a==null?void 0:a.meta,isLoading:o,error:i,refresh:l}}function Ns(e){const{client:r}=w(),[a,c]=t.useState(null),[o,d]=t.useState(!0),[i,u]=t.useState(null),l=t.useCallback(async()=>{d(!0),u(null);try{const n=await r.report.getDashboard(e);c(n)}catch(n){u(n instanceof Error?n.message:"Failed to load dashboard")}finally{d(!1)}},[r,e]);return t.useEffect(()=>{l()},[l]),{dashboard:a,isLoading:o,error:i,refresh:l}}function ws(e,r,a="https://api.saas-support.com/v1"){const c=t.useMemo(()=>{const p=new L.Transport(a,{type:"embedToken",token:e});return new L.ReportClient(p)},[e,a]),[o,d]=t.useState(null),[i,u]=t.useState(!0),[l,n]=t.useState(null),m=t.useCallback(async()=>{u(!0),n(null);try{const p=await c.getDashboard(r);d(p)}catch(p){n(p instanceof Error?p.message:"Failed to load dashboard")}finally{u(!1)}},[c,r]);return t.useEffect(()=>{m()},[m]),{dashboard:o,reportClient:c,isLoading:i,error:l,refresh:m}}function Ss({onResult:e,mode:r="both",placeholder:a,appearance:c}){const{appearance:o}=w(),{execute:d,isLoading:i,error:u}=U(),l=c??o,[n,m]=t.useState(""),[p,x]=t.useState(r==="sql"?"sql":"nl"),y=t.useCallback(async h=>{if(h.preventDefault(),!n.trim())return;const f=await d(p==="sql"?{sql:n}:{naturalLanguage:n});f&&(e==null||e(f))},[n,p,d,e]);return s.jsx(P,{appearance:l,children:s.jsxs("div",{className:"ss-card ss-card-wide",children:[r==="both"&&s.jsxs("div",{className:"ss-tab-group ss-tab-group-sm",children:[s.jsx("button",{type:"button",className:`ss-tab ${p==="nl"?"ss-tab-active":""}`,onClick:()=>x("nl"),children:"Natural Language"}),s.jsx("button",{type:"button",className:`ss-tab ${p==="sql"?"ss-tab-active":""}`,onClick:()=>x("sql"),children:"SQL"})]}),u&&s.jsx("div",{className:"ss-global-error",children:u}),s.jsxs("form",{onSubmit:y,children:[s.jsx("div",{className:"ss-field",children:s.jsx("textarea",{className:"ss-input ss-query-textarea",placeholder:a??(p==="sql"?"SELECT ...":"Ask a question about your data..."),value:n,onChange:h=>m(h.target.value),rows:3})}),s.jsxs("button",{type:"submit",className:"ss-btn ss-btn-primary",disabled:i||!n.trim(),children:[i&&s.jsx("span",{className:"ss-spinner"}),"Run query"]})]})]})})}function X({columns:e,rows:r,sortable:a=!0,maxRows:c,appearance:o}){const{appearance:d}=w(),i=o??d,[u,l]=t.useState(null),[n,m]=t.useState("asc"),p=t.useMemo(()=>u?[...r].sort((g,f)=>{const b=g[u],j=f[u];if(b==null&&j==null)return 0;if(b==null)return 1;if(j==null)return-1;if(typeof b=="number"&&typeof j=="number")return n==="asc"?b-j:j-b;const v=String(b),N=String(j);return n==="asc"?v.localeCompare(N):N.localeCompare(v)}):r,[r,u,n]),x=c?p.slice(0,c):p,y=g=>{a&&(u===g?m(f=>f==="asc"?"desc":"asc"):(l(g),m("asc")))};function h(g){return g==null?"":typeof g=="object"?JSON.stringify(g):String(g)}return s.jsx(P,{appearance:i,children:s.jsxs("div",{className:"ss-table-container",children:[s.jsxs("table",{className:"ss-table",children:[s.jsx("thead",{children:s.jsx("tr",{children:e.map(g=>s.jsxs("th",{className:`ss-th ${a?"ss-th-sortable":""} ${u===g?n==="asc"?"ss-sorted-asc":"ss-sorted-desc":""}`,onClick:()=>y(g),children:[g,u===g&&s.jsx("span",{className:"ss-sort-indicator",children:n==="asc"?" ▲":" ▼"})]},g))})}),s.jsx("tbody",{children:x.map((g,f)=>s.jsx("tr",{className:"ss-tr",children:e.map(b=>s.jsx("td",{className:"ss-td",children:h(g[b])},b))},f))})]}),c&&r.length>c&&s.jsxs("div",{className:"ss-table-footer",children:["Showing ",c," of ",r.length," rows"]})]})})}function ks(e,r="#6366f1"){const a=parseInt(r.replace("#","").slice(0,2),16),c=[];for(let o=0;o<e;o++){const d=(a+o*Math.floor(360/Math.max(e,1)))%360;c.push(`hsl(${d}, 65%, 55%)`)}return c}function Cs({labels:e,values:r,w:a,h:c,colors:o}){const d=Math.max(...r,1),i=40,u=a-i*2,l=c-i*2,n=Math.max(1,u/e.length-4);return s.jsxs("g",{children:[s.jsx("line",{x1:i,y1:c-i,x2:a-i,y2:c-i,stroke:"#ccc",strokeWidth:1}),r.map((m,p)=>{const x=m/d*l,y=i+u/e.length*p+2,h=c-i-x;return s.jsxs("g",{children:[s.jsx("rect",{x:y,y:h,width:n,height:x,fill:o[p%o.length],rx:2}),s.jsx("text",{x:y+n/2,y:c-i+14,textAnchor:"middle",fontSize:10,fill:"#666",children:e[p].length>8?e[p].slice(0,8)+"...":e[p]})]},p)})]})}function $s({labels:e,values:r,w:a,h:c,colors:o}){const d=Math.max(...r,1),i=40,u=a-i*2,l=c-i*2,n=e.length>1?u/(e.length-1):0,m=r.map((p,x)=>{const y=i+n*x,h=c-i-p/d*l;return`${y},${h}`});return s.jsxs("g",{children:[[.25,.5,.75,1].map(p=>{const x=c-i-p*l;return s.jsx("line",{x1:i,y1:x,x2:a-i,y2:x,stroke:"#eee",strokeWidth:1},p)}),s.jsx("polyline",{points:m.join(" "),fill:"none",stroke:o[0],strokeWidth:2}),r.map((p,x)=>{const y=i+n*x,h=c-i-p/d*l;return s.jsx("circle",{cx:y,cy:h,r:4,fill:o[0]},x)})]})}function Es({labels:e,values:r,w:a,h:c,colors:o}){const d=r.reduce((x,y)=>x+y,0)||1,i=a/2,u=c/2-20,l=Math.min(a,c)/2-40,n=2*Math.PI*l;let m=0;const p=r.map((x,y)=>{const h=x/d,g=h*n,f={dash:g,offset:m,color:o[y%o.length],label:e[y],pct:h};return m+=g,f});return s.jsxs("g",{children:[p.map((x,y)=>s.jsx("circle",{cx:i,cy:u,r:l,fill:"none",stroke:x.color,strokeWidth:l*.6,strokeDasharray:`${x.dash} ${n-x.dash}`,strokeDashoffset:-x.offset,transform:`rotate(-90 ${i} ${u})`},y)),s.jsx("g",{transform:`translate(${i-e.length*30}, ${c-20})`,children:p.slice(0,6).map((x,y)=>s.jsxs("g",{transform:`translate(${y*60}, 0)`,children:[s.jsx("rect",{width:10,height:10,fill:x.color,rx:2}),s.jsx("text",{x:14,y:9,fontSize:9,fill:"#666",children:x.label.length>6?x.label.slice(0,6)+"..":x.label})]},y))})]})}function Z({type:e,data:r,title:a,width:c=400,height:o=300,appearance:d}){const{appearance:i}=w(),u=d??i,l=t.useMemo(()=>ks(r.labels.length),[r.labels.length]);return s.jsx(P,{appearance:u,children:s.jsxs("div",{className:"ss-chart-container",children:[a&&s.jsx("h3",{className:"ss-chart-title",children:a}),s.jsxs("svg",{viewBox:`0 0 ${c} ${o}`,width:"100%",style:{maxWidth:c},children:[e==="bar"&&s.jsx(Cs,{labels:r.labels,values:r.values,w:c,h:o,colors:l}),e==="line"&&s.jsx($s,{labels:r.labels,values:r.values,w:c,h:o,colors:l}),e==="pie"&&s.jsx(Es,{labels:r.labels,values:r.values,w:c,h:o,colors:l})]})]})})}function Ps({dashboardId:e,embedToken:r,baseUrl:a,refreshInterval:c,appearance:o}){const d=w(),i=o??(d==null?void 0:d.appearance),u=t.useMemo(()=>{if(r){const b=a??"https://api.saas-support.com/v1",j=new L.Transport(b,{type:"embedToken",token:r});return new L.ReportClient(j)}return d.client.report},[r,a,d]),[l,n]=t.useState([]),[m,p]=t.useState({}),[x,y]=t.useState(!0),[h,g]=t.useState(null),f=t.useCallback(async()=>{y(!0),g(null);try{const b=await u.getDashboard(e),j=JSON.parse(b.layoutJson||"[]");n(j);const v={};for(const N of j)try{const k=await u.listQueries({search:N.queryId,perPage:1});if(k.data.length>0&&k.data[0].generatedSql){const T=await u.executeQuery({sql:k.data[0].generatedSql});v[N.queryId]=T}}catch{}p(v)}catch(b){g(b instanceof Error?b.message:"Failed to load dashboard")}finally{y(!1)}},[u,e]);return t.useEffect(()=>{f()},[f]),t.useEffect(()=>{if(!c||c<=0)return;const b=setInterval(f,c*1e3);return()=>clearInterval(b)},[c,f]),s.jsx(P,{appearance:i,children:s.jsxs("div",{className:"ss-dashboard-grid",children:[x&&s.jsxs("div",{className:"ss-loading",children:[s.jsx("span",{className:"ss-spinner"})," Loading dashboard..."]}),h&&s.jsx("div",{className:"ss-global-error",children:h}),!x&&l.map((b,j)=>{const v=m[b.queryId];if(!v)return null;const N=v.columns.length>=2?{labels:v.rows.map(k=>String(k[v.columns[0]]??"")),values:v.rows.map(k=>Number(k[v.columns[1]]??0))}:{labels:[],values:[]};return s.jsxs("div",{className:"ss-widget",children:[b.title&&s.jsx("h4",{className:"ss-widget-header",children:b.title}),b.chartType==="table"?s.jsx(X,{columns:v.columns,rows:v.rows,maxRows:50}):s.jsx(Z,{type:b.chartType||"bar",data:N,width:b.w,height:b.h})]},j)})]})})}function Ts(e){return new Date(e).toLocaleDateString("en-US",{year:"numeric",month:"short",day:"numeric"})}function Is({onSelectQuery:e,onRunQuery:r,appearance:a}){const{appearance:c}=w(),{queries:o,isLoading:d,error:i}=K(),{execute:u,isLoading:l}=U(),n=a??c,m=t.useCallback(async p=>{if(!p.generatedSql)return;const x=await u({sql:p.generatedSql});x&&(r==null||r(x))},[u,r]);return s.jsx(P,{appearance:n,children:s.jsxs("div",{className:"ss-card ss-card-wide",children:[s.jsx("h2",{className:"ss-title",children:"Saved Queries"}),d&&s.jsxs("div",{className:"ss-loading",children:[s.jsx("span",{className:"ss-spinner"})," Loading..."]}),i&&s.jsx("div",{className:"ss-global-error",children:i}),!d&&o.length===0&&s.jsx("p",{className:"ss-empty",children:"No saved queries."}),!d&&o.map(p=>s.jsxs("div",{className:"ss-saved-query-card",onClick:()=>e==null?void 0:e(p),children:[s.jsxs("div",{className:"ss-saved-query-header",children:[s.jsx("span",{className:"ss-saved-query-name",children:p.name}),p.chartType&&s.jsx("span",{className:"ss-badge",children:p.chartType})]}),p.naturalLanguage&&s.jsx("p",{className:"ss-saved-query-desc",children:p.naturalLanguage}),s.jsxs("div",{className:"ss-saved-query-footer",children:[s.jsx("span",{className:"ss-saved-query-date",children:Ts(p.createdAt)}),s.jsx("button",{type:"button",className:"ss-btn ss-btn-sm ss-btn-primary",disabled:l||!p.generatedSql,onClick:x=>{x.stopPropagation(),m(p)},children:l?s.jsx("span",{className:"ss-spinner"}):"Run"})]})]},p.id))]})})}function Ls({embedToken:e,dashboardId:r,baseUrl:a="https://api.saas-support.com/v1",refreshInterval:c,appearance:o}){const d=t.useMemo(()=>{const g=new L.Transport(a,{type:"embedToken",token:e});return new L.ReportClient(g)},[e,a]),[i,u]=t.useState([]),[l,n]=t.useState(!0),[m,p]=t.useState(null),x=t.useMemo(()=>z(o),[o]),y=t.useMemo(()=>q(x),[x]),h=t.useCallback(async()=>{n(!0),p(null);try{const g=await d.getDashboard(r),f=JSON.parse(g.layoutJson||"[]"),b=await Promise.all(f.map(async j=>{try{const v=await d.listQueries({search:j.queryId,perPage:1});if(v.data.length>0&&v.data[0].generatedSql){const N=await d.executeQuery({sql:v.data[0].generatedSql});return{...j,result:N}}}catch{}return j}));u(b)}catch(g){p(g instanceof Error?g.message:"Failed to load dashboard")}finally{n(!1)}},[d,r]);return t.useEffect(()=>{h()},[h]),t.useEffect(()=>{if(!c||c<=0)return;const g=setInterval(h,c*1e3);return()=>clearInterval(g)},[c,h]),s.jsx("div",{ref:g=>{if(!g||g.shadowRoot)return;const f=g.attachShadow({mode:"open"}),b=document.createElement("style");b.textContent=y,f.appendChild(b);const j=document.createElement("div");f.appendChild(j)},style:{display:"contents"},children:s.jsxs("div",{className:"ss-dashboard-grid",children:[l&&s.jsx("div",{className:"ss-loading",children:"Loading dashboard..."}),m&&s.jsx("div",{className:"ss-global-error",children:m}),!l&&i.map((g,f)=>{if(!g.result)return null;const{columns:b,rows:j}=g.result;return s.jsxs("div",{className:"ss-widget",children:[g.title&&s.jsx("h4",{className:"ss-widget-header",children:g.title}),s.jsxs("table",{className:"ss-table",children:[s.jsx("thead",{children:s.jsx("tr",{children:b.map(v=>s.jsx("th",{className:"ss-th",children:v},v))})}),s.jsx("tbody",{children:j.slice(0,50).map((v,N)=>s.jsx("tr",{className:"ss-tr",children:b.map(k=>s.jsx("td",{className:"ss-td",children:String(v[k]??"")},k))},N))})]})]},f)})]})})}exports.SaaSError=L.SaaSError;exports.SaaSSupport=L.SaaSSupport;exports.isMfaRequired=L.isMfaRequired;exports.Chart=Z;exports.CouponInput=js;exports.DashboardView=Ps;exports.DataTable=X;exports.InvoiceHistory=J;exports.OrgSwitcher=us;exports.PaymentPortal=ys;exports.PricingTable=ms;exports.QueryInput=Ss;exports.ReportEmbed=Ls;exports.SaaSContext=D;exports.SaaSProvider=es;exports.SavedQueryList=Is;exports.SignIn=os;exports.SignUp=ls;exports.SubscriptionStatus=V;exports.UsageDisplay=Y;exports.UserButton=is;exports.UserProfile=ds;exports.useAuth=M;exports.useBilling=xs;exports.useDashboard=Ns;exports.useEmbedDashboard=ws;exports.useInvoices=Q;exports.useOrg=O;exports.useProfile=A;exports.useQuery=U;exports.useReport=vs;exports.useSaaSContext=w;exports.useSavedQueries=K;exports.useSignIn=F;exports.useSignUp=R;exports.useSubscription=_;exports.useUsage=G;exports.useUser=ns;
|
package/dist/react.js
CHANGED
|
@@ -1202,7 +1202,7 @@ function As({ appearance: s, signInUrl: a, onSignIn: r }) {
|
|
|
1202
1202
|
}
|
|
1203
1203
|
function Ds({ appearance: s }) {
|
|
1204
1204
|
const { appearance: a } = k(), { user: r, signOut: c } = j(), t = s ?? a, [d, l] = g(!1), [u, o] = g(!1), n = R(null), m = S((h) => {
|
|
1205
|
-
n.current && !
|
|
1205
|
+
n.current && !h.composedPath().includes(n.current) && l(!1);
|
|
1206
1206
|
}, []);
|
|
1207
1207
|
if (L(() => {
|
|
1208
1208
|
if (d) {
|
|
@@ -1458,7 +1458,7 @@ function Fs({ appearance: s }) {
|
|
|
1458
1458
|
}
|
|
1459
1459
|
function Ms({ appearance: s, onOrgChange: a }) {
|
|
1460
1460
|
const { appearance: r } = k(), { orgs: c, selectedOrg: t, selectOrg: d, createOrg: l, isLoading: u } = os(), o = s ?? r, [n, m] = g(!1), [p, h] = g(!1), [v, y] = g(""), [b, x] = g(""), [f, N] = g(null), [w, $] = g(!1), E = R(null), z = S((P) => {
|
|
1461
|
-
E.current && !
|
|
1461
|
+
E.current && !P.composedPath().includes(E.current) && (m(!1), h(!1));
|
|
1462
1462
|
}, []);
|
|
1463
1463
|
L(() => {
|
|
1464
1464
|
if (n) {
|