@enterprisestandard/react 0.0.8-beta.20260219.2 → 0.0.8-beta.20260224.2

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.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export*from"@enterprisestandard/core";import{createContext as Y,useCallback as S,useContext as L,useEffect as T,useState as J}from"react";import{jsx as D}from"react/jsx-runtime";var $=Y(void 0),V=/^[a-zA-Z0-9._-]{1,1024}$/;function X(n){if(!n.trim())throw Error("SSOProvider storageKey must be a non-empty string.");if(!V.test(n))throw Error("SSOProvider storageKey must be 1–1024 characters and contain only letters, numbers, dots, underscores, and hyphens.")}var k=(n)=>{if(n===void 0||n==="")return"es.sso.user";return X(n),n};function W(n,i){if(typeof window>"u")return null;try{let p=(n==="local"?localStorage:sessionStorage).getItem(i);if(!p)return null;let c=JSON.parse(p);if(c.sso?.expires)c.sso.expires=new Date(c.sso.expires);return c}catch(t){return console.error("Error loading user from storage:",t),null}}async function q(n){try{let i=await fetch(n);if(i.status===401)return null;if(!i.ok)throw Error(`Failed to fetch user: ${i.status} ${i.statusText}`);let t=await i.json();if(t.sso?.expires&&typeof t.sso.expires==="string")t.sso.expires=new Date(t.sso.expires);return t}catch(i){return console.error("Error fetching user from URL:",i),null}}function _({storageKey:n,userUrl:i,storage:t="memory",disableListener:p=!1,children:c}){let[o,m]=J(null),[P,h]=J(!!i),s=k(n),b=S(()=>{if(t==="memory")return null;return W(t,s)},[t,s]),f=S((d)=>{if(t==="memory"||typeof window>"u")return;try{let u=t==="local"?localStorage:sessionStorage;if(d===null)u.removeItem(s);else u.setItem(s,JSON.stringify(d))}catch(u){console.error("Error saving user to storage:",u)}},[t,s]),l=S((d)=>{m(d),f(d)},[f]),y=S(async()=>{if(!i)return;h(!0);try{let d=await q(i);m(d),f(d)}finally{h(!1)}},[i,f]);T(()=>{let d=b();if(d)m(d);if(i)y();else h(!1)},[b,i,y]),T(()=>{if(p||t==="memory")return;let d=(O)=>{if(O.key!==s)return;if(O.newValue===null)m(null);else try{let v=JSON.parse(O.newValue);if(v.sso?.expires)v.sso.expires=new Date(v.sso.expires);m(v)}catch(v){console.error("Error parsing user from storage event:",v)}},u=()=>{m(null)};return window.addEventListener("storage",d),window.addEventListener("es-sso-logout",u),()=>{window.removeEventListener("storage",d),window.removeEventListener("es-sso-logout",u)}},[p,t,s]);let R={user:o,setUser:l,isLoading:P};return D($.Provider,{value:R,children:c})}function I(){let n=L($);if(n===void 0)throw Error("useUser must be used within a SSOProvider");return n}async function E(n,i){let t=k(i),p=W("local",t);if(p)return p;let c=W("session",t);if(c)return c;if(n){let o=await q(n);if(o)return o}return}async function a(n){try{let i=await fetch(n,{headers:{Accept:"application/json"}});if(!i.ok)return{success:!1,error:`HTTP ${i.status}`};let t=await i.json();if(!t.success)return{success:!1,error:t.message||"Logout failed"};if(typeof window<"u")localStorage.removeItem("es.sso.user"),sessionStorage.removeItem("es.sso.user"),window.dispatchEvent(new CustomEvent("es-sso-logout"));return{success:!0}}catch(i){return{success:!1,error:i instanceof Error?i.message:"Network error"}}}import{jsx as B,Fragment as z}from"react/jsx-runtime";function F({complete:n=!1,children:i}){let{isLoading:t}=I();if(t&&!n)return B(z,{children:i});return B(z,{})}import{jsx as H,Fragment as C}from"react/jsx-runtime";function g({children:n}){let{user:i}=I();if(i)return H(C,{children:n});return H(C,{})}import{jsx as Q,Fragment as M}from"react/jsx-runtime";function j({children:n}){let{user:i,isLoading:t}=I();if(i||t)return Q(M,{});return Q(M,{children:n})}import{jsx as N,jsxs as w,Fragment as r}from"react/jsx-runtime";function x(n){if(!n)return"Unknown user";return n.name??n.email??n.id??"Unknown user"}function e(n){return n.tenantName??n.companyName??n.tenantId??n.clientId}function K({activeSession:n,sessions:i,onSwitch:t,availableTenants:p=[],onLogin:c}){return w("details",{className:"relative inline-block text-left",children:[w("summary",{className:"cursor-pointer select-none rounded-md border px-3 py-2 text-sm",children:[x(n?.user)," · ",n?e(n):"No active session"]}),w("div",{className:"absolute left-0 mt-2 w-64 rounded-md border bg-white p-3 text-sm shadow",children:[N("div",{className:"mb-2 text-gray-500 text-xs uppercase",children:"Active"}),N("div",{className:"mb-4",children:n?w("div",{className:"rounded-md border px-2 py-1",children:[e(n)," · ",x(n.user)]}):N("div",{className:"text-gray-500",children:"No active session"})}),N("div",{className:"mb-2 text-gray-500 text-xs uppercase",children:"Sessions"}),w("div",{className:"space-y-2",children:[i.length===0&&N("div",{className:"text-gray-500",children:"No sessions"}),i.map((o)=>w("button",{type:"button",className:"w-full rounded-md border px-2 py-1 text-left hover:bg-gray-50",onClick:()=>t(o.clientId),children:[e(o)," · ",x(o.user)]},o.clientId))]}),p.length>0&&w(r,{children:[N("div",{className:"mt-4 mb-2 text-gray-500 text-xs uppercase",children:"Login to another tenant"}),N("div",{className:"space-y-2",children:p.map((o)=>N("button",{type:"button",className:"w-full rounded-md border px-2 py-1 text-left hover:bg-gray-50",onClick:()=>c?.(o.clientId),children:o.tenantName??o.companyName??o.tenantId??o.clientId},o.clientId))})]})]})]})}import{useCallback as Z,useEffect as U,useState as A}from"react";function G(n={}){let i=n.sessionsUrl??"/api/sessions",t=n.switchUrl??"/api/sessions/switch",[p,c]=A([]),[o,m]=A(null),[P,h]=A(!0),s=Z(async()=>{h(!0);try{let f=await fetch(i);if(!f.ok){c([]),m(null);return}let l=await f.json();c(l.sessions??[]);let y=l.sessions?.find((R)=>R.clientId===l.activeSession)??null;m(y)}finally{h(!1)}},[i]),b=Z(async(f)=>{let l=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({clientId:f})});if(l.redirected&&typeof window<"u"){window.location.assign(l.url);return}if(!l.ok){if(typeof window<"u")window.location.assign(`/api/auth/login?clientId=${f}`);return}await s()},[s,t]);return U(()=>{s()},[s]),{activeSession:o,sessions:p,isLoading:P,refresh:s,switchSession:b}}function nn(n){let{activeSession:i,isLoading:t}=G({sessionsUrl:n});return{user:i?.user??null,isLoading:t}}export{I as useUser,G as useSessions,nn as useActiveUser,a as logout,E as getUser,K as TenantSwitcher,j as SignedOut,g as SignedIn,F as SignInLoading,_ as SSOProvider};
1
+ export*from"@enterprisestandard/core";import{createContext as C,useCallback as S,useContext as H,useEffect as T,useState as J}from"react";import{jsx as a}from"react/jsx-runtime";var $=C(void 0),M=/^[a-zA-Z0-9._-]{1,1024}$/;function Q(n){if(!n.trim())throw Error("SSOProvider storageKey must be a non-empty string.");if(!M.test(n))throw Error("SSOProvider storageKey must be 1–1024 characters and contain only letters, numbers, dots, underscores, and hyphens.")}var k=(n)=>{if(n===void 0||n==="")return"es.sso.user";return Q(n),n};function e(n,i){if(typeof window>"u")return null;try{let p=(n==="local"?localStorage:sessionStorage).getItem(i);if(!p)return null;let c=JSON.parse(p);if(c.sso?.expires)c.sso.expires=new Date(c.sso.expires);return c}catch(t){return console.error("Error loading user from storage:",t),null}}async function q(n){try{let i=await fetch(n);if(i.status===401)return null;if(!i.ok)throw Error(`Failed to fetch user: ${i.status} ${i.statusText}`);let t=await i.json();if(t.sso?.expires&&typeof t.sso.expires==="string")t.sso.expires=new Date(t.sso.expires);return t}catch(i){return console.error("Error fetching user from URL:",i),null}}function Z({storageKey:n,userUrl:i,storage:t="memory",disableListener:p=!1,children:c}){let[o,l]=J(null),[P,h]=J(!!i),s=k(n),b=S(()=>{if(t==="memory")return null;return e(t,s)},[t,s]),m=S((d)=>{if(t==="memory"||typeof window>"u")return;try{let N=t==="local"?localStorage:sessionStorage;if(d===null)N.removeItem(s);else N.setItem(s,JSON.stringify(d))}catch(N){console.error("Error saving user to storage:",N)}},[t,s]),f=S((d)=>{l(d),m(d)},[m]),y=S(async()=>{if(!i)return;h(!0);try{let d=await q(i);l(d),m(d)}finally{h(!1)}},[i,m]);T(()=>{let d=b();if(d)l(d);if(i)y();else h(!1)},[b,i,y]),T(()=>{if(p||t==="memory")return;let d=(x)=>{if(x.key!==s)return;if(x.newValue===null)l(null);else try{let v=JSON.parse(x.newValue);if(v.sso?.expires)v.sso.expires=new Date(v.sso.expires);l(v)}catch(v){console.error("Error parsing user from storage event:",v)}},N=()=>{l(null)};return window.addEventListener("storage",d),window.addEventListener("es-sso-logout",N),()=>{window.removeEventListener("storage",d),window.removeEventListener("es-sso-logout",N)}},[p,t,s]);let R={user:o,setUser:f,isLoading:P};return a($.Provider,{value:R,children:c})}function I(){let n=H($);if(n===void 0)throw Error("useUser must be used within a SSOProvider");return n}async function G(n,i){let t=k(i),p=e("local",t);if(p)return p;let c=e("session",t);if(c)return c;if(n){let o=await q(n);if(o)return o}return}async function Y(n){try{let i=await fetch(n,{headers:{Accept:"application/json"}});if(!i.ok)return{success:!1,error:`HTTP ${i.status}`};let t=await i.json();if(!t.success)return{success:!1,error:t.message||"Logout failed"};if(typeof window<"u")localStorage.removeItem("es.sso.user"),sessionStorage.removeItem("es.sso.user"),window.dispatchEvent(new CustomEvent("es-sso-logout"));return{success:!0}}catch(i){return{success:!1,error:i instanceof Error?i.message:"Network error"}}}import{jsx as X,Fragment as V}from"react/jsx-runtime";function L({complete:n=!1,children:i}){let{isLoading:t}=I();if(t&&!n)return X(V,{children:i});return null}import{jsx as D,Fragment as E}from"react/jsx-runtime";function _({children:n}){let{user:i}=I();if(i)return D(E,{children:n});return null}import{jsx as j,Fragment as g}from"react/jsx-runtime";function F({children:n}){let{user:i,isLoading:t}=I();if(i||t)return null;return j(g,{children:n})}import{jsx as u,jsxs as w,Fragment as r}from"react/jsx-runtime";function O(n){if(!n)return"Unknown user";return n.name??n.email??n.id??"Unknown user"}function W(n){return n.tenantName??n.companyName??n.tenantId??n.clientId}function K({activeSession:n,sessions:i,onSwitch:t,availableTenants:p=[],onLogin:c}){return w("details",{className:"relative inline-block text-left",children:[w("summary",{className:"cursor-pointer select-none rounded-md border px-3 py-2 text-sm",children:[O(n?.user)," · ",n?W(n):"No active session"]}),w("div",{className:"absolute left-0 mt-2 w-64 rounded-md border bg-white p-3 text-sm shadow",children:[u("div",{className:"mb-2 text-gray-500 text-xs uppercase",children:"Active"}),u("div",{className:"mb-4",children:n?w("div",{className:"rounded-md border px-2 py-1",children:[W(n)," · ",O(n.user)]}):u("div",{className:"text-gray-500",children:"No active session"})}),u("div",{className:"mb-2 text-gray-500 text-xs uppercase",children:"Sessions"}),w("div",{className:"space-y-2",children:[i.length===0&&u("div",{className:"text-gray-500",children:"No sessions"}),i.map((o)=>w("button",{type:"button",className:"w-full rounded-md border px-2 py-1 text-left hover:bg-gray-50",onClick:()=>t(o.clientId),children:[W(o)," · ",O(o.user)]},o.clientId))]}),p.length>0&&w(r,{children:[u("div",{className:"mt-4 mb-2 text-gray-500 text-xs uppercase",children:"Login to another tenant"}),u("div",{className:"space-y-2",children:p.map((o)=>u("button",{type:"button",className:"w-full rounded-md border px-2 py-1 text-left hover:bg-gray-50",onClick:()=>c?.(o.clientId),children:o.tenantName??o.companyName??o.tenantId??o.clientId},o.clientId))})]})]})]})}import{useCallback as z,useEffect as U,useState as A}from"react";function B(n={}){let i=n.sessionsUrl??"/api/sessions",t=n.switchUrl??"/api/sessions/switch",[p,c]=A([]),[o,l]=A(null),[P,h]=A(!0),s=z(async()=>{h(!0);try{let m=await fetch(i);if(!m.ok){c([]),l(null);return}let f=await m.json();c(f.sessions??[]);let y=f.sessions?.find((R)=>R.clientId===f.activeSession)??null;l(y)}finally{h(!1)}},[i]),b=z(async(m)=>{let f=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({clientId:m})});if(f.redirected&&typeof window<"u"){window.location.assign(f.url);return}if(!f.ok){if(typeof window<"u")window.location.assign(`/api/auth/login?clientId=${m}`);return}await s()},[s,t]);return U(()=>{s()},[s]),{activeSession:o,sessions:p,isLoading:P,refresh:s,switchSession:b}}function nn(n){let{activeSession:i,isLoading:t}=B({sessionsUrl:n});return{user:i?.user??null,isLoading:t}}export{I as useUser,B as useSessions,nn as useActiveUser,Y as logout,G as getUser,K as TenantSwitcher,F as SignedOut,_ as SignedIn,L as SignInLoading,Z as SSOProvider};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enterprisestandard/react",
3
- "version": "0.0.8-beta.20260219.2",
3
+ "version": "0.0.8-beta.20260224.2",
4
4
  "description": "Enterprise Standard React Components",
5
5
  "private": false,
6
6
  "author": "enterprisestandard",
@@ -22,7 +22,7 @@
22
22
  "./package.json": "./package.json"
23
23
  },
24
24
  "dependencies": {
25
- "@enterprisestandard/core": "0.0.8-beta.20260219.2"
25
+ "@enterprisestandard/core": "0.0.8-beta.20260224.2"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "react": "^18.0.0 || ^19.0.0",