@vertesia/ui 0.79.1 → 0.79.3

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 (67) hide show
  1. package/lib/esm/core/components/SelectBox.js +1 -1
  2. package/lib/esm/core/components/SelectBox.js.map +1 -1
  3. package/lib/esm/core/hooks/CompositeState.js +1 -139
  4. package/lib/esm/core/hooks/CompositeState.js.map +1 -1
  5. package/lib/esm/env/index.js +2 -2
  6. package/lib/esm/env/index.js.map +1 -1
  7. package/lib/esm/features/agent/PayloadBuilder.js +55 -80
  8. package/lib/esm/features/agent/PayloadBuilder.js.map +1 -1
  9. package/lib/esm/features/agent/chat/ModernAgentOutput/AllMessagesMixed.js +1 -1
  10. package/lib/esm/features/agent/chat/ModernAgentOutput/AllMessagesMixed.js.map +1 -1
  11. package/lib/esm/features/agent/chat/ModernAgentOutput/Header.js +1 -1
  12. package/lib/esm/features/agent/chat/ModernAgentOutput/Header.js.map +1 -1
  13. package/lib/esm/features/agent/chat/ModernAgentOutput/WorkstreamTabs.js +2 -2
  14. package/lib/esm/features/agent/chat/ModernAgentOutput/WorkstreamTabs.js.map +1 -1
  15. package/lib/esm/features/store/collections/SelectCollection.js +104 -39
  16. package/lib/esm/features/store/collections/SelectCollection.js.map +1 -1
  17. package/lib/esm/features/store/objects/DocumentSearchResults.js +2 -10
  18. package/lib/esm/features/store/objects/DocumentSearchResults.js.map +1 -1
  19. package/lib/esm/features/store/objects/components/useDownloadObject.js +2 -2
  20. package/lib/esm/features/store/objects/components/useDownloadObject.js.map +1 -1
  21. package/lib/esm/features/store/objects/upload/useSmartFileUploadProcessing.js +10 -9
  22. package/lib/esm/features/store/objects/upload/useSmartFileUploadProcessing.js.map +1 -1
  23. package/lib/esm/features/user/UserInfo.js +0 -2
  24. package/lib/esm/features/user/UserInfo.js.map +1 -1
  25. package/lib/esm/session/UserSession.js +0 -1
  26. package/lib/esm/session/UserSession.js.map +1 -1
  27. package/lib/esm/session/UserSessionProvider.js +2 -9
  28. package/lib/esm/session/UserSessionProvider.js.map +1 -1
  29. package/lib/esm/session/auth/composable.js +67 -66
  30. package/lib/esm/session/auth/composable.js.map +1 -1
  31. package/lib/esm/widgets/form/Form.js +1 -1
  32. package/lib/esm/widgets/form/Form.js.map +1 -1
  33. package/lib/esm/widgets/form/ManagedObject.js +0 -4
  34. package/lib/esm/widgets/form/ManagedObject.js.map +1 -1
  35. package/lib/tsconfig.tsbuildinfo +1 -1
  36. package/lib/types/core/hooks/CompositeState.d.ts +6 -115
  37. package/lib/types/core/hooks/CompositeState.d.ts.map +1 -1
  38. package/lib/types/env/index.d.ts +1 -3
  39. package/lib/types/env/index.d.ts.map +1 -1
  40. package/lib/types/features/agent/PayloadBuilder.d.ts +19 -11
  41. package/lib/types/features/agent/PayloadBuilder.d.ts.map +1 -1
  42. package/lib/types/features/store/collections/SelectCollection.d.ts +2 -1
  43. package/lib/types/features/store/collections/SelectCollection.d.ts.map +1 -1
  44. package/lib/types/features/store/objects/DocumentSearchResults.d.ts.map +1 -1
  45. package/lib/types/features/store/objects/components/useDownloadObject.d.ts +1 -1
  46. package/lib/types/features/store/objects/components/useDownloadObject.d.ts.map +1 -1
  47. package/lib/types/features/store/objects/upload/useSmartFileUploadProcessing.d.ts.map +1 -1
  48. package/lib/types/features/user/UserInfo.d.ts.map +1 -1
  49. package/lib/types/session/UserSession.d.ts.map +1 -1
  50. package/lib/types/session/UserSessionProvider.d.ts.map +1 -1
  51. package/lib/types/session/auth/composable.d.ts.map +1 -1
  52. package/lib/types/widgets/form/ManagedObject.d.ts.map +1 -1
  53. package/lib/vertesia-ui-core.js +1 -1
  54. package/lib/vertesia-ui-core.js.map +1 -1
  55. package/lib/vertesia-ui-env.js +1 -1
  56. package/lib/vertesia-ui-env.js.map +1 -1
  57. package/lib/vertesia-ui-features.js +1 -1
  58. package/lib/vertesia-ui-features.js.map +1 -1
  59. package/lib/vertesia-ui-router.js.map +1 -1
  60. package/lib/vertesia-ui-session.js +1 -1
  61. package/lib/vertesia-ui-session.js.map +1 -1
  62. package/lib/vertesia-ui-shell.js.map +1 -1
  63. package/lib/vertesia-ui-widgets.js +1 -1
  64. package/lib/vertesia-ui-widgets.js.map +1 -1
  65. package/package.json +166 -166
  66. package/src/env/index.ts +1 -1
  67. package/src/features/store/objects/upload/useSmartFileUploadProcessing.ts +1 -3
@@ -1 +1 @@
1
- {"version":3,"file":"vertesia-ui-router.js","sources":["esm/router/path.js","esm/router/PathWithParams.js","esm/router/HistoryNavigator.js","esm/router/FixLinks.js","esm/router/PathMatcher.js","esm/router/Router.js","esm/router/Nav.js","esm/router/RouteComponent.js","esm/router/NestedNavigationContext.js","esm/router/Route404.js","esm/router/NestedRouterProvider.js","esm/router/RouterProvider.js"],"sourcesContent":["export function isRootPath(path) {\n return path === '/' || path === '';\n}\nexport function joinPath(path1, path2) {\n if (path1.endsWith('/') && path2.startsWith('/')) {\n path2 = path1 + path2.substring(1);\n }\n else if (path1.endsWith('/')) {\n path2 = path1 + path2;\n }\n else if (path2.startsWith('/')) {\n path2 = path1 + path2;\n }\n else {\n path2 = path1 + '/' + path2;\n }\n return path2;\n}\nexport function getPathSegments(path) {\n if (path === '') {\n return [];\n }\n if (path === '/') {\n return [''];\n }\n let s = 0, e = path.length;\n if (path.startsWith('./')) {\n s = 2;\n }\n else if (path.startsWith('/')) {\n s = 1;\n }\n if (path.endsWith('/')) {\n e = path.length - 1;\n }\n return (s > 0 || e < path.length ? path.substring(s, e) : path).split('/');\n}\nexport function toSegments(path) {\n if (typeof path === 'string') {\n return getPathSegments(path);\n }\n else if (Array.isArray(path)) {\n return path;\n }\n else {\n throw new Error(`Unsupported path object: ${path}`);\n }\n}\n// export class Path {\n// static parse(path: string, abs = false) {\n// if (abs !== undefined) {\n// abs = path.startsWith('/');\n// }\n// return new Path(getPathSegments(path), abs);\n// }\n// constructor(public segments: string[], public isAbsolute = false) {\n// }\n// getParameters() {\n// const out = [];\n// for (const segment of this.segments) {\n// if (segment[0] === ':') {\n// out.push(segment.substring(1));\n// }\n// }\n// if (this.segments[this.segments.length - 1] === '*') {\n// out.push('_');\n// }\n// return out;\n// }\n// resolveParameters(path: string) {\n// const params: PathMatchParams = {};\n// const resolvedSegments = getPathSegments(path);\n// if (resolvedSegments.length < this.segments.length) {\n// return null;\n// }\n// const segments = this.segments;\n// for (let i = 0, l = segments.length; i < l; i++) {\n// const seg = segments[i];\n// if (seg[0] === ':') {\n// params[seg.substring(1)] = resolvedSegments[i];\n// }\n// }\n// if (resolvedSegments.length - this.segments.length) {\n// params._ = resolvedSegments.slice(this.segments.length);\n// }\n// return params;\n// }\n// match(path: string | string[] | Path) {\n// const segments = toSegments(path);\n// if (segments.length < this.segments.length) {\n// return false;\n// }\n// let params: PathMatchParams | undefined;\n// const mySegments = this.segments;\n// for (let i = 0, l = mySegments.length; i < l; i++) {\n// const segment = mySegments[i];\n// if (segment === ':') {\n// if (!params) params = {};\n// params[segment.substring(1)] = segment;\n// } else if (segment !== segments[i]) {\n// if (i === l - 1 && segment === '*') {\n// if (!params) params = {};\n// params._ = segments.slice(i);\n// return params;\n// }\n// return false;\n// }\n// }\n// return params ? params : false;\n// }\n// join(path: Path | string | string[]) {\n// const segments = toSegments(path);\n// return new Path(this.segments.concat(segments), this.isAbsolute);\n// }\n// getRelativePath(path: Path | string | string[], asAbsolute: boolean = false) {\n// const segments = toSegments(path);\n// const extraSegmentsCount = segments.length - this.segments.length;\n// if (extraSegmentsCount <= 0) {\n// return null;\n// }\n// return new Path(segments.slice(this.segments.length), asAbsolute);\n// }\n// prependSegments(segments: string[]) {\n// this.segments = segments.concat(this.segments);\n// }\n// appendSegments(segments: string[]) {\n// this.segments = this.segments.concat(segments);\n// }\n// toAbsolutePath() {\n// return '/' + this.segments.join('/');\n// }\n// toRelativePath() {\n// return this.segments.join('/');\n// }\n// toString() {\n// const path = this.segments.join('/');\n// return this.isAbsolute ? '/' + path : path;\n// }\n// }\n//# sourceMappingURL=path.js.map","export class PathWithParams {\n path;\n params;\n hash;\n constructor(path) {\n let i = path.lastIndexOf('#');\n if (i > -1) {\n this.hash = path.substring(i);\n path = path.substring(0, i);\n }\n else {\n this.hash = '';\n }\n i = path.indexOf('?');\n if (i > -1) {\n this.path = path.substring(0, i);\n this.params = new URLSearchParams(path.substring(i + 1));\n }\n else {\n this.path = path;\n this.params = new URLSearchParams();\n }\n }\n add(params) {\n for (const [key, value] of Object.entries(params)) {\n this.params.set(key, value);\n }\n return this;\n }\n toString() {\n return this.path + '?' + this.params.toString() + this.hash;\n }\n}\n//# sourceMappingURL=PathWithParams.js.map","import { joinPath } from \"./path\";\nimport { PathWithParams } from \"./PathWithParams\";\nconst BASE_PATH = Symbol('BASE_PATH');\nexport { BASE_PATH };\nexport class LocationChangeEvent {\n name;\n type;\n location;\n state;\n _canceled = false;\n constructor(name, type, location, state) {\n this.name = name;\n this.type = type;\n this.location = location;\n this.state = state;\n }\n get isPageLoad() {\n return this.type === 'initial';\n }\n get isBackForward() {\n return this.type === 'popState';\n }\n get isLinkClick() {\n return this.type === 'linkClick';\n }\n get isNavigation() {\n return this.type === 'navigate';\n }\n get isCancelable() {\n return this.name === 'beforeChange';\n }\n cancel() {\n if (this.name === 'afterChange') {\n throw new Error('Cannot cancel afterChange event');\n }\n this._canceled = true;\n }\n}\nexport class BeforeLocationChangeEvent extends LocationChangeEvent {\n constructor(type, location, state) {\n super('beforeChange', type, location, state);\n }\n}\nexport class AfterLocationChangeEvent extends LocationChangeEvent {\n constructor(type, location, state) {\n super('afterChange', type, location, state);\n }\n}\nfunction getElementHrefAsUrl(elem) {\n if (elem && elem.tagName.toLowerCase() === 'a') {\n const href = elem?.href.trim();\n if (typeof href === 'string' && href.length > 0) {\n return new URL(href);\n }\n }\n return null;\n}\nexport class HistoryNavigator {\n // params to preserve in the query string when navigating\n stickyParams;\n _popStateListener;\n _linkNavListener;\n _listeners = [];\n constructor() {\n }\n addListener(listener) {\n this._listeners.push(listener);\n }\n fireLocationChange(event) {\n for (const listener of this._listeners) {\n listener(event);\n }\n }\n /**\n * Should be called when the page is first loaded.\n * It will fire a location change event with type `initial` and the current window.location as the location\n */\n firePageLoad() {\n this.fireLocationChange(new AfterLocationChangeEvent('initial', new URL(window.location.href)));\n }\n addStickyParams(path) {\n if (this.stickyParams) {\n return new PathWithParams(path).add(this.stickyParams).toString();\n }\n return path;\n }\n navigate(to, options = {}) {\n if (options.basePath) {\n let basePath = options.basePath;\n if (!basePath.startsWith('/')) {\n basePath = '/' + basePath;\n }\n to = joinPath(basePath, to);\n }\n to = this.addStickyParams(to);\n this._navigate(new URL(to, window.location.href), 'navigate', options);\n }\n _navigate(to, type, options) {\n const beforeEvent = new BeforeLocationChangeEvent(type, to, options.state);\n this.fireLocationChange(beforeEvent);\n if (beforeEvent._canceled) {\n return;\n }\n // Build navigation chain by preserving previous history\n const currentState = window.history.state;\n const currentTitle = document.title;\n // Create new history chain entry\n const newChainEntry = {\n title: currentTitle,\n href: window.location.pathname + window.location.search + window.location.hash\n };\n // Build the history chain - clear if using replace\n let historyChain = [];\n if (!options.replace && currentState?.historyChain) {\n historyChain = [...currentState.historyChain];\n }\n // Only add to chain if not replacing\n if (!options.replace) {\n historyChain.push(newChainEntry);\n // Limit chain length to prevent memory issues (keep last 10 entries)\n if (historyChain.length > 10) {\n historyChain = historyChain.slice(-10);\n }\n }\n const stateToStore = {\n from: window.location.href,\n historyChain: historyChain,\n data: options.state || undefined\n };\n window.history[options.replace ? 'replaceState' : 'pushState'](stateToStore, '', to.href);\n this.fireLocationChange(new AfterLocationChangeEvent(type, to, options.state));\n }\n start() {\n if (typeof window === \"undefined\") {\n return;\n }\n const _popStateListener = (ev) => {\n let type = ev.state ? 'popState' : 'linkClick';\n const to = new URL(window.location.href);\n let state = undefined;\n if (ev.state) {\n type = 'popState';\n state = ev.state.data;\n }\n else {\n type = 'linkClick';\n }\n this.fireLocationChange(new AfterLocationChangeEvent(type, to, state));\n };\n const _linkNavListener = (ev) => {\n const url = getElementHrefAsUrl(ev.target);\n if (url && url.origin === window.location.origin) {\n ev.preventDefault();\n const to = new URL(this.addStickyParams(url.href));\n const basePath = ev[BASE_PATH] || ev.target[BASE_PATH];\n if (basePath) {\n to.pathname = joinPath(basePath, to.pathname);\n }\n this._navigate(to, 'linkClick', {});\n }\n };\n this._popStateListener = _popStateListener;\n this._linkNavListener = _linkNavListener;\n window.addEventListener('popstate', _popStateListener);\n document.body.addEventListener('click', this._linkNavListener);\n }\n stop() {\n this._popStateListener && window.removeEventListener('popstate', this._popStateListener);\n this._linkNavListener && document.body.removeEventListener('click', this._linkNavListener);\n }\n}\n//# sourceMappingURL=HistoryNavigator.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { useEffect, useRef } from \"react\";\nimport { BASE_PATH } from \"./HistoryNavigator\";\nexport function FixLinks({ basePath, children }) {\n const ref = useRef(null);\n useEffect(() => {\n if (ref.current) {\n const divElem = ref.current;\n const listener = (ev) => {\n const elem = ev.target;\n if (elem.tagName.toLowerCase() === 'a') {\n ev[BASE_PATH] = basePath;\n }\n };\n divElem.addEventListener('click', listener);\n return () => {\n divElem.removeEventListener('click', listener);\n };\n }\n }, [ref.current]);\n return (_jsx(\"div\", { ref: ref, className: \"h-full w-full\", children: children }));\n}\n//# sourceMappingURL=FixLinks.js.map","import { getPathSegments, toSegments } from \"./path\";\n/**\n * Path matcher which support :param and *wildcard segments.\n * The wildcard segment is only supported as the last segment\n */\nexport class PathMatcher {\n tree = new RootSegmentNode();\n loadMapping(mapping) {\n for (const [key, value] of Object.entries(mapping)) {\n this.addSegments(getPathSegments(key), value);\n }\n }\n addPath(path, value) {\n this.addSegments(getPathSegments(path), value);\n }\n addSegments(segments, value) {\n let node = this.tree;\n for (let i = 0, l = segments.length; i < l; i++) {\n const segment = segments[i];\n if (segment[0] === ':') {\n let childNode = node.wildcard;\n if (!childNode) {\n childNode = new VariableSegmentNode(segment);\n node.wildcard = childNode;\n }\n else if (!(childNode instanceof VariableSegmentNode)) {\n throw new Error(`Failed to index path segments: ${segments.join('/')}. A wildcard \":\" segment will overwrite an existing wildcard segment at path: ${'/' + segments.slice(0, i).join(\"/\")}`);\n }\n node = childNode;\n }\n else if (segment === '*') {\n if (node.wildcard) {\n throw new Error(`Failed to index path segments: ${segments.join('/')}. A wildcard \"*\" segment already exists at path: ${'/' + segments.slice(0, i).join(\"/\")}`);\n }\n node.wildcard = new WildcardSegmentNode(segment, value);\n if (i < l - 1) {\n throw new Error(`Failed to index path segments: ${segments.join('/')}. A wildcard segment must be the last segment`);\n }\n return;\n }\n else {\n let childNode = node.children[segment];\n if (!childNode) {\n childNode = new LiteralSegmentNode(segment);\n node.children[segment] = childNode;\n } // else // a literal segment already exists\n node = childNode;\n }\n }\n if (node.value !== undefined) {\n throw new Error(`Failed to index path segments: ${segments.join('/')}. A value already exists at path: ${'/' + segments.join(\"/\")}`);\n }\n node.value = value;\n }\n match(path) {\n const segments = toSegments(path);\n if (segments.length === 0) {\n return null;\n }\n const params = {};\n let node = this.tree;\n for (const segment of segments) {\n const match = node.match(segment, params);\n if (match) {\n node = match;\n }\n else {\n return null;\n }\n }\n if (!node.value) { // not a leaf node (partial match)\n if (node instanceof ParentSegmentNode) {\n if (node.wildcard instanceof WildcardSegmentNode) {\n node = node.wildcard.match('', params);\n if (!node.value) {\n throw new Error(\"Wildcard segment node `*` must have a value\");\n }\n }\n }\n if (!node.value)\n return null; // not a leaf node, neither a trailing wildcard (partial match)\n }\n let matchedSegments, remainingSegments;\n if (params._ && params._.length > 0) {\n matchedSegments = segments.slice(0, -params._.length);\n remainingSegments = params._;\n }\n else {\n matchedSegments = segments;\n }\n return { params, matchedSegments, remainingSegments, value: node.value };\n }\n}\nclass ParentSegmentNode {\n name;\n value;\n children = {};\n wildcard;\n constructor(name, value) {\n this.name = name;\n this.value = value;\n }\n match(segment, params) {\n let node = this.children[segment];\n if (node) {\n return node;\n }\n else if (this.wildcard) {\n if (this.wildcard instanceof WildcardSegmentNode) {\n return this.wildcard.match(segment, params);\n }\n else if (this.wildcard instanceof VariableSegmentNode) {\n params[this.wildcard.paramName] = segment;\n return this.wildcard;\n }\n else {\n throw new Error(\"Unknown wildcard segment node type: \" + this.wildcard.constructor.name);\n }\n }\n else {\n return null;\n }\n }\n}\nclass RootSegmentNode extends ParentSegmentNode {\n constructor() {\n super(\"#root\");\n }\n}\nclass LiteralSegmentNode extends ParentSegmentNode {\n constructor(name, value) {\n super(name, value);\n }\n}\nclass VariableSegmentNode extends ParentSegmentNode {\n paramName;\n constructor(name, value) {\n super(name, value);\n this.paramName = name.substring(1);\n }\n}\nclass WildcardSegmentNode {\n name;\n value;\n constructor(name, value) {\n this.name = name;\n this.value = value;\n }\n match(segment, params) {\n if (!params._) {\n params._ = segment ? [segment] : [];\n }\n else {\n segment && params._.push(segment);\n }\n return this;\n }\n}\n//# sourceMappingURL=PathMatcher.js.map","import { createContext, useContext, useEffect } from \"react\";\nimport { HistoryNavigator } from \"./HistoryNavigator\";\nimport { PathMatcher } from \"./PathMatcher\";\nimport { isRootPath, joinPath } from \"./path\";\nexport class BaseRouter {\n // the path to use when navigating to the root of the router\n index;\n matcher = new PathMatcher();\n constructor(routes, index) {\n this.index = index;\n for (const route of routes) {\n this.matcher.addPath(route.path, route);\n }\n }\n match(path) {\n const useIndex = isRootPath(path) && this.index;\n return this.matcher.match(useIndex ? this.index : path);\n }\n}\nexport class Router extends BaseRouter {\n prompt;\n observer;\n navigator = new HistoryNavigator();\n constructor(routes, updateState) {\n super(routes);\n this.navigator.addListener((event) => {\n if (event.isCancelable && this.prompt && !!this.prompt.when) {\n if (!window.confirm(this.prompt.message))\n return;\n }\n if (this.observer) {\n this.observer(event);\n }\n // only process afterChange events\n if (event.name === \"afterChange\") {\n const match = this.match(event.location.pathname);\n if (match && match.value) {\n updateState({\n ...match,\n state: event.state,\n });\n }\n else {\n updateState(null);\n }\n }\n });\n }\n getTopRouter() {\n return this;\n }\n /**\n * Subsequent navigations will preserve the given params in the query string.\n * Use null to clear the sticky params.\n * @param params\n */\n setStickyParams(params) {\n this.navigator.stickyParams = params != null ? params : undefined;\n }\n withObserver(observer) {\n this.observer = observer;\n return this;\n }\n start() {\n this.navigator.start();\n // initialize with the current location\n this.navigator.firePageLoad();\n }\n stop() {\n this.navigator.stop();\n }\n navigate(path, options) {\n this.navigator.navigate(path, options);\n }\n}\nexport class NestedRouter extends BaseRouter {\n parent;\n basePath;\n constructor(parent, basePath, routes) {\n super(routes);\n this.parent = parent;\n this.basePath = basePath;\n }\n getTopRouter() {\n if (this.parent instanceof Router) {\n return this.parent;\n }\n else {\n return this.parent.getTopRouter();\n }\n }\n navigate(path, options) {\n // base path is nested by default in a NestedRouter unless explicitly set to false by caller\n const isBasePathNested = options?.isBasePathNested ?? true;\n let basePath;\n if (isBasePathNested) {\n const childBasePath = options?.basePath;\n // e.g. \"/store\" + \"/objects/123\" => \"/store/objects/123\"\n basePath = childBasePath ? joinPath(this.basePath, childBasePath) : this.basePath;\n }\n else {\n // e.g. \"/store\" + \"/studio\" => \"/studio\"\n basePath = options?.basePath ?? this.basePath;\n }\n this.parent.navigate(path, {\n ...options,\n basePath,\n });\n }\n}\nconst ReactRouterContext = createContext(undefined);\nexport { ReactRouterContext };\nexport function useRouterContext() {\n const ctx = useContext(ReactRouterContext);\n if (!ctx) {\n throw new Error(\"useRouter must be used within a RouterProvider\");\n }\n return ctx;\n}\nexport function useNavigate() {\n const { navigate } = useRouterContext();\n return navigate;\n}\nexport function useParams(arg) {\n const { params } = useRouterContext();\n if (arg) {\n return params[arg];\n }\n else {\n return params;\n }\n}\nexport function useLocation() {\n const { location } = useRouterContext();\n return location;\n}\nexport function useNavigationPrompt(prompt) {\n const { router } = useRouterContext();\n useEffect(() => {\n router.getTopRouter().prompt = prompt;\n return () => {\n router.getTopRouter().prompt = undefined;\n };\n }, []);\n useEffect(() => {\n if (prompt.when) {\n const doBlock = prompt.when;\n const listener = function (ev) {\n if (doBlock) {\n ev.preventDefault();\n ev.returnValue = \"\";\n }\n };\n window.addEventListener(\"beforeunload\", listener);\n return () => {\n window.removeEventListener(\"beforeunload\", listener);\n };\n }\n }, [prompt.when]);\n}\n//# sourceMappingURL=Router.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { useNavigate, useRouterContext } from \"./Router\";\nexport function Nav({ children, onClick }) {\n const navigate = useNavigate();\n const _onClick = (ev) => {\n const link = ev.target.closest('a');\n if (link && link.href) {\n ev.stopPropagation();\n ev.preventDefault();\n navigate(link.href, { replace: true });\n onClick?.(ev);\n }\n };\n return (_jsx(\"span\", { onClick: _onClick, children: children }));\n}\nexport function NavLink({ children, href, className, topLevelNav, clearBreadcrumbs = false }) {\n const { router } = useRouterContext();\n const _onClick = (ev) => {\n ev.stopPropagation();\n ev.preventDefault();\n const actualRouter = topLevelNav ? router.getTopRouter() : router;\n actualRouter.navigate(href, { replace: clearBreadcrumbs });\n };\n return (_jsx(\"a\", { href: href, className: className, onClick: _onClick, children: children }));\n}\n//# sourceMappingURL=Nav.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { useEffect, useState } from \"react\";\nimport { useRouterContext } from \"./Router\";\nexport function RouteComponent({ spinner }) {\n const ctx = useRouterContext();\n const route = ctx.route;\n if (route.Component) {\n const Component = route.Component;\n return _jsx(Component, { ...ctx.params });\n }\n else if (route.LazyComponent) {\n return _jsx(LazyRouteComponent, { route: route, spinner: spinner });\n }\n else {\n throw new Error(`Invalid route for ${route.path}. Either Component or LazyCOmponent must be specified.`);\n }\n}\nfunction LazyRouteComponent({ route, spinner }) {\n const [Component, setComponent] = useState(null);\n useEffect(() => {\n route.LazyComponent().then(module => {\n if (!module.default) {\n throw new Error(`Lazy module for ${route.path} does not have a default export`);\n }\n // we need to wrap the component type in an arrow function\n // otherwise the setState function will execute the function as a state update function\n setComponent(() => module.default);\n });\n }, [route]);\n return Component ? (_jsx(Component, {})) : spinner || null;\n}\n//# sourceMappingURL=RouteComponent.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { FixLinks } from \"./FixLinks\";\nimport { RouteComponent } from \"./RouteComponent\";\nimport { ReactRouterContext, useRouterContext } from \"./Router\";\nimport { joinPath } from \"./path\";\nexport function NestedNavigationContext({ basePath, fixLinks = false, children }) {\n const ctx = useRouterContext();\n const wrapWithFixLinks = fixLinks ?\n (elem) => _jsx(FixLinks, { basePath: ctx.matchedRoutePath, children: elem })\n : (elem) => elem;\n return (_jsx(ReactRouterContext.Provider, { value: {\n ...ctx,\n navigate: (to, options) => {\n const actualBasePath = options?.basePath ? joinPath(basePath, options.basePath) : basePath;\n return ctx.navigate(to, { ...options, basePath: actualBasePath });\n }\n }, children: wrapWithFixLinks(children ? children : _jsx(RouteComponent, {})) }));\n}\n//# sourceMappingURL=NestedNavigationContext.js.map","import { jsxs as _jsxs, jsx as _jsx } from \"react/jsx-runtime\";\nimport { useRouterContext } from \"./Router\";\nexport function Route404Component() {\n const ctx = useRouterContext();\n return _jsxs(\"div\", { children: [\"Route not found for path \", ctx.matchedRoutePath] });\n}\nexport function createRoute404() {\n return {\n params: {},\n matchedSegments: [],\n state: null,\n value: {\n path: 'virtual:404',\n Component: () => _jsx(Route404Component, {})\n }\n };\n}\n//# sourceMappingURL=Route404.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { useEffect, useMemo, useState } from \"react\";\nimport { FixLinks } from \"./FixLinks\";\nimport { createRoute404 } from \"./Route404\";\nimport { RouteComponent } from \"./RouteComponent\";\nimport { NestedRouter, ReactRouterContext, useRouterContext } from \"./Router\";\nexport function NestedRouterProvider({ routes, index, children, fixLinks = false }) {\n const ctx = useRouterContext();\n const [nestedRouteMatch, setNestedRouteMatch] = useState(undefined);\n const nestedRouter = useMemo(() => {\n if (typeof window === 'undefined')\n return null;\n const basePath = ctx.matchedRoutePath;\n const nestedRouter = new NestedRouter(ctx.router, basePath, routes);\n nestedRouter.index = index;\n return nestedRouter;\n }, []);\n useEffect(() => {\n if (nestedRouter) {\n if (ctx.matchedRoutePath !== nestedRouter.basePath) {\n // the change doesn't belong to this nested router\n // it should be handled by the top level router which will change the page or the nested router\n return;\n }\n const route = nestedRouter.match(ctx.remainingPath || '/') || createRoute404();\n setNestedRouteMatch(route);\n }\n }, [nestedRouter, ctx.remainingPath]);\n const wrapWithFixLinks = fixLinks ?\n (elem) => _jsx(FixLinks, { basePath: ctx.matchedRoutePath, children: elem })\n : (elem) => elem;\n return nestedRouteMatch && (_jsx(ReactRouterContext.Provider, { value: {\n ...ctx,\n router: nestedRouter,\n route: nestedRouteMatch.value,\n params: nestedRouteMatch.params,\n matchedRoutePath: '/' + nestedRouteMatch.matchedSegments.join('/'),\n remainingPath: nestedRouteMatch.remainingSegments ? '/' + nestedRouteMatch.remainingSegments.join('/') : undefined,\n navigate: (to, options) => {\n return nestedRouter.navigate(to, options);\n }\n }, children: wrapWithFixLinks(children ? children : _jsx(RouteComponent, {})) }));\n}\n//# sourceMappingURL=NestedRouterProvider.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { useSafeLayoutEffect } from \"@vertesia/ui/core\";\n//import { useSafeLayoutEffect } from \"../core\";\nimport { useMemo, useState } from \"react\";\nimport { RouteComponent } from \"./RouteComponent\";\nimport { ReactRouterContext, Router } from \"./Router\";\nimport { createRoute404 } from \"./Route404\";\nexport function RouterProvider({ routes, index, onChange, children }) {\n const [state, setState] = useState(undefined);\n const router = useMemo(() => {\n if (typeof window === 'undefined')\n return null;\n const router = new Router(routes, (match) => {\n if (match === null) {\n match = createRoute404();\n }\n setState({\n location: window.location,\n route: match.value,\n params: match.params,\n state: match.state,\n router: router,\n matchedRoutePath: '/' + match.matchedSegments.join('/'),\n remainingPath: match.remainingSegments ? '/' + match.remainingSegments.join('/') : undefined,\n navigate: (to, options) => {\n return router.navigate(to, options);\n }\n });\n }).withObserver(onChange);\n router.index = index;\n return router;\n }, []);\n useSafeLayoutEffect(() => {\n router && router.start();\n return () => {\n router && router.stop();\n };\n }, []);\n return state && (_jsx(ReactRouterContext.Provider, { value: state, children: children ? children : _jsx(RouteComponent, {}) }));\n}\n//# sourceMappingURL=RouterProvider.js.map"],"names":["isRootPath","path","joinPath","path1","path2","endsWith","startsWith","substring","getPathSegments","s","e","length","split","toSegments","Array","isArray","Error","PathWithParams","params","hash","constructor","i","lastIndexOf","this","indexOf","URLSearchParams","add","key","value","Object","entries","set","toString","BASE_PATH","Symbol","LocationChangeEvent","name","type","location","state","_canceled","isPageLoad","isBackForward","isLinkClick","isNavigation","isCancelable","cancel","BeforeLocationChangeEvent","super","AfterLocationChangeEvent","HistoryNavigator","stickyParams","_popStateListener","_linkNavListener","_listeners","addListener","listener","push","fireLocationChange","event","firePageLoad","URL","window","href","addStickyParams","navigate","to","options","basePath","_navigate","beforeEvent","currentState","history","newChainEntry","title","document","pathname","search","historyChain","replace","slice","stateToStore","from","data","undefined","start","ev","url","elem","tagName","toLowerCase","trim","getElementHrefAsUrl","target","origin","preventDefault","addEventListener","body","stop","removeEventListener","FixLinks","children","ref","useRef","useEffect","current","divElem","_jsx","className","PathMatcher","tree","RootSegmentNode","loadMapping","mapping","addSegments","addPath","segments","node","l","segment","childNode","wildcard","VariableSegmentNode","join","WildcardSegmentNode","LiteralSegmentNode","match","matchedSegments","remainingSegments","ParentSegmentNode","_","paramName","BaseRouter","index","matcher","routes","route","useIndex","Router","prompt","observer","navigator","updateState","when","confirm","message","getTopRouter","setStickyParams","withObserver","NestedRouter","parent","isBasePathNested","childBasePath","ReactRouterContext","createContext","useRouterContext","ctx","useContext","useNavigate","useParams","arg","useLocation","useNavigationPrompt","router","doBlock","returnValue","Nav","onClick","link","closest","stopPropagation","NavLink","topLevelNav","clearBreadcrumbs","RouteComponent","spinner","Component","LazyComponent","LazyRouteComponent","setComponent","useState","then","module","default","NestedNavigationContext","fixLinks","wrapWithFixLinks","matchedRoutePath","Provider","actualBasePath","Route404Component","_jsxs","createRoute404","NestedRouterProvider","nestedRouteMatch","setNestedRouteMatch","nestedRouter","useMemo","remainingPath","RouterProvider","onChange","setState","useSafeLayoutEffect"],"mappings":"sNAA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AA,EAAQkE,KAEZS,EAAS,CACLnK,SAAUwB,OAAOxB,SACjBkG,MAAOV,EAAMlG,MACbV,OAAQ4G,EAAM5G,OACdqB,MAAOuF,EAAMvF,MACb4H,OAAQA,EACRwB,iBAAkB,IAAM7D,EAAMC,gBAAgBJ,KAAK,KACnD2E,cAAexE,EAAME,kBAAoB,IAAMF,EAAME,kBAAkBL,KAAK,UAAOvC,EACnFnB,SAAU,CAACC,EAAIC,IACJgG,EAAOlG,SAASC,EAAIC,QAGpCiF,aAAaoD,GAEhB,OADArC,EAAO9B,MAAQA,EACR8B,IACR,IAOH,OANAuC,GAAoB,KAChBvC,GAAUA,EAAO9E,QACV,KACH8E,GAAUA,EAAOjE,UAEtB,IACI3D,GAAUoE,EAAK8C,EAAmBmC,SAAU,CAAEhK,MAAOW,EAAO8D,SAAUA,GAAsBM,EAAKmE,EAAgB,CAAA,IAC5H"}
1
+ {"version":3,"file":"vertesia-ui-router.js","sources":["esm/router/path.js","esm/router/PathWithParams.js","esm/router/HistoryNavigator.js","esm/router/FixLinks.js","esm/router/PathMatcher.js","esm/router/Router.js","esm/router/Nav.js","esm/router/RouteComponent.js","esm/router/NestedNavigationContext.js","esm/router/Route404.js","esm/router/NestedRouterProvider.js","esm/router/RouterProvider.js"],"sourcesContent":["export function isRootPath(path) {\n return path === '/' || path === '';\n}\nexport function joinPath(path1, path2) {\n if (path1.endsWith('/') && path2.startsWith('/')) {\n path2 = path1 + path2.substring(1);\n }\n else if (path1.endsWith('/')) {\n path2 = path1 + path2;\n }\n else if (path2.startsWith('/')) {\n path2 = path1 + path2;\n }\n else {\n path2 = path1 + '/' + path2;\n }\n return path2;\n}\nexport function getPathSegments(path) {\n if (path === '') {\n return [];\n }\n if (path === '/') {\n return [''];\n }\n let s = 0, e = path.length;\n if (path.startsWith('./')) {\n s = 2;\n }\n else if (path.startsWith('/')) {\n s = 1;\n }\n if (path.endsWith('/')) {\n e = path.length - 1;\n }\n return (s > 0 || e < path.length ? path.substring(s, e) : path).split('/');\n}\nexport function toSegments(path) {\n if (typeof path === 'string') {\n return getPathSegments(path);\n }\n else if (Array.isArray(path)) {\n return path;\n }\n else {\n throw new Error(`Unsupported path object: ${path}`);\n }\n}\n// export class Path {\n// static parse(path: string, abs = false) {\n// if (abs !== undefined) {\n// abs = path.startsWith('/');\n// }\n// return new Path(getPathSegments(path), abs);\n// }\n// constructor(public segments: string[], public isAbsolute = false) {\n// }\n// getParameters() {\n// const out = [];\n// for (const segment of this.segments) {\n// if (segment[0] === ':') {\n// out.push(segment.substring(1));\n// }\n// }\n// if (this.segments[this.segments.length - 1] === '*') {\n// out.push('_');\n// }\n// return out;\n// }\n// resolveParameters(path: string) {\n// const params: PathMatchParams = {};\n// const resolvedSegments = getPathSegments(path);\n// if (resolvedSegments.length < this.segments.length) {\n// return null;\n// }\n// const segments = this.segments;\n// for (let i = 0, l = segments.length; i < l; i++) {\n// const seg = segments[i];\n// if (seg[0] === ':') {\n// params[seg.substring(1)] = resolvedSegments[i];\n// }\n// }\n// if (resolvedSegments.length - this.segments.length) {\n// params._ = resolvedSegments.slice(this.segments.length);\n// }\n// return params;\n// }\n// match(path: string | string[] | Path) {\n// const segments = toSegments(path);\n// if (segments.length < this.segments.length) {\n// return false;\n// }\n// let params: PathMatchParams | undefined;\n// const mySegments = this.segments;\n// for (let i = 0, l = mySegments.length; i < l; i++) {\n// const segment = mySegments[i];\n// if (segment === ':') {\n// if (!params) params = {};\n// params[segment.substring(1)] = segment;\n// } else if (segment !== segments[i]) {\n// if (i === l - 1 && segment === '*') {\n// if (!params) params = {};\n// params._ = segments.slice(i);\n// return params;\n// }\n// return false;\n// }\n// }\n// return params ? params : false;\n// }\n// join(path: Path | string | string[]) {\n// const segments = toSegments(path);\n// return new Path(this.segments.concat(segments), this.isAbsolute);\n// }\n// getRelativePath(path: Path | string | string[], asAbsolute: boolean = false) {\n// const segments = toSegments(path);\n// const extraSegmentsCount = segments.length - this.segments.length;\n// if (extraSegmentsCount <= 0) {\n// return null;\n// }\n// return new Path(segments.slice(this.segments.length), asAbsolute);\n// }\n// prependSegments(segments: string[]) {\n// this.segments = segments.concat(this.segments);\n// }\n// appendSegments(segments: string[]) {\n// this.segments = this.segments.concat(segments);\n// }\n// toAbsolutePath() {\n// return '/' + this.segments.join('/');\n// }\n// toRelativePath() {\n// return this.segments.join('/');\n// }\n// toString() {\n// const path = this.segments.join('/');\n// return this.isAbsolute ? '/' + path : path;\n// }\n// }\n//# sourceMappingURL=path.js.map","export class PathWithParams {\n path;\n params;\n hash;\n constructor(path) {\n let i = path.lastIndexOf('#');\n if (i > -1) {\n this.hash = path.substring(i);\n path = path.substring(0, i);\n }\n else {\n this.hash = '';\n }\n i = path.indexOf('?');\n if (i > -1) {\n this.path = path.substring(0, i);\n this.params = new URLSearchParams(path.substring(i + 1));\n }\n else {\n this.path = path;\n this.params = new URLSearchParams();\n }\n }\n add(params) {\n for (const [key, value] of Object.entries(params)) {\n this.params.set(key, value);\n }\n return this;\n }\n toString() {\n return this.path + '?' + this.params.toString() + this.hash;\n }\n}\n//# sourceMappingURL=PathWithParams.js.map","import { joinPath } from \"./path\";\nimport { PathWithParams } from \"./PathWithParams\";\nconst BASE_PATH = Symbol('BASE_PATH');\nexport { BASE_PATH };\nexport class LocationChangeEvent {\n name;\n type;\n location;\n state;\n _canceled = false;\n constructor(name, type, location, state) {\n this.name = name;\n this.type = type;\n this.location = location;\n this.state = state;\n }\n get isPageLoad() {\n return this.type === 'initial';\n }\n get isBackForward() {\n return this.type === 'popState';\n }\n get isLinkClick() {\n return this.type === 'linkClick';\n }\n get isNavigation() {\n return this.type === 'navigate';\n }\n get isCancelable() {\n return this.name === 'beforeChange';\n }\n cancel() {\n if (this.name === 'afterChange') {\n throw new Error('Cannot cancel afterChange event');\n }\n this._canceled = true;\n }\n}\nexport class BeforeLocationChangeEvent extends LocationChangeEvent {\n constructor(type, location, state) {\n super('beforeChange', type, location, state);\n }\n}\nexport class AfterLocationChangeEvent extends LocationChangeEvent {\n constructor(type, location, state) {\n super('afterChange', type, location, state);\n }\n}\nfunction getElementHrefAsUrl(elem) {\n if (elem && elem.tagName.toLowerCase() === 'a') {\n const href = elem?.href.trim();\n if (typeof href === 'string' && href.length > 0) {\n return new URL(href);\n }\n }\n return null;\n}\nexport class HistoryNavigator {\n // params to preserve in the query string when navigating\n stickyParams;\n _popStateListener;\n _linkNavListener;\n _listeners = [];\n constructor() {\n }\n addListener(listener) {\n this._listeners.push(listener);\n }\n fireLocationChange(event) {\n for (const listener of this._listeners) {\n listener(event);\n }\n }\n /**\n * Should be called when the page is first loaded.\n * It will fire a location change event with type `initial` and the current window.location as the location\n */\n firePageLoad() {\n this.fireLocationChange(new AfterLocationChangeEvent('initial', new URL(window.location.href)));\n }\n addStickyParams(path) {\n if (this.stickyParams) {\n return new PathWithParams(path).add(this.stickyParams).toString();\n }\n return path;\n }\n navigate(to, options = {}) {\n if (options.basePath) {\n let basePath = options.basePath;\n if (!basePath.startsWith('/')) {\n basePath = '/' + basePath;\n }\n to = joinPath(basePath, to);\n }\n to = this.addStickyParams(to);\n this._navigate(new URL(to, window.location.href), 'navigate', options);\n }\n _navigate(to, type, options) {\n const beforeEvent = new BeforeLocationChangeEvent(type, to, options.state);\n this.fireLocationChange(beforeEvent);\n if (beforeEvent._canceled) {\n return;\n }\n // Build navigation chain by preserving previous history\n const currentState = window.history.state;\n const currentTitle = document.title;\n // Create new history chain entry\n const newChainEntry = {\n title: currentTitle,\n href: window.location.pathname + window.location.search + window.location.hash\n };\n // Build the history chain - clear if using replace\n let historyChain = [];\n if (!options.replace && currentState?.historyChain) {\n historyChain = [...currentState.historyChain];\n }\n // Only add to chain if not replacing\n if (!options.replace) {\n historyChain.push(newChainEntry);\n // Limit chain length to prevent memory issues (keep last 10 entries)\n if (historyChain.length > 10) {\n historyChain = historyChain.slice(-10);\n }\n }\n const stateToStore = {\n from: window.location.href,\n historyChain: historyChain,\n data: options.state || undefined\n };\n window.history[options.replace ? 'replaceState' : 'pushState'](stateToStore, '', to.href);\n this.fireLocationChange(new AfterLocationChangeEvent(type, to, options.state));\n }\n start() {\n if (typeof window === \"undefined\") {\n return;\n }\n const _popStateListener = (ev) => {\n let type = ev.state ? 'popState' : 'linkClick';\n const to = new URL(window.location.href);\n let state = undefined;\n if (ev.state) {\n type = 'popState';\n state = ev.state.data;\n }\n else {\n type = 'linkClick';\n }\n this.fireLocationChange(new AfterLocationChangeEvent(type, to, state));\n };\n const _linkNavListener = (ev) => {\n const url = getElementHrefAsUrl(ev.target);\n if (url && url.origin === window.location.origin) {\n ev.preventDefault();\n const to = new URL(this.addStickyParams(url.href));\n const basePath = ev[BASE_PATH] || ev.target[BASE_PATH];\n if (basePath) {\n to.pathname = joinPath(basePath, to.pathname);\n }\n this._navigate(to, 'linkClick', {});\n }\n };\n this._popStateListener = _popStateListener;\n this._linkNavListener = _linkNavListener;\n window.addEventListener('popstate', _popStateListener);\n document.body.addEventListener('click', this._linkNavListener);\n }\n stop() {\n this._popStateListener && window.removeEventListener('popstate', this._popStateListener);\n this._linkNavListener && document.body.removeEventListener('click', this._linkNavListener);\n }\n}\n//# sourceMappingURL=HistoryNavigator.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { useEffect, useRef } from \"react\";\nimport { BASE_PATH } from \"./HistoryNavigator\";\nexport function FixLinks({ basePath, children }) {\n const ref = useRef(null);\n useEffect(() => {\n if (ref.current) {\n const divElem = ref.current;\n const listener = (ev) => {\n const elem = ev.target;\n if (elem.tagName.toLowerCase() === 'a') {\n ev[BASE_PATH] = basePath;\n }\n };\n divElem.addEventListener('click', listener);\n return () => {\n divElem.removeEventListener('click', listener);\n };\n }\n }, [ref.current]);\n return (_jsx(\"div\", { ref: ref, className: \"h-full w-full\", children: children }));\n}\n//# sourceMappingURL=FixLinks.js.map","import { getPathSegments, toSegments } from \"./path\";\n/**\n * Path matcher which support :param and *wildcard segments.\n * The wildcard segment is only supported as the last segment\n */\nexport class PathMatcher {\n tree = new RootSegmentNode();\n loadMapping(mapping) {\n for (const [key, value] of Object.entries(mapping)) {\n this.addSegments(getPathSegments(key), value);\n }\n }\n addPath(path, value) {\n this.addSegments(getPathSegments(path), value);\n }\n addSegments(segments, value) {\n let node = this.tree;\n for (let i = 0, l = segments.length; i < l; i++) {\n const segment = segments[i];\n if (segment[0] === ':') {\n let childNode = node.wildcard;\n if (!childNode) {\n childNode = new VariableSegmentNode(segment);\n node.wildcard = childNode;\n }\n else if (!(childNode instanceof VariableSegmentNode)) {\n throw new Error(`Failed to index path segments: ${segments.join('/')}. A wildcard \":\" segment will overwrite an existing wildcard segment at path: ${'/' + segments.slice(0, i).join(\"/\")}`);\n }\n node = childNode;\n }\n else if (segment === '*') {\n if (node.wildcard) {\n throw new Error(`Failed to index path segments: ${segments.join('/')}. A wildcard \"*\" segment already exists at path: ${'/' + segments.slice(0, i).join(\"/\")}`);\n }\n node.wildcard = new WildcardSegmentNode(segment, value);\n if (i < l - 1) {\n throw new Error(`Failed to index path segments: ${segments.join('/')}. A wildcard segment must be the last segment`);\n }\n return;\n }\n else {\n let childNode = node.children[segment];\n if (!childNode) {\n childNode = new LiteralSegmentNode(segment);\n node.children[segment] = childNode;\n } // else // a literal segment already exists\n node = childNode;\n }\n }\n if (node.value !== undefined) {\n throw new Error(`Failed to index path segments: ${segments.join('/')}. A value already exists at path: ${'/' + segments.join(\"/\")}`);\n }\n node.value = value;\n }\n match(path) {\n const segments = toSegments(path);\n if (segments.length === 0) {\n return null;\n }\n const params = {};\n let node = this.tree;\n for (const segment of segments) {\n const match = node.match(segment, params);\n if (match) {\n node = match;\n }\n else {\n return null;\n }\n }\n if (!node.value) { // not a leaf node (partial match)\n if (node instanceof ParentSegmentNode) {\n if (node.wildcard instanceof WildcardSegmentNode) {\n node = node.wildcard.match('', params);\n if (!node.value) {\n throw new Error(\"Wildcard segment node `*` must have a value\");\n }\n }\n }\n if (!node.value)\n return null; // not a leaf node, neither a trailing wildcard (partial match)\n }\n let matchedSegments, remainingSegments;\n if (params._ && params._.length > 0) {\n matchedSegments = segments.slice(0, -params._.length);\n remainingSegments = params._;\n }\n else {\n matchedSegments = segments;\n }\n return { params, matchedSegments, remainingSegments, value: node.value };\n }\n}\nclass ParentSegmentNode {\n name;\n value;\n children = {};\n wildcard;\n constructor(name, value) {\n this.name = name;\n this.value = value;\n }\n match(segment, params) {\n let node = this.children[segment];\n if (node) {\n return node;\n }\n else if (this.wildcard) {\n if (this.wildcard instanceof WildcardSegmentNode) {\n return this.wildcard.match(segment, params);\n }\n else if (this.wildcard instanceof VariableSegmentNode) {\n params[this.wildcard.paramName] = segment;\n return this.wildcard;\n }\n else {\n throw new Error(\"Unknown wildcard segment node type: \" + this.wildcard.constructor.name);\n }\n }\n else {\n return null;\n }\n }\n}\nclass RootSegmentNode extends ParentSegmentNode {\n constructor() {\n super(\"#root\");\n }\n}\nclass LiteralSegmentNode extends ParentSegmentNode {\n constructor(name, value) {\n super(name, value);\n }\n}\nclass VariableSegmentNode extends ParentSegmentNode {\n paramName;\n constructor(name, value) {\n super(name, value);\n this.paramName = name.substring(1);\n }\n}\nclass WildcardSegmentNode {\n name;\n value;\n constructor(name, value) {\n this.name = name;\n this.value = value;\n }\n match(segment, params) {\n if (!params._) {\n params._ = segment ? [segment] : [];\n }\n else {\n segment && params._.push(segment);\n }\n return this;\n }\n}\n//# sourceMappingURL=PathMatcher.js.map","import { createContext, useContext, useEffect } from \"react\";\nimport { HistoryNavigator } from \"./HistoryNavigator\";\nimport { PathMatcher } from \"./PathMatcher\";\nimport { isRootPath, joinPath } from \"./path\";\nexport class BaseRouter {\n // the path to use when navigating to the root of the router\n index;\n matcher = new PathMatcher();\n constructor(routes, index) {\n this.index = index;\n for (const route of routes) {\n this.matcher.addPath(route.path, route);\n }\n }\n match(path) {\n const useIndex = isRootPath(path) && this.index;\n return this.matcher.match(useIndex ? this.index : path);\n }\n}\nexport class Router extends BaseRouter {\n prompt;\n observer;\n navigator = new HistoryNavigator();\n constructor(routes, updateState) {\n super(routes);\n this.navigator.addListener((event) => {\n if (event.isCancelable && this.prompt && !!this.prompt.when) {\n if (!window.confirm(this.prompt.message))\n return;\n }\n if (this.observer) {\n this.observer(event);\n }\n // only process afterChange events\n if (event.name === \"afterChange\") {\n const match = this.match(event.location.pathname);\n if (match && match.value) {\n updateState({\n ...match,\n state: event.state,\n });\n }\n else {\n updateState(null);\n }\n }\n });\n }\n getTopRouter() {\n return this;\n }\n /**\n * Subsequent navigations will preserve the given params in the query string.\n * Use null to clear the sticky params.\n * @param params\n */\n setStickyParams(params) {\n this.navigator.stickyParams = params != null ? params : undefined;\n }\n withObserver(observer) {\n this.observer = observer;\n return this;\n }\n start() {\n this.navigator.start();\n // initialize with the current location\n this.navigator.firePageLoad();\n }\n stop() {\n this.navigator.stop();\n }\n navigate(path, options) {\n this.navigator.navigate(path, options);\n }\n}\nexport class NestedRouter extends BaseRouter {\n parent;\n basePath;\n constructor(parent, basePath, routes) {\n super(routes);\n this.parent = parent;\n this.basePath = basePath;\n }\n getTopRouter() {\n if (this.parent instanceof Router) {\n return this.parent;\n }\n else {\n return this.parent.getTopRouter();\n }\n }\n navigate(path, options) {\n // base path is nested by default in a NestedRouter unless explicitly set to false by caller\n const isBasePathNested = options?.isBasePathNested ?? true;\n let basePath;\n if (isBasePathNested) {\n const childBasePath = options?.basePath;\n // e.g. \"/store\" + \"/objects/123\" => \"/store/objects/123\"\n basePath = childBasePath ? joinPath(this.basePath, childBasePath) : this.basePath;\n }\n else {\n // e.g. \"/store\" + \"/studio\" => \"/studio\"\n basePath = options?.basePath ?? this.basePath;\n }\n this.parent.navigate(path, {\n ...options,\n basePath,\n });\n }\n}\nconst ReactRouterContext = createContext(undefined);\nexport { ReactRouterContext };\nexport function useRouterContext() {\n const ctx = useContext(ReactRouterContext);\n if (!ctx) {\n throw new Error(\"useRouter must be used within a RouterProvider\");\n }\n return ctx;\n}\nexport function useNavigate() {\n const { navigate } = useRouterContext();\n return navigate;\n}\nexport function useParams(arg) {\n const { params } = useRouterContext();\n if (arg) {\n return params[arg];\n }\n else {\n return params;\n }\n}\nexport function useLocation() {\n const { location } = useRouterContext();\n return location;\n}\nexport function useNavigationPrompt(prompt) {\n const { router } = useRouterContext();\n useEffect(() => {\n router.getTopRouter().prompt = prompt;\n return () => {\n router.getTopRouter().prompt = undefined;\n };\n }, []);\n useEffect(() => {\n if (prompt.when) {\n const doBlock = prompt.when;\n const listener = function (ev) {\n if (doBlock) {\n ev.preventDefault();\n ev.returnValue = \"\";\n }\n };\n window.addEventListener(\"beforeunload\", listener);\n return () => {\n window.removeEventListener(\"beforeunload\", listener);\n };\n }\n }, [prompt.when]);\n}\n//# sourceMappingURL=Router.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { useNavigate, useRouterContext } from \"./Router\";\nexport function Nav({ children, onClick }) {\n const navigate = useNavigate();\n const _onClick = (ev) => {\n const link = ev.target.closest('a');\n if (link && link.href) {\n ev.stopPropagation();\n ev.preventDefault();\n navigate(link.href, { replace: true });\n onClick?.(ev);\n }\n };\n return (_jsx(\"span\", { onClick: _onClick, children: children }));\n}\nexport function NavLink({ children, href, className, topLevelNav, clearBreadcrumbs = false }) {\n const { router } = useRouterContext();\n const _onClick = (ev) => {\n ev.stopPropagation();\n ev.preventDefault();\n const actualRouter = topLevelNav ? router.getTopRouter() : router;\n actualRouter.navigate(href, { replace: clearBreadcrumbs });\n };\n return (_jsx(\"a\", { href: href, className: className, onClick: _onClick, children: children }));\n}\n//# sourceMappingURL=Nav.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { useEffect, useState } from \"react\";\nimport { useRouterContext } from \"./Router\";\nexport function RouteComponent({ spinner }) {\n const ctx = useRouterContext();\n const route = ctx.route;\n if (route.Component) {\n const Component = route.Component;\n return _jsx(Component, { ...ctx.params });\n }\n else if (route.LazyComponent) {\n return _jsx(LazyRouteComponent, { route: route, spinner: spinner });\n }\n else {\n throw new Error(`Invalid route for ${route.path}. Either Component or LazyCOmponent must be specified.`);\n }\n}\nfunction LazyRouteComponent({ route, spinner }) {\n const [Component, setComponent] = useState(null);\n useEffect(() => {\n route.LazyComponent().then(module => {\n if (!module.default) {\n throw new Error(`Lazy module for ${route.path} does not have a default export`);\n }\n // we need to wrap the component type in an arrow function\n // otherwise the setState function will execute the function as a state update function\n setComponent(() => module.default);\n });\n }, [route]);\n return Component ? (_jsx(Component, {})) : spinner || null;\n}\n//# sourceMappingURL=RouteComponent.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { FixLinks } from \"./FixLinks\";\nimport { RouteComponent } from \"./RouteComponent\";\nimport { ReactRouterContext, useRouterContext } from \"./Router\";\nimport { joinPath } from \"./path\";\nexport function NestedNavigationContext({ basePath, fixLinks = false, children }) {\n const ctx = useRouterContext();\n const wrapWithFixLinks = fixLinks ?\n (elem) => _jsx(FixLinks, { basePath: ctx.matchedRoutePath, children: elem })\n : (elem) => elem;\n return (_jsx(ReactRouterContext.Provider, { value: {\n ...ctx,\n navigate: (to, options) => {\n const actualBasePath = options?.basePath ? joinPath(basePath, options.basePath) : basePath;\n return ctx.navigate(to, { ...options, basePath: actualBasePath });\n }\n }, children: wrapWithFixLinks(children ? children : _jsx(RouteComponent, {})) }));\n}\n//# sourceMappingURL=NestedNavigationContext.js.map","import { jsxs as _jsxs, jsx as _jsx } from \"react/jsx-runtime\";\nimport { useRouterContext } from \"./Router\";\nexport function Route404Component() {\n const ctx = useRouterContext();\n return _jsxs(\"div\", { children: [\"Route not found for path \", ctx.matchedRoutePath] });\n}\nexport function createRoute404() {\n return {\n params: {},\n matchedSegments: [],\n state: null,\n value: {\n path: 'virtual:404',\n Component: () => _jsx(Route404Component, {})\n }\n };\n}\n//# sourceMappingURL=Route404.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { useEffect, useMemo, useState } from \"react\";\nimport { FixLinks } from \"./FixLinks\";\nimport { createRoute404 } from \"./Route404\";\nimport { RouteComponent } from \"./RouteComponent\";\nimport { NestedRouter, ReactRouterContext, useRouterContext } from \"./Router\";\nexport function NestedRouterProvider({ routes, index, children, fixLinks = false }) {\n const ctx = useRouterContext();\n const [nestedRouteMatch, setNestedRouteMatch] = useState(undefined);\n const nestedRouter = useMemo(() => {\n if (typeof window === 'undefined')\n return null;\n const basePath = ctx.matchedRoutePath;\n const nestedRouter = new NestedRouter(ctx.router, basePath, routes);\n nestedRouter.index = index;\n return nestedRouter;\n }, []);\n useEffect(() => {\n if (nestedRouter) {\n if (ctx.matchedRoutePath !== nestedRouter.basePath) {\n // the change doesn't belong to this nested router\n // it should be handled by the top level router which will change the page or the nested router\n return;\n }\n const route = nestedRouter.match(ctx.remainingPath || '/') || createRoute404();\n setNestedRouteMatch(route);\n }\n }, [nestedRouter, ctx.remainingPath]);\n const wrapWithFixLinks = fixLinks ?\n (elem) => _jsx(FixLinks, { basePath: ctx.matchedRoutePath, children: elem })\n : (elem) => elem;\n return nestedRouteMatch && (_jsx(ReactRouterContext.Provider, { value: {\n ...ctx,\n router: nestedRouter,\n route: nestedRouteMatch.value,\n params: nestedRouteMatch.params,\n matchedRoutePath: '/' + nestedRouteMatch.matchedSegments.join('/'),\n remainingPath: nestedRouteMatch.remainingSegments ? '/' + nestedRouteMatch.remainingSegments.join('/') : undefined,\n navigate: (to, options) => {\n return nestedRouter.navigate(to, options);\n }\n }, children: wrapWithFixLinks(children ? children : _jsx(RouteComponent, {})) }));\n}\n//# sourceMappingURL=NestedRouterProvider.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { useSafeLayoutEffect } from \"@vertesia/ui/core\";\n//import { useSafeLayoutEffect } from \"../core\";\nimport { useMemo, useState } from \"react\";\nimport { RouteComponent } from \"./RouteComponent\";\nimport { ReactRouterContext, Router } from \"./Router\";\nimport { createRoute404 } from \"./Route404\";\nexport function RouterProvider({ routes, index, onChange, children }) {\n const [state, setState] = useState(undefined);\n const router = useMemo(() => {\n if (typeof window === 'undefined')\n return null;\n const router = new Router(routes, (match) => {\n if (match === null) {\n match = createRoute404();\n }\n setState({\n location: window.location,\n route: match.value,\n params: match.params,\n state: match.state,\n router: router,\n matchedRoutePath: '/' + match.matchedSegments.join('/'),\n remainingPath: match.remainingSegments ? '/' + match.remainingSegments.join('/') : undefined,\n navigate: (to, options) => {\n return router.navigate(to, options);\n }\n });\n }).withObserver(onChange);\n router.index = index;\n return router;\n }, []);\n useSafeLayoutEffect(() => {\n router && router.start();\n return () => {\n router && router.stop();\n };\n }, []);\n return state && (_jsx(ReactRouterContext.Provider, { value: state, children: children ? children : _jsx(RouteComponent, {}) }));\n}\n//# sourceMappingURL=RouterProvider.js.map"],"names":["isRootPath","path","joinPath","path1","path2","endsWith","startsWith","substring","getPathSegments","s","e","length","split","toSegments","Array","isArray","Error","PathWithParams","params","hash","constructor","i","lastIndexOf","this","indexOf","URLSearchParams","add","key","value","Object","entries","set","toString","BASE_PATH","Symbol","LocationChangeEvent","name","type","location","state","_canceled","isPageLoad","isBackForward","isLinkClick","isNavigation","isCancelable","cancel","BeforeLocationChangeEvent","super","AfterLocationChangeEvent","HistoryNavigator","stickyParams","_popStateListener","_linkNavListener","_listeners","addListener","listener","push","fireLocationChange","event","firePageLoad","URL","window","href","addStickyParams","navigate","to","options","basePath","_navigate","beforeEvent","currentState","history","newChainEntry","title","document","pathname","search","historyChain","replace","slice","stateToStore","from","data","undefined","start","ev","url","elem","tagName","toLowerCase","trim","getElementHrefAsUrl","target","origin","preventDefault","addEventListener","body","stop","removeEventListener","FixLinks","children","ref","useRef","useEffect","current","divElem","_jsx","className","PathMatcher","tree","RootSegmentNode","loadMapping","mapping","addSegments","addPath","segments","node","l","segment","childNode","wildcard","VariableSegmentNode","join","WildcardSegmentNode","LiteralSegmentNode","match","matchedSegments","remainingSegments","ParentSegmentNode","_","paramName","BaseRouter","index","matcher","routes","route","useIndex","Router","prompt","observer","navigator","updateState","when","confirm","message","getTopRouter","setStickyParams","withObserver","NestedRouter","parent","isBasePathNested","childBasePath","ReactRouterContext","createContext","useRouterContext","ctx","useContext","useNavigate","useParams","arg","useLocation","useNavigationPrompt","router","doBlock","returnValue","Nav","onClick","link","closest","stopPropagation","NavLink","topLevelNav","clearBreadcrumbs","RouteComponent","spinner","Component","LazyComponent","LazyRouteComponent","setComponent","useState","then","module","default","NestedNavigationContext","fixLinks","wrapWithFixLinks","matchedRoutePath","Provider","actualBasePath","Route404Component","_jsxs","createRoute404","NestedRouterProvider","nestedRouteMatch","setNestedRouteMatch","nestedRouter","useMemo","remainingPath","RouterProvider","onChange","setState","useSafeLayoutEffect"],"mappings":"sNAAO,SAASA,EAAWC,GACvB,MAAgB,MAATA,GAAyB,KAATA,CAC3B,CACO,SAASC,EAASC,EAAOC,GAa5B,OAXIA,EADAD,EAAME,SAAS,MAAQD,EAAME,WAAW,KAChCH,EAAQC,EAAMG,UAAU,GAE3BJ,EAAME,SAAS,MAGfD,EAAME,WAAW,KAFdH,EAAQC,EAMRD,EAAQ,IAAMC,CAG9B,CACO,SAASI,EAAgBP,GAC5B,GAAa,KAATA,EACA,MAAO,GAEX,GAAa,MAATA,EACA,MAAO,CAAC,IAEZ,IAAIQ,EAAI,EAAGC,EAAIT,EAAKU,OAUpB,OATIV,EAAKK,WAAW,MAChBG,EAAI,EAECR,EAAKK,WAAW,OACrBG,EAAI,GAEJR,EAAKI,SAAS,OACdK,EAAIT,EAAKU,OAAS,IAEdF,EAAI,GAAKC,EAAIT,EAAKU,OAASV,EAAKM,UAAUE,EAAGC,GAAKT,GAAMW,MAAM,IAC1E,CACO,SAASC,EAAWZ,GACvB,GAAoB,iBAATA,EACP,OAAOO,EAAgBP,GAEtB,GAAIa,MAAMC,QAAQd,GACnB,OAAOA,EAGP,MAAM,IAAIe,MAAM,4BAA4Bf,IAEpD,8FC/CO,MAAMgB,EACThB,KACAiB,OACAC,KACA,WAAAC,CAAYnB,GACR,IAAIoB,EAAIpB,EAAKqB,YAAY,KACrBD,GAAI,GACJE,KAAKJ,KAAOlB,EAAKM,UAAUc,GAC3BpB,EAAOA,EAAKM,UAAU,EAAGc,IAGzBE,KA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JiI,EAAanI,SAASC,EAAIC,IAEtCkC,SAAUqF,EAAiBrF,GAAsBM,EAAKmE,EAAgB,CAAA,KACjF,CCnCO,SAASyB,GAAehE,OAAEA,EAAMF,MAAEA,EAAKmE,SAAEA,EAAQnG,SAAEA,IACtD,MAAO9D,EAAOkK,GAAYrB,OAAShG,GAC7B+E,EAASkC,GAAQ,KACnB,GAAsB,oBAAXvI,OACP,OAAO,KACX,MAAMqG,EAAS,IAAIzB,EAAOH,GAAST,IACjB,OAAVA,IACAA,EAAQkE,KAEZS,EAAS,CACLnK,SAAUwB,OAAOxB,SACjBkG,MAAOV,EAAMlG,MACbV,OAAQ4G,EAAM5G,OACdqB,MAAOuF,EAAMvF,MACb4H,OAAQA,EACRwB,iBAAkB,IAAM7D,EAAMC,gBAAgBJ,KAAK,KACnD2E,cAAexE,EAAME,kBAAoB,IAAMF,EAAME,kBAAkBL,KAAK,UAAOvC,EACnFnB,SAAU,CAACC,EAAIC,IACJgG,EAAOlG,SAASC,EAAIC,QAGpCiF,aAAaoD,GAEhB,OADArC,EAAO9B,MAAQA,EACR8B,IACR,IAOH,OANAuC,GAAoB,KAChBvC,GAAUA,EAAO9E,QACV,KACH8E,GAAUA,EAAOjE,UAEtB,IACI3D,GAAUoE,EAAK8C,EAAmBmC,SAAU,CAAEhK,MAAOW,EAAO8D,SAAUA,GAAsBM,EAAKmE,EAAgB,CAAE,IAC9H"}
@@ -1,2 +1,2 @@
1
- import{jwtDecode as e}from"jwt-decode";import{Env as t}from"@vertesia/ui/env";import{getAnalytics as o,logEvent as n}from"firebase/analytics";import{initializeApp as r}from"firebase/app";import{getAuth as i,onAuthStateChanged as s}from"firebase/auth";import{useState as a,useEffect as c,createContext as l,useContext as u,useCallback as h,useRef as d}from"react";import{useUserSession as g}from"@vertesia/ui/session";import{VertesiaClient as m}from"@vertesia/client";import{jsx as p}from"react/jsx-runtime";const f="composableai.lastSelectedAccountId",w="composableai.lastSelectedProjectId";let v,k,S=null,y=null,b=null;function T(){if(!S)try{if(!t.firebase)throw new Error("Firebase configuration is not available in the environment");S=r(t.firebase)}catch(e){throw console.error("Failed to initialize Firebase app:",e),new Error("Firebase initialization failed - environment may not be properly initialized")}return S}function _(){return y||(y=o(T())),y}function I(){return b||(b=i(T())),b}async function j(e){if(e)if(t.firebase)try{e&&console.log(`Resolving tenant ID from email: ${e}`);let o=3,n=250;for(;o>0;)try{const o=await fetch("/api/resolve-tenant",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({tenantEmail:e}),signal:AbortSignal.timeout(5e3)});if(!o)throw new Error("No response received from tenant API");if(!o.ok){try{const e=await o.json();console.error("Failed to resolve tenant ID:",e.error)}catch(e){console.error(`Failed to resolve tenant ID: HTTP ${o.status}`)}if(404===o.status)return void console.warn(`Tenant not found for ${e}`);throw new Error(`HTTP error ${o.status}`)}const n=await o.json();if(n&&n.firebaseTenantId){const e=I();return e.tenantId=n.firebaseTenantId,t.firebase.providerType=n.provider??"oidc",console.log(`Tenant ID set to ${e.tenantId}`),n}return void console.error(`Invalid response format, missing tenantId for ${e}`)}catch(e){if(!(o>1))throw e;console.warn(`Tenant resolution failed, retrying in ${n}ms...`,e),await new Promise((e=>setTimeout(e,n))),n*=2,o--}}catch(e){console.error("Error setting Firebase tenant:",e instanceof Error?e.message:"Unknown error")}else console.log("Firebase configuration is not available in the environment");else console.log("No tenant name or email specified, skipping tenant setup")}async function E(e){const o=I().currentUser;return o?o.getIdToken(e).then((n=>(t.logger.info("Got Firebase token",{vertesia:{user_email:o.email,user_name:o.displayName,user_id:o.uid,refresh:e}}),n))).catch((n=>(t.logger.error("Failed to get Firebase token",{vertesia:{user_email:o.email,user_name:o.displayName,user_id:o.uid,refresh:e,error:n}}),console.error("Failed to get access token",n),null))):(t.logger.warn("No user found"),Promise.resolve(null))}async function A(o,n,r,i){console.log(`Getting/refreshing composable token for account ${n} and project ${r} `),t.logger.info("Getting/refreshing composable token",{vertesia:{account_id:n,project_id:r}});const s=await o();if(!s)throw console.log("No id token found - using cookie auth"),new Error("No id token found");const a=t.endpoints.sts;console.log("Using STS for token generation:",a),t.logger.info("Using STS for token generation",{vertesia:{account_id:n,project_id:r,sts_url:a}});try{const o=new URL(a+"/token/issue"),c={type:"user",account_id:n,project_id:r,expires_at:i?Math.floor(Date.now()/1e3)+i:void 0},l=await fetch(o,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${s}`},body:JSON.stringify(c)});if(s&&412===l?.status){console.log("412: auth succeeded but user doesn't exist - signup required",l?.status),t.logger.error("412: auth succeeded but user doesn't exist - signup required",{vertesia:{account_id:n,project_id:r,status:l?.status}});const o=e(s);if(!o?.email)throw t.logger.error("No email found in id token"),new Error("No email found in id token");throw t.logger.error("User not found",{vertesia:{account_id:n,project_id:r,email:o.email}}),new U("User not found",o.email)}if(!l.ok){const e=await l.text();throw console.error("STS token generation failed:",l.status,e),t.logger.error("STS token generation failed",{vertesia:{status:l.status,error:e,account_id:n,project_id:r}}),new Error(`Failed to get token from STS: ${l.status}`)}const{token:u}=await l.json();return console.log("Successfully got token from STS"),t.logger.info("Successfully got token from STS"),u}catch(e){if(e instanceof U)throw e;throw localStorage.removeItem(f),localStorage.removeItem(w),console.error("Failed to get composable token from STS",e),t.logger.error("Failed to get composable token from STS",{vertesia:{account_id:n,project_id:r,error:e}}),new Error("Failed to get composable token")}}async function F(e,t,o){return A(E,e,t,o)}async function P(o,n,r,i=!1,s=!1){const a=o??localStorage.getItem(f)??void 0,c=n??localStorage.getItem(w+"-"+a)??void 0;if(!i&&v&&k&&k.exp>Date.now()/1e3+300)return{rawToken:v,token:k,error:!1};if(!s&&I().currentUser?v=await F(a,c):(r||v)&&(v=await A((()=>Promise.resolve(r??v)),a,c)),!v)throw t.logger.error("Cannot acquire a composable token",{vertesia:{account_id:a,project_id:c}}),new Error("Cannot acquire a composable token");if(k=e(v),!k||!k.exp||!v)throw console.error("Invalid composable token",k),t.logger.error("Invalid composable token",{vertesia:{account_id:a,project_id:c}}),new Error("Invalid composable token");return{rawToken:v,token:k,error:!1}}class U extends Error{email;constructor(e,t){super(e),this.name="UserNotFoundError",this.email=t}}function $(){const{user:e}=g(),[t,o]=a(null),[n,r]=a(!0),[i,s]=a(null);return c((()=>{(async()=>{if(!e?.email)return o(null),void r(!1);try{const t=await fetch("/api/resolve-tenant",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({tenantEmail:e.email})});if(t.ok){const e=await t.json();o(e?{tenantKey:e.name||"unknown",name:e.label||e.name||"Unknown",domain:e.domain||[],firebaseTenantId:e.firebaseTenantId,provider:e.provider,logo:e.logo}:null)}else o(null)}catch(e){console.error("Error loading current tenant:",e),s("Failed to load tenant configuration"),o(null)}finally{r(!1)}})()}),[e?.email]),{currentTenant:t,isLoading:n,error:i}}class N{types;map={};constructor(e){this.types=e,e.sort(((e,t)=>e.name.localeCompare(t.name)));for(const t of e)this.map[t.id]=t}getType(e){return this.map[e]}getTypeLayout(e){const t=this.map[e];return t?t.table_layout:void 0}getTypeName(e){const t=this.map[e];return t?t.name:void 0}}class C{isLoading=!0;client;authError;authToken;typeRegistry;setSession;lastSelectedAccount;lastSelectedProject;onboardingComplete;constructor(e,o){this.client=e||new m({serverUrl:t.endpoints.studio,storeUrl:t.endpoints.zeno,tokenServerUrl:t.endpoints.sts}),o&&(this.setSession=o),this.logout=this.logout.bind(this)}get store(){return this.client.store}get user(){return this.authToken}get account(){return this.authToken?.account}get project(){return this.authToken?.project}get accounts(){return this.authToken?.accounts}get authCallback(){return this.rawAuthToken.then((e=>`Bearer ${e}`))}get rawAuthToken(){return P().then((t=>{const o=t?.rawToken;if(!o)throw new Error("No token available");return this.authToken=e(o),o}))}signOut(){this.logout()}getAccount(){return this.authToken?.account}async login(o){return this.authError=void 0,this.isLoading=!1,this.client.withAuthCallback((()=>this.authCallback)),this.authToken=e(o),console.log(`Logging in as ${this.authToken?.name} with account ${this.authToken?.account.name} (${this.authToken?.account.id}, and project ${this.authToken?.project?.name} (${this.authToken?.project?.id})`),localStorage.setItem(f,this.authToken.account.id),localStorage.setItem(w+"-"+this.authToken.account.id,this.authToken.project?.id??""),t.onLogin?.(this.authToken),await Promise.all([this._loadTypes(),this.fetchOnboardingStatus()]),Promise.resolve()}isLoggedIn(){return!!this.authToken}logout(){console.log("Logging out"),this.authToken&&I().signOut(),this.authError=void 0,this.isLoading=!1,this.authToken=void 0,this.typeRegistry=void 0,this.setSession=void 0,this.client.withAuthCallback(void 0)}async switchAccount(e){localStorage.setItem(f,e),this&&(this.account&&this.project?localStorage.setItem(w+"-"+this.account.id,this.project.id):this.account&&localStorage.removeItem(w+"-"+this.account.id)),window.location.replace("/?a="+e)}async switchProject(e){this.account&&localStorage.setItem(w+"-"+this.account.id,e),window.location.replace("/?a="+this.account?.id+"&p="+e)}async _loadTypes(){if(this.project)return this.store.types.list({},{layout:!0}).then((e=>this.typeRegistry=new N(e))).catch((e=>{throw console.error("Failed to fetch object types",e),e}));console.log("No project selected")}async reloadTypes(){return this._loadTypes().then((()=>{this.setSession?.(this.clone())}))}async fetchAccounts(){return this.client.accounts.list().then((e=>{if(!this.authToken)throw new Error("No token available");this.authToken.accounts=e,this.setSession?.(this.clone())})).catch((e=>{throw console.error("Failed to fetch accounts",e),e}))}async fetchOnboardingStatus(){if(this.onboardingComplete)return console.log("Onboarding already completed"),!1;const e=this.onboardingComplete;try{const t=await this.client.account.onboardingProgress();if(this.onboardingComplete=Object.values(t).every((e=>!0===e)),e!==this.onboardingComplete)return!0;this.setSession?.(this.clone())}catch(e){console.error("Error fetching onboarding status:",e),this.onboardingComplete=!1,this.setSession?.(this.clone())}return!1}clone(){const e=new C(this.client);return e.isLoading=this.isLoading,e.authError=this.authError,e.authToken=this.authToken,e.setSession=this.setSession,e.lastSelectedAccount=this.lastSelectedAccount,e.switchAccount=this.switchAccount,e.typeRegistry=this.typeRegistry,e.onboardingComplete=this.onboardingComplete,e}}const L=l(void 0);function D(){const e=u(L);if(!e)throw new Error("useUserSession must be used within a UserSessionProvider");return e}const O="auth_state",R="auth_state_expiry";const x=[".composable.sh",".vertesia.dev","vertesia.app"];function z(){return!!t.isDocker||x.some((e=>window.location.hostname.endsWith(e)))}function q({children:e}){const o=new URLSearchParams(location.hash.substring(1)),n=o.get("token"),r=o.get("state"),[i,l]=a(new C),{generateState:u,verifyState:g,clearState:m}={generateState:h((()=>{const e=crypto.randomUUID(),t=Date.now()+3e5;return sessionStorage.setItem(O,e),sessionStorage.setItem(R,t.toString()),e}),[]),verifyState:h((e=>{if(!e)return"Missing state";const t=sessionStorage.getItem(O),o=parseInt(sessionStorage.getItem(R)||"0");let n;return n=t!==e?`State mismatched (${t} !== ${e})`:Date.now()>o?"State expired":void 0,n}),[]),clearState:h((()=>{sessionStorage.removeItem(O),sessionStorage.removeItem(R)}),[])},v=d(!1),k=(e,t)=>{const o=new URL("https://internal-auth.vertesia.app/?sts=https://sts-staging.vertesia.io"),n=new URL(window.location.href);n.hash="",o.searchParams.set("redirect_uri",n.toString()),o.searchParams.set("state",u()),location.replace(o.toString())};return c((()=>{if(v.current)return void console.log("Auth: skipping duplicate auth flow initiation");v.current=!0,console.log("Auth: starting auth flow"),t.logger.info("Starting auth flow");const e=new URL(window.location.href),o=e.searchParams.get("a")??localStorage.getItem(f)??void 0,a=e.searchParams.get("p")??localStorage.getItem(w+"-"+o)??void 0;if(console.log("Auth: selected account",o),console.log("Auth: selected project",a),t.logger.info("Selected account and project",{vertesia:{account_id:o,project_id:a}}),n&&r){const e=g(r);return e?(console.error(`Auth: invalid state: ${e}`),t.logger.error(`Invalid state: ${e}`,{vertesia:{state:r}}),k()):m(),void P(o,a,n,!1,z()).then((e=>{i.login(e.rawToken).then((()=>{l(i.clone()),window.location.hash=""}))})).catch((e=>{console.error("Failed to fetch user token from studio, redirecting to central auth",e),t.logger.error("Failed to fetch user token from studio, redirecting to central auth",{vertesia:{error:e}}),k()}))}return i.isLoggedIn()||(console.log("Auth: not logged in & no token/state"),t.logger.info("Not logged in & no token/state",{vertesia:{account_id:o,project_id:a}}),z()?(console.log("Auth: on dev domain, redirecting to central auth with selection",o,a),t.logger.info("Redirecting to central auth with selection",{vertesia:{account_id:o,project_id:a}}),k()):(console.log("Auth: not on dev domain"),t.logger.info("Not on dev domain",{vertesia:{account_id:o,project_id:a}}))),s(I(),(async e=>{e?(console.log("Auth: successful login with firebase"),t.logger.info("Successful login with firebase",{vertesia:{account_id:o,project_id:a}}),i.setSession=l,await P(o,a,void 0,!1,z()).then((e=>{i.login(e.rawToken).then((()=>l(i.clone())))})).catch((e=>{console.error("Failed to fetch user token from studio",e),t.logger.error("Failed to fetch user token from studio",{vertesia:{account_id:o,project_id:a,error:e}}),e instanceof U||i.logout(),i.isLoading=!1,i.authError=e,l(i.clone())}))):(console.log("Auth: using anonymous user"),t.logger.info("Using anonymous user",{vertesia:{account_id:o,project_id:a}}),i.client.withAuthCallback(void 0),i.logout(),l(i.clone()))}))}),[]),p(L.Provider,{value:i,children:e})}function G(){return{tagUserSession:async e=>{const t=window.localStorage.getItem("composableSignupData");e?t&&window.localStorage.removeItem("composableSignupData"):console.error("No user found -- skipping tagging")},trackEvent:(e,o)=>{t.isProd||console.debug("track event",e,o),n(_(),e,{...o,debug_mode:!t.isProd})}}}export{f as LastSelectedAccountId_KEY,w as LastSelectedProjectId_KEY,N as TypeRegistry,U as UserNotFoundError,C as UserSession,L as UserSessionContext,q as UserSessionProvider,A as fetchComposableToken,F as fetchComposableTokenFromFirebaseToken,P as getComposableToken,_ as getFirebaseAnalytics,T as getFirebaseApp,I as getFirebaseAuth,E as getFirebaseAuthToken,j as setFirebaseTenant,$ as useCurrentTenant,G as useUXTracking,D as useUserSession};
1
+ import{jwtDecode as e}from"jwt-decode";import{Env as t}from"@vertesia/ui/env";import{getAnalytics as o,logEvent as n}from"firebase/analytics";import{initializeApp as r}from"firebase/app";import{getAuth as i,onAuthStateChanged as a}from"firebase/auth";import{useState as s,useEffect as c,createContext as l,useContext as u,useCallback as h}from"react";import{useUserSession as d}from"@vertesia/ui/session";import{VertesiaClient as g}from"@vertesia/client";import{jsx as m}from"react/jsx-runtime";const p="composableai.lastSelectedAccountId",f="composableai.lastSelectedProjectId";let w,v,k=null,b=null,y=null;function S(){if(!k)try{if(!t.firebase)throw new Error("Firebase configuration is not available in the environment");k=r(t.firebase)}catch(e){throw console.error("Failed to initialize Firebase app:",e),new Error("Firebase initialization failed - environment may not be properly initialized")}return k}function T(){return b||(b=o(S())),b}function _(){return y||(y=i(S())),y}async function j(e){if(e)if(t.firebase)try{e&&console.log(`Resolving tenant ID from email: ${e}`);let o=3,n=250;for(;o>0;)try{const o=await fetch("/api/resolve-tenant",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({tenantEmail:e}),signal:AbortSignal.timeout(5e3)});if(!o)throw new Error("No response received from tenant API");if(!o.ok){try{const e=await o.json();console.error("Failed to resolve tenant ID:",e.error)}catch(e){console.error(`Failed to resolve tenant ID: HTTP ${o.status}`)}if(404===o.status)return void console.warn(`Tenant not found for ${e}`);throw new Error(`HTTP error ${o.status}`)}const n=await o.json();if(n&&n.firebaseTenantId){const e=_();return e.tenantId=n.firebaseTenantId,t.firebase.providerType=n.provider??"oidc",console.log(`Tenant ID set to ${e.tenantId}`),n}return void console.error(`Invalid response format, missing tenantId for ${e}`)}catch(e){if(!(o>1))throw e;console.warn(`Tenant resolution failed, retrying in ${n}ms...`,e),await new Promise((e=>setTimeout(e,n))),n*=2,o--}}catch(e){console.error("Error setting Firebase tenant:",e instanceof Error?e.message:"Unknown error")}else console.log("Firebase configuration is not available in the environment");else console.log("No tenant name or email specified, skipping tenant setup")}async function I(e){const o=_().currentUser;return o?o.getIdToken(e).then((n=>(t.logger.info("Got Firebase token",{vertesia:{user_email:o.email,user_name:o.displayName,user_id:o.uid,refresh:e}}),n))).catch((n=>(t.logger.error("Failed to get Firebase token",{vertesia:{user_email:o.email,user_name:o.displayName,user_id:o.uid,refresh:e,error:n}}),console.error("Failed to get access token",n),null))):(t.logger.warn("No user found"),Promise.resolve(null))}async function F(o,n,r,i){console.log(`Getting/refreshing composable token for account ${n} and project ${r} `),t.logger.info("Getting/refreshing composable token",{vertesia:{account_id:n,project_id:r}});const a=await o();if(!a)throw console.log("No id token found - using cookie auth"),new Error("No id token found");console.log("Fetching composable token from "+t.endpoints.studio),t.logger.info("Fetching composable token from"+t.endpoints.studio,{vertesia:{account_id:n,project_id:r}});const s=new URL(t.endpoints.studio+"/auth/token");n&&s.searchParams.set("accountId",n),r&&s.searchParams.set("projectId",r),i&&s.searchParams.set("ttl",String(i)),console.log(`Getting composable token for account ${n} and project ${r}`),t.logger.info("Getting composable token",{vertesia:{account_id:n,project_id:r}});const c=await fetch(s,{method:"GET",headers:{"Content-Type":"application/json",Authorization:`Bearer ${a}`}}).catch((e=>{throw localStorage.removeItem(p),localStorage.removeItem(f),console.error("Failed to get composable token",e),t.logger.error("Failed to get composable token",{vertesia:{account_id:n,project_id:r,error:e}}),new Error("Failed to get composable token")}));if(a&&412===c?.status){console.log("412: auth succeeded but user doesn't exist - signup required",c?.status),t.logger.error("412: auth succeeded but user doesn't exist - signup required",{vertesia:{account_id:n,project_id:r,status:c?.status}});const o=e(a);if(!o?.email)throw t.logger.error("No email found in id token"),new Error("No email found in id token");throw t.logger.error("User not found",{vertesia:{account_id:n,project_id:r,email:o.email}}),new P("User not found",o.email)}if(!c||!c.ok)throw console.error("Failed to get composable token",c),t.logger.error("Failed to get composable token",{vertesia:{account_id:n,project_id:r,status:c?.status}}),new Error("Failed to get composable token");const{token:l}=await c.json().catch((e=>{t.logger.error("Failed to parse composable token",{vertesia:{account_id:n,project_id:r,error:e}}),console.error("Failed to parse composable token",e)}));return l}async function E(e,t,o){return F(I,e,t,o)}async function A(o,n,r,i=!1,a=!1){const s=o??localStorage.getItem(p)??void 0,c=n??localStorage.getItem(f+"-"+s)??void 0;if(!i&&w&&v&&v.exp>Date.now()/1e3+300)return{rawToken:w,token:v,error:!1};if(!a&&_().currentUser?w=await E(s,c):(r||w)&&(w=await F((()=>Promise.resolve(r??w)),s,c)),!w)throw t.logger.error("Cannot acquire a composable token",{vertesia:{account_id:s,project_id:c}}),new Error("Cannot acquire a composable token");if(v=e(w),!v||!v.exp||!w)throw console.error("Invalid composable token",v),t.logger.error("Invalid composable token",{vertesia:{account_id:s,project_id:c}}),new Error("Invalid composable token");return{rawToken:w,token:v,error:!1}}class P extends Error{email;constructor(e,t){super(e),this.name="UserNotFoundError",this.email=t}}function $(){const{user:e}=d(),[t,o]=s(null),[n,r]=s(!0),[i,a]=s(null);return c((()=>{(async()=>{if(!e?.email)return o(null),void r(!1);try{const t=await fetch("/api/resolve-tenant",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({tenantEmail:e.email})});if(t.ok){const e=await t.json();o(e?{tenantKey:e.name||"unknown",name:e.label||e.name||"Unknown",domain:e.domain||[],firebaseTenantId:e.firebaseTenantId,provider:e.provider,logo:e.logo}:null)}else o(null)}catch(e){console.error("Error loading current tenant:",e),a("Failed to load tenant configuration"),o(null)}finally{r(!1)}})()}),[e?.email]),{currentTenant:t,isLoading:n,error:i}}class U{types;map={};constructor(e){this.types=e,e.sort(((e,t)=>e.name.localeCompare(t.name)));for(const t of e)this.map[t.id]=t}getType(e){return this.map[e]}getTypeLayout(e){const t=this.map[e];return t?t.table_layout:void 0}getTypeName(e){const t=this.map[e];return t?t.name:void 0}}class C{isLoading=!0;client;authError;authToken;typeRegistry;setSession;lastSelectedAccount;lastSelectedProject;onboardingComplete;constructor(e,o){this.client=e||new g({serverUrl:t.endpoints.studio,storeUrl:t.endpoints.zeno}),o&&(this.setSession=o),this.logout=this.logout.bind(this)}get store(){return this.client.store}get user(){return this.authToken}get account(){return this.authToken?.account}get project(){return this.authToken?.project}get accounts(){return this.authToken?.accounts}get authCallback(){return this.rawAuthToken.then((e=>`Bearer ${e}`))}get rawAuthToken(){return A().then((t=>{const o=t?.rawToken;if(!o)throw new Error("No token available");return this.authToken=e(o),o}))}signOut(){this.logout()}getAccount(){return this.authToken?.account}async login(o){return this.authError=void 0,this.isLoading=!1,this.client.withAuthCallback((()=>this.authCallback)),this.authToken=e(o),console.log(`Logging in as ${this.authToken?.name} with account ${this.authToken?.account.name} (${this.authToken?.account.id}, and project ${this.authToken?.project?.name} (${this.authToken?.project?.id})`),localStorage.setItem(p,this.authToken.account.id),localStorage.setItem(f+"-"+this.authToken.account.id,this.authToken.project?.id??""),t.onLogin?.(this.authToken),await Promise.all([this._loadTypes(),this.fetchOnboardingStatus()]),Promise.resolve()}isLoggedIn(){return!!this.authToken}logout(){console.log("Logging out"),this.authToken&&_().signOut(),this.authError=void 0,this.isLoading=!1,this.authToken=void 0,this.typeRegistry=void 0,this.setSession=void 0,this.client.withAuthCallback(void 0)}async switchAccount(e){localStorage.setItem(p,e),this&&(this.account&&this.project?localStorage.setItem(f+"-"+this.account.id,this.project.id):this.account&&localStorage.removeItem(f+"-"+this.account.id)),window.location.replace("/?a="+e)}async switchProject(e){this.account&&localStorage.setItem(f+"-"+this.account.id,e),window.location.replace("/?a="+this.account?.id+"&p="+e)}async _loadTypes(){if(this.project)return this.store.types.list({},{layout:!0}).then((e=>this.typeRegistry=new U(e))).catch((e=>{throw console.error("Failed to fetch object types",e),e}));console.log("No project selected")}async reloadTypes(){return this._loadTypes().then((()=>{this.setSession?.(this.clone())}))}async fetchAccounts(){return this.client.accounts.list().then((e=>{if(!this.authToken)throw new Error("No token available");this.authToken.accounts=e,this.setSession?.(this.clone())})).catch((e=>{throw console.error("Failed to fetch accounts",e),e}))}async fetchOnboardingStatus(){if(this.onboardingComplete)return console.log("Onboarding already completed"),!1;const e=this.onboardingComplete;try{const t=await this.client.account.onboardingProgress();if(this.onboardingComplete=Object.values(t).every((e=>!0===e)),e!==this.onboardingComplete)return!0;this.setSession?.(this.clone())}catch(e){console.error("Error fetching onboarding status:",e),this.onboardingComplete=!1,this.setSession?.(this.clone())}return!1}clone(){const e=new C(this.client);return e.isLoading=this.isLoading,e.authError=this.authError,e.authToken=this.authToken,e.setSession=this.setSession,e.lastSelectedAccount=this.lastSelectedAccount,e.switchAccount=this.switchAccount,e.typeRegistry=this.typeRegistry,e.onboardingComplete=this.onboardingComplete,e}}const N=l(void 0);function L(){const e=u(N);if(!e)throw new Error("useUserSession must be used within a UserSessionProvider");return e}const R="auth_state",D="auth_state_expiry";const O=[".composable.sh",".vertesia.dev","vertesia.app"];function x(){return!!t.isDocker||O.some((e=>window.location.hostname.endsWith(e)))}function G({children:e}){const o=new URLSearchParams(location.hash.substring(1)),n=o.get("token"),r=o.get("state"),[i,l]=s(new C),{generateState:u,verifyState:d,clearState:g}={generateState:h((()=>{const e=crypto.randomUUID(),t=Date.now()+3e5;return sessionStorage.setItem(R,e),sessionStorage.setItem(D,t.toString()),e}),[]),verifyState:h((e=>{if(!e)return"Missing state";const t=sessionStorage.getItem(R),o=parseInt(sessionStorage.getItem(D)||"0");let n;return n=t!==e?`State mismatched (${t} !== ${e})`:Date.now()>o?"State expired":void 0,n}),[]),clearState:h((()=>{sessionStorage.removeItem(R),sessionStorage.removeItem(D)}),[])},w=(e,t)=>{const o=new URL("https://internal-auth.vertesia.app/"),n=new URL(window.location.href);n.hash="",o.searchParams.set("redirect_uri",n.toString()),o.searchParams.set("state",u()),location.replace(o.toString())};return c((()=>{console.log("Auth: starting auth flow"),t.logger.info("Starting auth flow");const e=new URL(window.location.href),o=e.searchParams.get("a")??localStorage.getItem(p)??void 0,s=e.searchParams.get("p")??localStorage.getItem(f+"-"+o)??void 0;if(console.log("Auth: selected account",o),console.log("Auth: selected project",s),t.logger.info("Selected account and project",{vertesia:{account_id:o,project_id:s}}),n&&r){const e=d(r);return e?(console.error(`Auth: invalid state: ${e}`),t.logger.error(`Invalid state: ${e}`,{vertesia:{state:r}}),w()):g(),void A(o,s,n,!1,x()).then((e=>{i.login(e.rawToken).then((()=>{l(i.clone()),window.location.hash=""}))})).catch((e=>{console.error("Failed to fetch user token from studio, redirecting to central auth",e),t.logger.error("Failed to fetch user token from studio, redirecting to central auth",{vertesia:{error:e}}),w()}))}return i.isLoggedIn()||(console.log("Auth: not logged in & no token/state"),t.logger.info("Not logged in & no token/state",{vertesia:{account_id:o,project_id:s}}),x()?(console.log("Auth: on dev domain, redirecting to central auth with selection",o,s),t.logger.info("Redirecting to central auth with selection",{vertesia:{account_id:o,project_id:s}}),w()):(console.log("Auth: not on dev domain"),t.logger.info("Not on dev domain",{vertesia:{account_id:o,project_id:s}}))),a(_(),(async e=>{e?(console.log("Auth: successful login with firebase"),t.logger.info("Successful login with firebase",{vertesia:{account_id:o,project_id:s}}),i.setSession=l,await A(o,s,void 0,!1,x()).then((e=>{i.login(e.rawToken).then((()=>l(i.clone())))})).catch((e=>{console.error("Failed to fetch user token from studio",e),t.logger.error("Failed to fetch user token from studio",{vertesia:{account_id:o,project_id:s,error:e}}),e instanceof P||i.logout(),i.isLoading=!1,i.authError=e,l(i.clone())}))):(console.log("Auth: using anonymous user"),t.logger.info("Using anonymous user",{vertesia:{account_id:o,project_id:s}}),i.client.withAuthCallback(void 0),i.logout(),l(i.clone()))}))}),[]),m(N.Provider,{value:i,children:e})}function z(){return{tagUserSession:async e=>{const t=window.localStorage.getItem("composableSignupData");e?t&&window.localStorage.removeItem("composableSignupData"):console.error("No user found -- skipping tagging")},trackEvent:(e,o)=>{t.isProd||console.debug("track event",e,o),n(T(),e,{...o,debug_mode:!t.isProd})}}}export{p as LastSelectedAccountId_KEY,f as LastSelectedProjectId_KEY,U as TypeRegistry,P as UserNotFoundError,C as UserSession,N as UserSessionContext,G as UserSessionProvider,F as fetchComposableToken,E as fetchComposableTokenFromFirebaseToken,A as getComposableToken,T as getFirebaseAnalytics,S as getFirebaseApp,_ as getFirebaseAuth,I as getFirebaseAuthToken,j as setFirebaseTenant,$ as useCurrentTenant,z as useUXTracking,L as useUserSession};
2
2
  //# sourceMappingURL=vertesia-ui-session.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"vertesia-ui-session.js","sources":["esm/session/constants.js","esm/session/auth/firebase.js","esm/session/auth/composable.js","esm/session/auth/useCurrentTenant.js","esm/session/TypeRegistry.js","esm/session/UserSession.js","esm/session/auth/useAuthState.js","esm/session/UserSessionProvider.js","esm/session/useUXTracking.js"],"sourcesContent":["export const LastSelectedAccountId_KEY = 'composableai.lastSelectedAccountId';\nexport const LastSelectedProjectId_KEY = 'composableai.lastSelectedProjectId';\n//# sourceMappingURL=constants.js.map","import { Env } from \"@vertesia/ui/env\";\nimport { getAnalytics } from \"firebase/analytics\";\nimport { initializeApp } from \"firebase/app\";\nimport { getAuth } from \"firebase/auth\";\n// Use lazy initialization to avoid accessing Env before it's initialized\nlet _firebaseApp = null;\nlet _analytics = null;\nlet _firebaseAuth = null;\n// Getters that lazily initialize Firebase components when first accessed\nexport function getFirebaseApp() {\n if (!_firebaseApp) {\n try {\n if (!Env.firebase) {\n throw new Error(\"Firebase configuration is not available in the environment\");\n }\n _firebaseApp = initializeApp(Env.firebase);\n }\n catch (error) {\n console.error(\"Failed to initialize Firebase app:\", error);\n throw new Error(\"Firebase initialization failed - environment may not be properly initialized\");\n }\n }\n return _firebaseApp;\n}\nexport function getFirebaseAnalytics() {\n if (!_analytics) {\n _analytics = getAnalytics(getFirebaseApp());\n }\n return _analytics;\n}\nexport function getFirebaseAuth() {\n if (!_firebaseAuth) {\n _firebaseAuth = getAuth(getFirebaseApp());\n }\n return _firebaseAuth;\n}\nexport async function setFirebaseTenant(tenantEmail) {\n if (!tenantEmail) {\n console.log(\"No tenant name or email specified, skipping tenant setup\");\n return;\n }\n if (!Env.firebase) {\n console.log(\"Firebase configuration is not available in the environment\");\n return;\n }\n try {\n if (tenantEmail)\n console.log(`Resolving tenant ID from email: ${tenantEmail}`);\n // Add retry logic with exponential backoff\n let retries = 3;\n let retryDelay = 250; // Start with 250ms delay\n while (retries > 0) {\n try {\n // Call the API endpoint to resolve the tenant ID\n const response = await fetch(\"/api/resolve-tenant\", {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n tenantEmail: tenantEmail,\n }),\n // Add timeout to prevent hanging requests\n signal: AbortSignal.timeout(5000),\n });\n // Check for network errors\n if (!response) {\n throw new Error(\"No response received from tenant API\");\n }\n // Handle HTTP error responses\n if (!response.ok) {\n // Try to parse the error response\n try {\n const errorData = await response.json();\n console.error(\"Failed to resolve tenant ID:\", errorData.error);\n }\n catch (parseError) {\n console.error(`Failed to resolve tenant ID: HTTP ${response.status}`);\n }\n // If the error is 404 Not Found, no need to retry\n if (response.status === 404) {\n console.warn(`Tenant not found for ${tenantEmail}`);\n return;\n }\n throw new Error(`HTTP error ${response.status}`);\n }\n // Successfully got a response, parse it\n const data = (await response.json());\n if (data && data.firebaseTenantId) {\n const auth = getFirebaseAuth();\n auth.tenantId = data.firebaseTenantId;\n Env.firebase.providerType = data.provider ?? \"oidc\";\n console.log(`Tenant ID set to ${auth.tenantId}`);\n return data;\n }\n else {\n console.error(`Invalid response format, missing tenantId for ${tenantEmail}`);\n return; // No need to retry for invalid response format\n }\n }\n catch (fetchError) {\n // Only retry for network-related errors\n if (retries > 1) {\n console.warn(`Tenant resolution failed, retrying in ${retryDelay}ms...`, fetchError);\n await new Promise((resolve) => setTimeout(resolve, retryDelay));\n retryDelay *= 2; // Exponential backoff\n retries--;\n }\n else {\n throw fetchError; // Last retry failed, propagate error\n }\n }\n }\n }\n catch (error) {\n // Final error handler\n console.error(\"Error setting Firebase tenant:\", error instanceof Error ? error.message : \"Unknown error\");\n // Continue without tenant ID - authentication will work without multi-tenancy\n // but the user will access the default tenant\n }\n}\nexport async function getFirebaseAuthToken(refresh) {\n const auth = getFirebaseAuth();\n const user = auth.currentUser;\n if (user) {\n return user\n .getIdToken(refresh)\n .then((token) => {\n Env.logger.info(\"Got Firebase token\", {\n vertesia: {\n user_email: user.email,\n user_name: user.displayName,\n user_id: user.uid,\n refresh: refresh,\n },\n });\n return token;\n })\n .catch((err) => {\n Env.logger.error(\"Failed to get Firebase token\", {\n vertesia: {\n user_email: user.email,\n user_name: user.displayName,\n user_id: user.uid,\n refresh: refresh,\n error: err,\n },\n });\n console.error(\"Failed to get access token\", err);\n return null;\n });\n }\n else {\n Env.logger.warn(\"No user found\");\n return Promise.resolve(null);\n }\n}\n//# sourceMappingURL=firebase.js.map","import { jwtDecode } from \"jwt-decode\";\nimport { Env } from '@vertesia/ui/env';\nimport { LastSelectedAccountId_KEY, LastSelectedProjectId_KEY } from '../constants';\nimport { getFirebaseAuth, getFirebaseAuthToken } from './firebase';\nlet AUTH_TOKEN_RAW;\nlet AUTH_TOKEN;\nexport async function fetchComposableToken(getIdToken, accountId, projectId, ttl) {\n console.log(`Getting/refreshing composable token for account ${accountId} and project ${projectId} `);\n Env.logger.info('Getting/refreshing composable token', {\n vertesia: {\n account_id: accountId,\n project_id: projectId,\n },\n });\n const idToken = await getIdToken(); //get from firebase\n if (!idToken) {\n console.log('No id token found - using cookie auth');\n throw new Error('No id token found');\n }\n // Use STS endpoint - either configured or default to sts.vertesia.io\n const stsEndpoint = Env.endpoints.sts;\n console.log('Using STS for token generation:', stsEndpoint);\n Env.logger.info('Using STS for token generation', {\n vertesia: {\n account_id: accountId,\n project_id: projectId,\n sts_url: stsEndpoint,\n },\n });\n try {\n // Call STS to generate a user token\n const stsUrl = new URL(stsEndpoint + '/token/issue');\n const requestBody = {\n type: 'user',\n account_id: accountId,\n project_id: projectId,\n expires_at: ttl ? Math.floor(Date.now() / 1000) + ttl : undefined,\n };\n const stsRes = await fetch(stsUrl, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${idToken}` // Firebase token for authentication\n },\n body: JSON.stringify(requestBody)\n });\n if (idToken && stsRes?.status === 412) {\n console.log(\"412: auth succeeded but user doesn't exist - signup required\", stsRes?.status);\n Env.logger.error(\"412: auth succeeded but user doesn't exist - signup required\", {\n vertesia: {\n account_id: accountId,\n project_id: projectId,\n status: stsRes?.status\n },\n });\n const idTokenDecoded = jwtDecode(idToken);\n if (!idTokenDecoded?.email) {\n Env.logger.error('No email found in id token');\n throw new Error('No email found in id token');\n }\n Env.logger.error('User not found', {\n vertesia: {\n account_id: accountId,\n project_id: projectId,\n email: idTokenDecoded.email\n }\n });\n throw new UserNotFoundError('User not found', idTokenDecoded.email);\n }\n if (!stsRes.ok) {\n const errorText = await stsRes.text();\n console.error('STS token generation failed:', stsRes.status, errorText);\n Env.logger.error('STS token generation failed', {\n vertesia: {\n status: stsRes.status,\n error: errorText,\n account_id: accountId,\n project_id: projectId,\n },\n });\n throw new Error(`Failed to get token from STS: ${stsRes.status}`);\n }\n const { token } = await stsRes.json();\n console.log('Successfully got token from STS');\n Env.logger.info('Successfully got token from STS');\n return token;\n }\n catch (error) {\n if (error instanceof UserNotFoundError) {\n throw error; // Re-throw UserNotFoundError\n }\n localStorage.removeItem(LastSelectedAccountId_KEY);\n localStorage.removeItem(LastSelectedProjectId_KEY);\n console.error('Failed to get composable token from STS', error);\n Env.logger.error('Failed to get composable token from STS', {\n vertesia: {\n account_id: accountId,\n project_id: projectId,\n error: error,\n },\n });\n throw new Error('Failed to get composable token');\n }\n}\n/**\n *\n * @param accountId\n * @param projectId\n * @param ttl time to live for the token in seconds\n * @returns\n */\nexport async function fetchComposableTokenFromFirebaseToken(accountId, projectId, ttl) {\n return fetchComposableToken(getFirebaseAuthToken, accountId, projectId, ttl);\n}\nexport async function getComposableToken(accountId, projectId, initToken, forceRefresh = false, useInternalAuth = false) {\n const selectedAccount = accountId ?? localStorage.getItem(LastSelectedAccountId_KEY) ?? undefined;\n const selectedProject = projectId ?? localStorage.getItem(LastSelectedProjectId_KEY + '-' + selectedAccount) ?? undefined;\n //token is still valid for more than 5 minutes\n if (!forceRefresh && AUTH_TOKEN_RAW && AUTH_TOKEN && AUTH_TOKEN.exp > (Date.now() / 1000 + 300)) {\n return { rawToken: AUTH_TOKEN_RAW, token: AUTH_TOKEN, error: false };\n }\n //token is close to expire, refresh it\n if (!useInternalAuth && getFirebaseAuth().currentUser) {\n //we have a firebase user, get the token from there\n AUTH_TOKEN_RAW = await fetchComposableTokenFromFirebaseToken(selectedAccount, selectedProject);\n }\n else if (initToken || AUTH_TOKEN_RAW) {\n // we have a token already and no firebase user, refresh it\n AUTH_TOKEN_RAW = await fetchComposableToken(() => Promise.resolve(initToken ?? AUTH_TOKEN_RAW), selectedAccount, selectedProject);\n }\n if (!AUTH_TOKEN_RAW) {\n Env.logger.error('Cannot acquire a composable token', {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n throw new Error('Cannot acquire a composable token');\n }\n AUTH_TOKEN = jwtDecode(AUTH_TOKEN_RAW);\n if (!AUTH_TOKEN || !AUTH_TOKEN.exp || !AUTH_TOKEN_RAW) {\n console.error('Invalid composable token', AUTH_TOKEN);\n Env.logger.error('Invalid composable token', {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n throw new Error('Invalid composable token');\n }\n return { rawToken: AUTH_TOKEN_RAW, token: AUTH_TOKEN, error: false };\n}\nexport class UserNotFoundError extends Error {\n email;\n constructor(message, email) {\n super(message);\n this.name = 'UserNotFoundError';\n this.email = email;\n }\n}\n//# sourceMappingURL=composable.js.map","import { useState, useEffect } from 'react';\nimport { useUserSession } from \"@vertesia/ui/session\";\nexport function useCurrentTenant() {\n const { user } = useUserSession();\n const [currentTenant, setCurrentTenant] = useState(null);\n const [isLoading, setIsLoading] = useState(true);\n const [error, setError] = useState(null);\n useEffect(() => {\n const loadCurrentTenant = async () => {\n if (!user?.email) {\n setCurrentTenant(null);\n setIsLoading(false);\n return;\n }\n try {\n const response = await fetch('/api/resolve-tenant', {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify({\n tenantEmail: user.email\n })\n });\n if (response.ok) {\n const tenantData = await response.json();\n if (tenantData) {\n // Convert the resolved tenant data to our TenantConfig format\n setCurrentTenant({\n tenantKey: tenantData.name || 'unknown',\n name: tenantData.label || tenantData.name || 'Unknown',\n domain: tenantData.domain || [],\n firebaseTenantId: tenantData.firebaseTenantId,\n provider: tenantData.provider,\n logo: tenantData.logo\n });\n }\n else {\n setCurrentTenant(null);\n }\n }\n else {\n setCurrentTenant(null);\n }\n }\n catch (error) {\n console.error('Error loading current tenant:', error);\n setError('Failed to load tenant configuration');\n setCurrentTenant(null);\n }\n finally {\n setIsLoading(false);\n }\n };\n loadCurrentTenant();\n }, [user?.email]);\n return {\n currentTenant,\n isLoading,\n error\n };\n}\n//# sourceMappingURL=useCurrentTenant.js.map","export class TypeRegistry {\n types;\n map = {};\n constructor(types) {\n this.types = types;\n //sort types\n types.sort((a, b) => a.name.localeCompare(b.name));\n for (const type of types) {\n this.map[type.id] = type;\n }\n }\n getType(id) {\n return this.map[id];\n }\n getTypeLayout(id) {\n const type = this.map[id];\n return type ? type.table_layout : undefined;\n }\n getTypeName(id) {\n const type = this.map[id];\n return type ? type.name : undefined;\n }\n}\n//# sourceMappingURL=TypeRegistry.js.map","import { jwtDecode } from 'jwt-decode';\nimport { createContext, useContext } from 'react';\nimport { VertesiaClient } from '@vertesia/client';\nimport { Env } from '@vertesia/ui/env';\nimport { getComposableToken } from './auth/composable';\nimport { getFirebaseAuth } from './auth/firebase';\nimport { TypeRegistry } from './TypeRegistry';\nimport { LastSelectedAccountId_KEY, LastSelectedProjectId_KEY } from './constants';\nexport { LastSelectedAccountId_KEY, LastSelectedProjectId_KEY };\nclass UserSession {\n isLoading = true;\n client;\n authError;\n authToken;\n typeRegistry;\n setSession;\n lastSelectedAccount;\n lastSelectedProject;\n onboardingComplete;\n constructor(client, setSession) {\n if (client) {\n this.client = client;\n }\n else {\n this.client = new VertesiaClient({\n serverUrl: Env.endpoints.studio,\n storeUrl: Env.endpoints.zeno,\n tokenServerUrl: Env.endpoints.sts\n });\n }\n if (setSession) {\n this.setSession = setSession;\n }\n this.logout = this.logout.bind(this);\n }\n get store() {\n return this.client.store;\n }\n get user() {\n return this.authToken;\n }\n get account() {\n return this.authToken?.account;\n }\n get project() {\n return this.authToken?.project;\n }\n get accounts() {\n return this.authToken?.accounts;\n }\n get authCallback() {\n return this.rawAuthToken.then(token => `Bearer ${token}`);\n }\n get rawAuthToken() {\n return getComposableToken().then(res => {\n const token = res?.rawToken;\n if (!token) {\n throw new Error('No token available');\n }\n this.authToken = jwtDecode(token);\n return token;\n });\n }\n signOut() {\n this.logout();\n }\n getAccount() {\n return this.authToken?.account;\n }\n async login(token) {\n this.authError = undefined;\n this.isLoading = false;\n this.client.withAuthCallback(() => this.authCallback);\n this.authToken = jwtDecode(token);\n console.log(`Logging in as ${this.authToken?.name} with account ${this.authToken?.account.name} (${this.authToken?.account.id}, and project ${this.authToken?.project?.name} (${this.authToken?.project?.id})`);\n //store selected account in local storage\n localStorage.setItem(LastSelectedAccountId_KEY, this.authToken.account.id);\n localStorage.setItem(LastSelectedProjectId_KEY + '-' + this.authToken.account.id, this.authToken.project?.id ?? '');\n // notify the host app of the login\n Env.onLogin?.(this.authToken);\n // Independent async calls\n await Promise.all([\n this._loadTypes(),\n this.fetchOnboardingStatus(),\n ]);\n return Promise.resolve();\n }\n isLoggedIn() {\n return !!this.authToken;\n }\n logout() {\n console.log('Logging out');\n if (this.authToken) {\n getFirebaseAuth().signOut();\n }\n this.authError = undefined;\n this.isLoading = false;\n this.authToken = undefined;\n this.typeRegistry = undefined;\n this.setSession = undefined;\n this.client.withAuthCallback(undefined);\n }\n async switchAccount(targetAccountId) {\n localStorage.setItem(LastSelectedAccountId_KEY, targetAccountId);\n if (this) {\n if (this.account && this.project) {\n localStorage.setItem(LastSelectedProjectId_KEY + '-' + this.account.id, this.project.id);\n }\n else if (this.account) {\n localStorage.removeItem(LastSelectedProjectId_KEY + '-' + this.account.id);\n }\n }\n window.location.replace('/?a=' + targetAccountId);\n }\n async switchProject(targetProjectId) {\n if (this.account) {\n localStorage.setItem(LastSelectedProjectId_KEY + '-' + this.account.id, targetProjectId);\n }\n window.location.replace('/?a=' + this.account?.id + '&p=' + targetProjectId);\n }\n async _loadTypes() {\n if (this.project) {\n return this.store.types.list({}, { layout: true }).then(types => this.typeRegistry = new TypeRegistry(types)).catch(err => {\n console.error('Failed to fetch object types', err);\n throw err;\n });\n }\n else {\n console.log('No project selected');\n }\n }\n async reloadTypes() {\n return this._loadTypes().then(() => {\n this.setSession?.(this.clone());\n });\n }\n async fetchAccounts() {\n return this.client.accounts.list().then(accounts => {\n if (!this.authToken) {\n throw new Error('No token available');\n }\n this.authToken.accounts = accounts;\n this.setSession?.(this.clone());\n }).catch(err => {\n console.error('Failed to fetch accounts', err);\n throw err;\n });\n }\n async fetchOnboardingStatus() {\n if (this.onboardingComplete) {\n console.log('Onboarding already completed');\n return false;\n }\n const previousStatus = this.onboardingComplete;\n try {\n const onboarding = await this.client.account.onboardingProgress();\n this.onboardingComplete = Object.values(onboarding).every(value => value === true);\n if (previousStatus !== this.onboardingComplete) {\n return true;\n }\n this.setSession?.(this.clone());\n }\n catch (error) {\n console.error('Error fetching onboarding status:', error);\n this.onboardingComplete = false;\n this.setSession?.(this.clone());\n }\n return false;\n }\n clone() {\n const session = new UserSession(this.client);\n session.isLoading = this.isLoading;\n session.authError = this.authError;\n session.authToken = this.authToken;\n session.setSession = this.setSession;\n session.lastSelectedAccount = this.lastSelectedAccount;\n session.switchAccount = this.switchAccount;\n session.typeRegistry = this.typeRegistry;\n session.onboardingComplete = this.onboardingComplete;\n return session;\n }\n}\nconst UserSessionContext = createContext(undefined);\nexport function useUserSession() {\n const session = useContext(UserSessionContext);\n if (!session) {\n throw new Error('useUserSession must be used within a UserSessionProvider');\n }\n return session;\n}\nexport { UserSession, UserSessionContext };\n//# sourceMappingURL=UserSession.js.map","/**\n * This hook is used to generate and verify state for OAuth2 authorization requests.\n * @returns\n */\nimport { useCallback } from \"react\";\nconst AUTH_STATE_KEY = 'auth_state';\nconst STATE_EXPIRY_KEY = 'auth_state_expiry';\nconst STATE_TTL = 5 * 60 * 1000; // 5 min\nexport function useAuthState() {\n // Generate new state\n const generateState = useCallback(() => {\n const state = crypto.randomUUID();\n const expiryTime = Date.now() + STATE_TTL;\n // Store state and expiry\n sessionStorage.setItem(AUTH_STATE_KEY, state);\n sessionStorage.setItem(STATE_EXPIRY_KEY, expiryTime.toString());\n return state;\n }, []);\n // Verify returned state\n const verifyState = useCallback((returnedState) => {\n if (!returnedState) {\n return 'Missing state';\n }\n const savedState = sessionStorage.getItem(AUTH_STATE_KEY);\n const expiryTime = parseInt(sessionStorage.getItem(STATE_EXPIRY_KEY) || '0');\n let reason;\n // Verify state matches and hasn't expired\n if (savedState !== returnedState) {\n reason = `State mismatched (${savedState} !== ${returnedState})`;\n }\n else if (Date.now() > expiryTime) {\n reason = 'State expired';\n }\n else {\n reason = undefined; // No errors\n }\n return reason;\n }, []);\n // Clear state (useful for cleanup)\n const clearState = useCallback(() => {\n sessionStorage.removeItem(AUTH_STATE_KEY);\n sessionStorage.removeItem(STATE_EXPIRY_KEY);\n }, []);\n return { generateState, verifyState, clearState };\n}\n//# sourceMappingURL=useAuthState.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { onAuthStateChanged } from \"firebase/auth\";\nimport { useEffect, useRef, useState } from \"react\";\nimport { UserNotFoundError, getComposableToken } from \"./auth/composable\";\nimport { getFirebaseAuth } from \"./auth/firebase\";\nimport { useAuthState } from \"./auth/useAuthState\";\nimport { Env } from \"@vertesia/ui/env\";\nimport { LastSelectedAccountId_KEY, LastSelectedProjectId_KEY, UserSession, UserSessionContext } from \"./UserSession\";\nconst devDomains = [\".composable.sh\", \".vertesia.dev\", \"vertesia.app\"];\nconst CENTRAL_AUTH_REDIRECT = \"https://internal-auth.vertesia.app/?sts=https://sts-staging.vertesia.io\";\nfunction shouldRedirectToCentralAuth() {\n // Authentication is not supported in Docker environment.\n // See https://github.com/vertesia/studio/wiki/Composable-UI-Hosting-Options\n if (Env.isDocker) {\n return true;\n }\n return devDomains.some((domain) => window.location.hostname.endsWith(domain));\n}\nexport function UserSessionProvider({ children }) {\n const hashParams = new URLSearchParams(location.hash.substring(1));\n const token = hashParams.get(\"token\");\n const state = hashParams.get(\"state\");\n const [session, setSession] = useState(new UserSession());\n const { generateState, verifyState, clearState } = useAuthState();\n const hasInitiatedAuthRef = useRef(false);\n const redirectToCentralAuth = (projectId, accountId) => {\n const url = new URL(CENTRAL_AUTH_REDIRECT);\n const currentUrl = new URL(window.location.href);\n currentUrl.hash = \"\";\n if (projectId)\n currentUrl.searchParams.set(\"p\", projectId);\n if (accountId)\n currentUrl.searchParams.set(\"a\", accountId);\n url.searchParams.set(\"redirect_uri\", currentUrl.toString());\n url.searchParams.set(\"state\", generateState());\n location.replace(url.toString());\n };\n useEffect(() => {\n // Make this effect idempotent - only run auth flow once\n if (hasInitiatedAuthRef.current) {\n console.log(\"Auth: skipping duplicate auth flow initiation\");\n return;\n }\n hasInitiatedAuthRef.current = true;\n console.log(\"Auth: starting auth flow\");\n Env.logger.info(\"Starting auth flow\");\n const currentUrl = new URL(window.location.href);\n const selectedAccount = currentUrl.searchParams.get(\"a\") ?? localStorage.getItem(LastSelectedAccountId_KEY) ?? undefined;\n const selectedProject = currentUrl.searchParams.get(\"p\") ??\n localStorage.getItem(LastSelectedProjectId_KEY + \"-\" + selectedAccount) ??\n undefined;\n console.log(\"Auth: selected account\", selectedAccount);\n console.log(\"Auth: selected project\", selectedProject);\n Env.logger.info(\"Selected account and project\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n if (token && state) {\n const validationError = verifyState(state);\n if (validationError) {\n console.error(`Auth: invalid state: ${validationError}`);\n Env.logger.error(`Invalid state: ${validationError}`, {\n vertesia: {\n state: state,\n },\n });\n redirectToCentralAuth();\n }\n else {\n clearState();\n }\n getComposableToken(selectedAccount, selectedProject, token, false, shouldRedirectToCentralAuth())\n .then((res) => {\n session.login(res.rawToken).then(() => {\n setSession(session.clone());\n //cleanup the hash\n window.location.hash = \"\";\n });\n })\n .catch((err) => {\n console.error(\"Failed to fetch user token from studio, redirecting to central auth\", err);\n Env.logger.error(\"Failed to fetch user token from studio, redirecting to central auth\", {\n vertesia: {\n error: err,\n },\n });\n redirectToCentralAuth();\n });\n return;\n }\n else {\n //if on a dev domain and not logged in, redirect to central auth\n if (!session.isLoggedIn()) {\n console.log(\"Auth: not logged in & no token/state\");\n Env.logger.info(\"Not logged in & no token/state\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n if (shouldRedirectToCentralAuth()) {\n console.log(\"Auth: on dev domain, redirecting to central auth with selection\", selectedAccount, selectedProject);\n Env.logger.info(\"Redirecting to central auth with selection\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n redirectToCentralAuth();\n }\n else {\n console.log(\"Auth: not on dev domain\");\n Env.logger.info(\"Not on dev domain\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n }\n }\n }\n return onAuthStateChanged(getFirebaseAuth(), async (firebaseUser) => {\n if (firebaseUser) {\n console.log(\"Auth: successful login with firebase\");\n Env.logger.info(\"Successful login with firebase\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n session.setSession = setSession;\n await getComposableToken(selectedAccount, selectedProject, undefined, false, shouldRedirectToCentralAuth())\n .then((res) => {\n session.login(res.rawToken).then(() => setSession(session.clone()));\n })\n .catch((err) => {\n console.error(\"Failed to fetch user token from studio\", err);\n Env.logger.error(\"Failed to fetch user token from studio\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n error: err,\n },\n });\n if (!(err instanceof UserNotFoundError))\n session.logout();\n session.isLoading = false;\n session.authError = err;\n setSession(session.clone());\n });\n }\n else {\n // anonymous user\n console.log(\"Auth: using anonymous user\");\n Env.logger.info(\"Using anonymous user\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n session.client.withAuthCallback(undefined);\n session.logout();\n setSession(session.clone());\n }\n });\n }, []);\n return _jsx(UserSessionContext.Provider, { value: session, children: children });\n}\n//# sourceMappingURL=UserSessionProvider.js.map","import { Env } from '@vertesia/ui/env';\nimport { logEvent } from \"firebase/analytics\";\nimport { getFirebaseAnalytics } from \"./auth/firebase\";\nexport function useUXTracking() {\n //identify user in monitoring and UX systems\n const tagUserSession = async (user) => {\n const signupData = window.localStorage.getItem(\"composableSignupData\");\n if (!user) {\n console.error('No user found -- skipping tagging');\n return;\n }\n if (signupData) {\n window.localStorage.removeItem(\"composableSignupData\");\n }\n };\n //send event to analytics and UX systems\n const trackEvent = (eventName, eventProperties) => {\n if (!Env.isProd) {\n console.debug('track event', eventName, eventProperties);\n }\n //GA via firebase\n logEvent(getFirebaseAnalytics(), eventName, { ...eventProperties, debug_mode: !Env.isProd });\n };\n return {\n tagUserSession,\n trackEvent\n };\n}\n//# sourceMappingURL=useUXTracking.js.map"],"names":["LastSelectedAccountId_KEY","LastSelectedProjectId_KEY","AUTH_TOKEN_RAW","AUTH_TOKEN","_firebaseApp","_analytics","_firebaseAuth","getFirebaseApp","Env","firebase","Error","initializeApp","error","console","getFirebaseAnalytics","getAnalytics","getFirebaseAuth","getAuth","async","setFirebaseTenant","tenantEmail","log","retries","retryDelay","response","fetch","method","headers","body","JSON","stringify","signal","AbortSignal","timeout","ok","errorData","json","parseError","status","warn","data","firebaseTenantId","auth","tenantId","providerType","provider","fetchError","Promise","resolve","setTimeout","message","getFirebaseAuthToken","refresh","user","currentUser","getIdToken","then","token","logger","info","vertesia","user_email","email","user_name","displayName","user_id","uid","catch","err","fetchComposableToken","accountId","projectId","ttl","account_id","project_id","idToken","stsEndpoint","endpoints","sts","sts_url","stsUrl","URL","requestBody","type","expires_at","Math","floor","Date","now","undefined","stsRes","Authorization","idTokenDecoded","jwtDecode","UserNotFoundError","errorText","text","localStorage","removeItem","fetchComposableTokenFromFirebaseToken","getComposableToken","initToken","forceRefresh","useInternalAuth","selectedAccount","getItem","selectedProject","exp","rawToken","constructor","super","this","name","useCurrentTenant","useUserSession","currentTenant","setCurrentTenant","useState","isLoading","setIsLoading","setError","useEffect","tenantData","tenantKey","label","domain","logo","loadCurrentTenant","TypeRegistry","types","map","sort","a","b","localeCompare","id","getType","getTypeLayout","table_layout","getTypeName","UserSession","client","authError","authToken","typeRegistry","setSession","lastSelectedAccount","lastSelectedProject","onboardingComplete","VertesiaClient","serverUrl","studio","storeUrl","zeno","tokenServerUrl","logout","bind","store","account","project","accounts","authCallback","rawAuthToken","res","signOut","getAccount","login","withAuthCallback","setItem","onLogin","all","_loadTypes","fetchOnboardingStatus","isLoggedIn","switchAccount","targetAccountId","window","location","replace","switchProject","targetProjectId","list","layout","reloadTypes","clone","fetchAccounts","previousStatus","onboarding","onboardingProgress","Object","values","every","value","session","UserSessionContext","createContext","useContext","AUTH_STATE_KEY","STATE_EXPIRY_KEY","devDomains","shouldRedirectToCentralAuth","isDocker","some","hostname","endsWith","UserSessionProvider","children","hashParams","URLSearchParams","hash","substring","get","state","generateState","verifyState","clearState","useCallback","crypto","randomUUID","expiryTime","sessionStorage","toString","returnedState","savedState","parseInt","reason","hasInitiatedAuthRef","useRef","redirectToCentralAuth","url","currentUrl","href","searchParams","set","current","validationError","onAuthStateChanged","firebaseUser","_jsx","Provider","useUXTracking","tagUserSession","signupData","trackEvent","eventName","eventProperties","isProd","debug","logEvent","debug_mode"],"mappings":"2fAAY,MAACA,EAA4B,qCAC5BC,EAA4B,qCCIzC,ICDIC,EACAC,EDAAC,EAAe,KACfC,EAAa,KACbC,EAAgB,KAEb,SAASC,IACZ,IAAKH,EACD,IACI,IAAKI,EAAIC,SACL,MAAM,IAAIC,MAAM,8DAEpBN,EAAeO,EAAcH,EAAIC,SACrC,CACA,MAAOG,GAEH,MADAC,QAAQD,MAAM,qCAAsCA,GAC9C,IAAIF,MAAM,+EACpB,CAEJ,OAAON,CACX,CACO,SAASU,IAIZ,OAHKT,IACDA,EAAaU,EAAaR,MAEvBF,CACX,CACO,SAASW,IAIZ,OAHKV,IACDA,EAAgBW,EAAQV,MAErBD,CACX,CACOY,eAAeC,EAAkBC,GACpC,GAAKA,EAIL,GAAKZ,EAAIC,SAIT,IACQW,GACAP,QAAQQ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,OAAOC,KAAK,iCAAkC,CAC9CC,SAAU,CACNa,WAAY+B,EACZ9B,WAAYgC,KAGpBuF,EAAQhD,WAAaA,QACf7C,EAAmBI,EAAiBE,OAAiBjB,GAAW,EAAO+G,KACxEhJ,MAAM2G,IACP8B,EAAQ3B,MAAMH,EAAIvD,UAAUpD,MAAK,IAAMyF,EAAWgD,EAAQT,cAEzDrH,OAAOC,IACRvD,QAAQD,MAAM,yCAA0CwD,GACxD5D,EAAIkD,OAAO9C,MAAM,yCAA0C,CACvDgD,SAAU,CACNa,WAAY+B,EACZ9B,WAAYgC,EACZ9F,MAAOwD,KAGTA,aAAe0B,GACjBmG,EAAQtC,SACZsC,EAAQ3E,WAAY,EACpB2E,EAAQnD,UAAY1E,EACpB6E,EAAWgD,EAAQT,cAKvB3K,QAAQQ,IAAI,8BACZb,EAAIkD,OAAOC,KAAK,uBAAwB,CACpCC,SAAU,CACNa,WAAY+B,EACZ9B,WAAYgC,KAGpBuF,EAAQpD,OAAO0B,sBAAiB9E,GAChCwG,EAAQtC,SACRV,EAAWgD,EAAQT,eAG5B,IACIsD,EAAK5C,EAAmB6C,SAAU,CAAE/C,MAAOC,EAASa,SAAUA,GACzE,CCtKO,SAASkC,IAoBZ,MAAO,CACHC,eAnBmB/N,MAAOmC,IAC1B,MAAM6L,EAAalE,OAAO/E,aAAaQ,QAAQ,wBAC1CpD,EAID6L,GACAlE,OAAO/E,aAAaC,WAAW,wBAJ/BrF,QAAQD,MAAM,sCAiBlBuO,WATe,CAACC,EAAWC,KACtB7O,EAAI8O,QACLzO,QAAQ0O,MAAM,cAAeH,EAAWC,GAG5CG,EAAS1O,IAAwBsO,EAAW,IAAKC,EAAiBI,YAAajP,EAAI8O,UAM3F"}
1
+ {"version":3,"file":"vertesia-ui-session.js","sources":["esm/session/constants.js","esm/session/auth/firebase.js","esm/session/auth/composable.js","esm/session/auth/useCurrentTenant.js","esm/session/TypeRegistry.js","esm/session/UserSession.js","esm/session/auth/useAuthState.js","esm/session/UserSessionProvider.js","esm/session/useUXTracking.js"],"sourcesContent":["export const LastSelectedAccountId_KEY = 'composableai.lastSelectedAccountId';\nexport const LastSelectedProjectId_KEY = 'composableai.lastSelectedProjectId';\n//# sourceMappingURL=constants.js.map","import { Env } from \"@vertesia/ui/env\";\nimport { getAnalytics } from \"firebase/analytics\";\nimport { initializeApp } from \"firebase/app\";\nimport { getAuth } from \"firebase/auth\";\n// Use lazy initialization to avoid accessing Env before it's initialized\nlet _firebaseApp = null;\nlet _analytics = null;\nlet _firebaseAuth = null;\n// Getters that lazily initialize Firebase components when first accessed\nexport function getFirebaseApp() {\n if (!_firebaseApp) {\n try {\n if (!Env.firebase) {\n throw new Error(\"Firebase configuration is not available in the environment\");\n }\n _firebaseApp = initializeApp(Env.firebase);\n }\n catch (error) {\n console.error(\"Failed to initialize Firebase app:\", error);\n throw new Error(\"Firebase initialization failed - environment may not be properly initialized\");\n }\n }\n return _firebaseApp;\n}\nexport function getFirebaseAnalytics() {\n if (!_analytics) {\n _analytics = getAnalytics(getFirebaseApp());\n }\n return _analytics;\n}\nexport function getFirebaseAuth() {\n if (!_firebaseAuth) {\n _firebaseAuth = getAuth(getFirebaseApp());\n }\n return _firebaseAuth;\n}\nexport async function setFirebaseTenant(tenantEmail) {\n if (!tenantEmail) {\n console.log(\"No tenant name or email specified, skipping tenant setup\");\n return;\n }\n if (!Env.firebase) {\n console.log(\"Firebase configuration is not available in the environment\");\n return;\n }\n try {\n if (tenantEmail)\n console.log(`Resolving tenant ID from email: ${tenantEmail}`);\n // Add retry logic with exponential backoff\n let retries = 3;\n let retryDelay = 250; // Start with 250ms delay\n while (retries > 0) {\n try {\n // Call the API endpoint to resolve the tenant ID\n const response = await fetch(\"/api/resolve-tenant\", {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n tenantEmail: tenantEmail,\n }),\n // Add timeout to prevent hanging requests\n signal: AbortSignal.timeout(5000),\n });\n // Check for network errors\n if (!response) {\n throw new Error(\"No response received from tenant API\");\n }\n // Handle HTTP error responses\n if (!response.ok) {\n // Try to parse the error response\n try {\n const errorData = await response.json();\n console.error(\"Failed to resolve tenant ID:\", errorData.error);\n }\n catch (parseError) {\n console.error(`Failed to resolve tenant ID: HTTP ${response.status}`);\n }\n // If the error is 404 Not Found, no need to retry\n if (response.status === 404) {\n console.warn(`Tenant not found for ${tenantEmail}`);\n return;\n }\n throw new Error(`HTTP error ${response.status}`);\n }\n // Successfully got a response, parse it\n const data = (await response.json());\n if (data && data.firebaseTenantId) {\n const auth = getFirebaseAuth();\n auth.tenantId = data.firebaseTenantId;\n Env.firebase.providerType = data.provider ?? \"oidc\";\n console.log(`Tenant ID set to ${auth.tenantId}`);\n return data;\n }\n else {\n console.error(`Invalid response format, missing tenantId for ${tenantEmail}`);\n return; // No need to retry for invalid response format\n }\n }\n catch (fetchError) {\n // Only retry for network-related errors\n if (retries > 1) {\n console.warn(`Tenant resolution failed, retrying in ${retryDelay}ms...`, fetchError);\n await new Promise((resolve) => setTimeout(resolve, retryDelay));\n retryDelay *= 2; // Exponential backoff\n retries--;\n }\n else {\n throw fetchError; // Last retry failed, propagate error\n }\n }\n }\n }\n catch (error) {\n // Final error handler\n console.error(\"Error setting Firebase tenant:\", error instanceof Error ? error.message : \"Unknown error\");\n // Continue without tenant ID - authentication will work without multi-tenancy\n // but the user will access the default tenant\n }\n}\nexport async function getFirebaseAuthToken(refresh) {\n const auth = getFirebaseAuth();\n const user = auth.currentUser;\n if (user) {\n return user\n .getIdToken(refresh)\n .then((token) => {\n Env.logger.info(\"Got Firebase token\", {\n vertesia: {\n user_email: user.email,\n user_name: user.displayName,\n user_id: user.uid,\n refresh: refresh,\n },\n });\n return token;\n })\n .catch((err) => {\n Env.logger.error(\"Failed to get Firebase token\", {\n vertesia: {\n user_email: user.email,\n user_name: user.displayName,\n user_id: user.uid,\n refresh: refresh,\n error: err,\n },\n });\n console.error(\"Failed to get access token\", err);\n return null;\n });\n }\n else {\n Env.logger.warn(\"No user found\");\n return Promise.resolve(null);\n }\n}\n//# sourceMappingURL=firebase.js.map","import { jwtDecode } from \"jwt-decode\";\nimport { Env } from '@vertesia/ui/env';\nimport { LastSelectedAccountId_KEY, LastSelectedProjectId_KEY } from '../constants';\nimport { getFirebaseAuth, getFirebaseAuthToken } from './firebase';\nlet AUTH_TOKEN_RAW;\nlet AUTH_TOKEN;\nexport async function fetchComposableToken(getIdToken, accountId, projectId, ttl) {\n console.log(`Getting/refreshing composable token for account ${accountId} and project ${projectId} `);\n Env.logger.info('Getting/refreshing composable token', {\n vertesia: {\n account_id: accountId,\n project_id: projectId,\n },\n });\n const idToken = await getIdToken(); //get from firebase\n if (!idToken) {\n console.log('No id token found - using cookie auth');\n throw new Error('No id token found');\n }\n console.log('Fetching composable token from ' + Env.endpoints.studio);\n Env.logger.info('Fetching composable token from' + Env.endpoints.studio, {\n vertesia: {\n account_id: accountId,\n project_id: projectId,\n },\n });\n const url = new URL(Env.endpoints.studio + '/auth/token');\n if (accountId)\n url.searchParams.set('accountId', accountId);\n if (projectId)\n url.searchParams.set('projectId', projectId);\n if (ttl)\n url.searchParams.set('ttl', String(ttl));\n console.log(`Getting composable token for account ${accountId} and project ${projectId}`);\n Env.logger.info('Getting composable token', {\n vertesia: {\n account_id: accountId,\n project_id: projectId,\n },\n });\n const res = await fetch(url, {\n method: 'GET',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${idToken}`\n }\n }).catch(err => {\n localStorage.removeItem(LastSelectedAccountId_KEY);\n localStorage.removeItem(LastSelectedProjectId_KEY);\n console.error('Failed to get composable token', err);\n Env.logger.error('Failed to get composable token', {\n vertesia: {\n account_id: accountId,\n project_id: projectId,\n error: err,\n },\n });\n throw new Error('Failed to get composable token');\n });\n if (idToken && res?.status === 412) {\n console.log(\"412: auth succeeded but user doesn't exist - signup required\", res?.status);\n Env.logger.error(\"412: auth succeeded but user doesn't exist - signup required\", {\n vertesia: {\n account_id: accountId,\n project_id: projectId,\n status: res?.status\n },\n });\n const idTokenDecoded = jwtDecode(idToken);\n if (!idTokenDecoded?.email) {\n Env.logger.error('No email found in id token');\n throw new Error('No email found in id token');\n }\n Env.logger.error('User not found', {\n vertesia: {\n account_id: accountId,\n project_id: projectId,\n email: idTokenDecoded.email\n }\n });\n throw new UserNotFoundError('User not found', idTokenDecoded.email);\n }\n if (!res || !res.ok) {\n console.error('Failed to get composable token', res);\n Env.logger.error('Failed to get composable token', {\n vertesia: {\n account_id: accountId,\n project_id: projectId,\n status: res?.status,\n },\n });\n throw new Error('Failed to get composable token');\n }\n const { token } = await res.json().catch(err => {\n Env.logger.error('Failed to parse composable token', {\n vertesia: {\n account_id: accountId,\n project_id: projectId,\n error: err,\n },\n });\n console.error('Failed to parse composable token', err);\n });\n return token;\n}\n/**\n *\n * @param accountId\n * @param projectId\n * @param ttl time to live for the token in seconds\n * @returns\n */\nexport async function fetchComposableTokenFromFirebaseToken(accountId, projectId, ttl) {\n return fetchComposableToken(getFirebaseAuthToken, accountId, projectId, ttl);\n}\nexport async function getComposableToken(accountId, projectId, initToken, forceRefresh = false, useInternalAuth = false) {\n const selectedAccount = accountId ?? localStorage.getItem(LastSelectedAccountId_KEY) ?? undefined;\n const selectedProject = projectId ?? localStorage.getItem(LastSelectedProjectId_KEY + '-' + selectedAccount) ?? undefined;\n //token is still valid for more than 5 minutes\n if (!forceRefresh && AUTH_TOKEN_RAW && AUTH_TOKEN && AUTH_TOKEN.exp > (Date.now() / 1000 + 300)) {\n return { rawToken: AUTH_TOKEN_RAW, token: AUTH_TOKEN, error: false };\n }\n //token is close to expire, refresh it\n if (!useInternalAuth && getFirebaseAuth().currentUser) {\n //we have a firebase user, get the token from there\n AUTH_TOKEN_RAW = await fetchComposableTokenFromFirebaseToken(selectedAccount, selectedProject);\n }\n else if (initToken || AUTH_TOKEN_RAW) {\n // we have a token already and no firebase user, refresh it\n AUTH_TOKEN_RAW = await fetchComposableToken(() => Promise.resolve(initToken ?? AUTH_TOKEN_RAW), selectedAccount, selectedProject);\n }\n if (!AUTH_TOKEN_RAW) {\n Env.logger.error('Cannot acquire a composable token', {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n throw new Error('Cannot acquire a composable token');\n }\n AUTH_TOKEN = jwtDecode(AUTH_TOKEN_RAW);\n if (!AUTH_TOKEN || !AUTH_TOKEN.exp || !AUTH_TOKEN_RAW) {\n console.error('Invalid composable token', AUTH_TOKEN);\n Env.logger.error('Invalid composable token', {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n throw new Error('Invalid composable token');\n }\n return { rawToken: AUTH_TOKEN_RAW, token: AUTH_TOKEN, error: false };\n}\nexport class UserNotFoundError extends Error {\n email;\n constructor(message, email) {\n super(message);\n this.name = 'UserNotFoundError';\n this.email = email;\n }\n}\n//# sourceMappingURL=composable.js.map","import { useState, useEffect } from 'react';\nimport { useUserSession } from \"@vertesia/ui/session\";\nexport function useCurrentTenant() {\n const { user } = useUserSession();\n const [currentTenant, setCurrentTenant] = useState(null);\n const [isLoading, setIsLoading] = useState(true);\n const [error, setError] = useState(null);\n useEffect(() => {\n const loadCurrentTenant = async () => {\n if (!user?.email) {\n setCurrentTenant(null);\n setIsLoading(false);\n return;\n }\n try {\n const response = await fetch('/api/resolve-tenant', {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify({\n tenantEmail: user.email\n })\n });\n if (response.ok) {\n const tenantData = await response.json();\n if (tenantData) {\n // Convert the resolved tenant data to our TenantConfig format\n setCurrentTenant({\n tenantKey: tenantData.name || 'unknown',\n name: tenantData.label || tenantData.name || 'Unknown',\n domain: tenantData.domain || [],\n firebaseTenantId: tenantData.firebaseTenantId,\n provider: tenantData.provider,\n logo: tenantData.logo\n });\n }\n else {\n setCurrentTenant(null);\n }\n }\n else {\n setCurrentTenant(null);\n }\n }\n catch (error) {\n console.error('Error loading current tenant:', error);\n setError('Failed to load tenant configuration');\n setCurrentTenant(null);\n }\n finally {\n setIsLoading(false);\n }\n };\n loadCurrentTenant();\n }, [user?.email]);\n return {\n currentTenant,\n isLoading,\n error\n };\n}\n//# sourceMappingURL=useCurrentTenant.js.map","export class TypeRegistry {\n types;\n map = {};\n constructor(types) {\n this.types = types;\n //sort types\n types.sort((a, b) => a.name.localeCompare(b.name));\n for (const type of types) {\n this.map[type.id] = type;\n }\n }\n getType(id) {\n return this.map[id];\n }\n getTypeLayout(id) {\n const type = this.map[id];\n return type ? type.table_layout : undefined;\n }\n getTypeName(id) {\n const type = this.map[id];\n return type ? type.name : undefined;\n }\n}\n//# sourceMappingURL=TypeRegistry.js.map","import { jwtDecode } from 'jwt-decode';\nimport { createContext, useContext } from 'react';\nimport { VertesiaClient } from '@vertesia/client';\nimport { Env } from '@vertesia/ui/env';\nimport { getComposableToken } from './auth/composable';\nimport { getFirebaseAuth } from './auth/firebase';\nimport { TypeRegistry } from './TypeRegistry';\nimport { LastSelectedAccountId_KEY, LastSelectedProjectId_KEY } from './constants';\nexport { LastSelectedAccountId_KEY, LastSelectedProjectId_KEY };\nclass UserSession {\n isLoading = true;\n client;\n authError;\n authToken;\n typeRegistry;\n setSession;\n lastSelectedAccount;\n lastSelectedProject;\n onboardingComplete;\n constructor(client, setSession) {\n if (client) {\n this.client = client;\n }\n else {\n this.client = new VertesiaClient({\n serverUrl: Env.endpoints.studio,\n storeUrl: Env.endpoints.zeno,\n });\n }\n if (setSession) {\n this.setSession = setSession;\n }\n this.logout = this.logout.bind(this);\n }\n get store() {\n return this.client.store;\n }\n get user() {\n return this.authToken;\n }\n get account() {\n return this.authToken?.account;\n }\n get project() {\n return this.authToken?.project;\n }\n get accounts() {\n return this.authToken?.accounts;\n }\n get authCallback() {\n return this.rawAuthToken.then(token => `Bearer ${token}`);\n }\n get rawAuthToken() {\n return getComposableToken().then(res => {\n const token = res?.rawToken;\n if (!token) {\n throw new Error('No token available');\n }\n this.authToken = jwtDecode(token);\n return token;\n });\n }\n signOut() {\n this.logout();\n }\n getAccount() {\n return this.authToken?.account;\n }\n async login(token) {\n this.authError = undefined;\n this.isLoading = false;\n this.client.withAuthCallback(() => this.authCallback);\n this.authToken = jwtDecode(token);\n console.log(`Logging in as ${this.authToken?.name} with account ${this.authToken?.account.name} (${this.authToken?.account.id}, and project ${this.authToken?.project?.name} (${this.authToken?.project?.id})`);\n //store selected account in local storage\n localStorage.setItem(LastSelectedAccountId_KEY, this.authToken.account.id);\n localStorage.setItem(LastSelectedProjectId_KEY + '-' + this.authToken.account.id, this.authToken.project?.id ?? '');\n // notify the host app of the login\n Env.onLogin?.(this.authToken);\n // Independent async calls\n await Promise.all([\n this._loadTypes(),\n this.fetchOnboardingStatus(),\n ]);\n return Promise.resolve();\n }\n isLoggedIn() {\n return !!this.authToken;\n }\n logout() {\n console.log('Logging out');\n if (this.authToken) {\n getFirebaseAuth().signOut();\n }\n this.authError = undefined;\n this.isLoading = false;\n this.authToken = undefined;\n this.typeRegistry = undefined;\n this.setSession = undefined;\n this.client.withAuthCallback(undefined);\n }\n async switchAccount(targetAccountId) {\n localStorage.setItem(LastSelectedAccountId_KEY, targetAccountId);\n if (this) {\n if (this.account && this.project) {\n localStorage.setItem(LastSelectedProjectId_KEY + '-' + this.account.id, this.project.id);\n }\n else if (this.account) {\n localStorage.removeItem(LastSelectedProjectId_KEY + '-' + this.account.id);\n }\n }\n window.location.replace('/?a=' + targetAccountId);\n }\n async switchProject(targetProjectId) {\n if (this.account) {\n localStorage.setItem(LastSelectedProjectId_KEY + '-' + this.account.id, targetProjectId);\n }\n window.location.replace('/?a=' + this.account?.id + '&p=' + targetProjectId);\n }\n async _loadTypes() {\n if (this.project) {\n return this.store.types.list({}, { layout: true }).then(types => this.typeRegistry = new TypeRegistry(types)).catch(err => {\n console.error('Failed to fetch object types', err);\n throw err;\n });\n }\n else {\n console.log('No project selected');\n }\n }\n async reloadTypes() {\n return this._loadTypes().then(() => {\n this.setSession?.(this.clone());\n });\n }\n async fetchAccounts() {\n return this.client.accounts.list().then(accounts => {\n if (!this.authToken) {\n throw new Error('No token available');\n }\n this.authToken.accounts = accounts;\n this.setSession?.(this.clone());\n }).catch(err => {\n console.error('Failed to fetch accounts', err);\n throw err;\n });\n }\n async fetchOnboardingStatus() {\n if (this.onboardingComplete) {\n console.log('Onboarding already completed');\n return false;\n }\n const previousStatus = this.onboardingComplete;\n try {\n const onboarding = await this.client.account.onboardingProgress();\n this.onboardingComplete = Object.values(onboarding).every(value => value === true);\n if (previousStatus !== this.onboardingComplete) {\n return true;\n }\n this.setSession?.(this.clone());\n }\n catch (error) {\n console.error('Error fetching onboarding status:', error);\n this.onboardingComplete = false;\n this.setSession?.(this.clone());\n }\n return false;\n }\n clone() {\n const session = new UserSession(this.client);\n session.isLoading = this.isLoading;\n session.authError = this.authError;\n session.authToken = this.authToken;\n session.setSession = this.setSession;\n session.lastSelectedAccount = this.lastSelectedAccount;\n session.switchAccount = this.switchAccount;\n session.typeRegistry = this.typeRegistry;\n session.onboardingComplete = this.onboardingComplete;\n return session;\n }\n}\nconst UserSessionContext = createContext(undefined);\nexport function useUserSession() {\n const session = useContext(UserSessionContext);\n if (!session) {\n throw new Error('useUserSession must be used within a UserSessionProvider');\n }\n return session;\n}\nexport { UserSession, UserSessionContext };\n//# sourceMappingURL=UserSession.js.map","/**\n * This hook is used to generate and verify state for OAuth2 authorization requests.\n * @returns\n */\nimport { useCallback } from \"react\";\nconst AUTH_STATE_KEY = 'auth_state';\nconst STATE_EXPIRY_KEY = 'auth_state_expiry';\nconst STATE_TTL = 5 * 60 * 1000; // 5 min\nexport function useAuthState() {\n // Generate new state\n const generateState = useCallback(() => {\n const state = crypto.randomUUID();\n const expiryTime = Date.now() + STATE_TTL;\n // Store state and expiry\n sessionStorage.setItem(AUTH_STATE_KEY, state);\n sessionStorage.setItem(STATE_EXPIRY_KEY, expiryTime.toString());\n return state;\n }, []);\n // Verify returned state\n const verifyState = useCallback((returnedState) => {\n if (!returnedState) {\n return 'Missing state';\n }\n const savedState = sessionStorage.getItem(AUTH_STATE_KEY);\n const expiryTime = parseInt(sessionStorage.getItem(STATE_EXPIRY_KEY) || '0');\n let reason;\n // Verify state matches and hasn't expired\n if (savedState !== returnedState) {\n reason = `State mismatched (${savedState} !== ${returnedState})`;\n }\n else if (Date.now() > expiryTime) {\n reason = 'State expired';\n }\n else {\n reason = undefined; // No errors\n }\n return reason;\n }, []);\n // Clear state (useful for cleanup)\n const clearState = useCallback(() => {\n sessionStorage.removeItem(AUTH_STATE_KEY);\n sessionStorage.removeItem(STATE_EXPIRY_KEY);\n }, []);\n return { generateState, verifyState, clearState };\n}\n//# sourceMappingURL=useAuthState.js.map","import { jsx as _jsx } from \"react/jsx-runtime\";\nimport { onAuthStateChanged } from \"firebase/auth\";\nimport { useEffect, useState } from \"react\";\nimport { UserNotFoundError, getComposableToken } from \"./auth/composable\";\nimport { getFirebaseAuth } from \"./auth/firebase\";\nimport { useAuthState } from \"./auth/useAuthState\";\nimport { Env } from \"@vertesia/ui/env\";\nimport { LastSelectedAccountId_KEY, LastSelectedProjectId_KEY, UserSession, UserSessionContext } from \"./UserSession\";\nconst devDomains = [\".composable.sh\", \".vertesia.dev\", \"vertesia.app\"];\nconst CENTRAL_AUTH_REDIRECT = \"https://internal-auth.vertesia.app/\";\nfunction shouldRedirectToCentralAuth() {\n // Authentication is not supported in Docker environment.\n // See https://github.com/vertesia/studio/wiki/Composable-UI-Hosting-Options\n if (Env.isDocker) {\n return true;\n }\n return devDomains.some((domain) => window.location.hostname.endsWith(domain));\n}\nexport function UserSessionProvider({ children }) {\n const hashParams = new URLSearchParams(location.hash.substring(1));\n const token = hashParams.get(\"token\");\n const state = hashParams.get(\"state\");\n const [session, setSession] = useState(new UserSession());\n const { generateState, verifyState, clearState } = useAuthState();\n const redirectToCentralAuth = (projectId, accountId) => {\n const url = new URL(CENTRAL_AUTH_REDIRECT);\n const currentUrl = new URL(window.location.href);\n currentUrl.hash = \"\";\n if (projectId)\n currentUrl.searchParams.set(\"p\", projectId);\n if (accountId)\n currentUrl.searchParams.set(\"a\", accountId);\n url.searchParams.set(\"redirect_uri\", currentUrl.toString());\n url.searchParams.set(\"state\", generateState());\n location.replace(url.toString());\n };\n useEffect(() => {\n console.log(\"Auth: starting auth flow\");\n Env.logger.info(\"Starting auth flow\");\n const currentUrl = new URL(window.location.href);\n const selectedAccount = currentUrl.searchParams.get(\"a\") ?? localStorage.getItem(LastSelectedAccountId_KEY) ?? undefined;\n const selectedProject = currentUrl.searchParams.get(\"p\") ??\n localStorage.getItem(LastSelectedProjectId_KEY + \"-\" + selectedAccount) ??\n undefined;\n console.log(\"Auth: selected account\", selectedAccount);\n console.log(\"Auth: selected project\", selectedProject);\n Env.logger.info(\"Selected account and project\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n if (token && state) {\n const validationError = verifyState(state);\n if (validationError) {\n console.error(`Auth: invalid state: ${validationError}`);\n Env.logger.error(`Invalid state: ${validationError}`, {\n vertesia: {\n state: state,\n },\n });\n redirectToCentralAuth();\n }\n else {\n clearState();\n }\n getComposableToken(selectedAccount, selectedProject, token, false, shouldRedirectToCentralAuth())\n .then((res) => {\n session.login(res.rawToken).then(() => {\n setSession(session.clone());\n //cleanup the hash\n window.location.hash = \"\";\n });\n })\n .catch((err) => {\n console.error(\"Failed to fetch user token from studio, redirecting to central auth\", err);\n Env.logger.error(\"Failed to fetch user token from studio, redirecting to central auth\", {\n vertesia: {\n error: err,\n },\n });\n redirectToCentralAuth();\n });\n return;\n }\n else {\n //if on a dev domain and not logged in, redirect to central auth\n if (!session.isLoggedIn()) {\n console.log(\"Auth: not logged in & no token/state\");\n Env.logger.info(\"Not logged in & no token/state\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n if (shouldRedirectToCentralAuth()) {\n console.log(\"Auth: on dev domain, redirecting to central auth with selection\", selectedAccount, selectedProject);\n Env.logger.info(\"Redirecting to central auth with selection\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n redirectToCentralAuth();\n }\n else {\n console.log(\"Auth: not on dev domain\");\n Env.logger.info(\"Not on dev domain\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n }\n }\n }\n return onAuthStateChanged(getFirebaseAuth(), async (firebaseUser) => {\n if (firebaseUser) {\n console.log(\"Auth: successful login with firebase\");\n Env.logger.info(\"Successful login with firebase\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n session.setSession = setSession;\n await getComposableToken(selectedAccount, selectedProject, undefined, false, shouldRedirectToCentralAuth())\n .then((res) => {\n session.login(res.rawToken).then(() => setSession(session.clone()));\n })\n .catch((err) => {\n console.error(\"Failed to fetch user token from studio\", err);\n Env.logger.error(\"Failed to fetch user token from studio\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n error: err,\n },\n });\n if (!(err instanceof UserNotFoundError))\n session.logout();\n session.isLoading = false;\n session.authError = err;\n setSession(session.clone());\n });\n }\n else {\n // anonymous user\n console.log(\"Auth: using anonymous user\");\n Env.logger.info(\"Using anonymous user\", {\n vertesia: {\n account_id: selectedAccount,\n project_id: selectedProject,\n },\n });\n session.client.withAuthCallback(undefined);\n session.logout();\n setSession(session.clone());\n }\n });\n }, []);\n return _jsx(UserSessionContext.Provider, { value: session, children: children });\n}\n//# sourceMappingURL=UserSessionProvider.js.map","import { Env } from '@vertesia/ui/env';\nimport { logEvent } from \"firebase/analytics\";\nimport { getFirebaseAnalytics } from \"./auth/firebase\";\nexport function useUXTracking() {\n //identify user in monitoring and UX systems\n const tagUserSession = async (user) => {\n const signupData = window.localStorage.getItem(\"composableSignupData\");\n if (!user) {\n console.error('No user found -- skipping tagging');\n return;\n }\n if (signupData) {\n window.localStorage.removeItem(\"composableSignupData\");\n }\n };\n //send event to analytics and UX systems\n const trackEvent = (eventName, eventProperties) => {\n if (!Env.isProd) {\n console.debug('track event', eventName, eventProperties);\n }\n //GA via firebase\n logEvent(getFirebaseAnalytics(), eventName, { ...eventProperties, debug_mode: !Env.isProd });\n };\n return {\n tagUserSession,\n trackEvent\n };\n}\n//# sourceMappingURL=useUXTracking.js.map"],"names":["LastSelectedAccountId_KEY","LastSelectedProjectId_KEY","AUTH_TOKEN_RAW","AUTH_TOKEN","_firebaseApp","_analytics","_firebaseAuth","getFirebaseApp","Env","firebase","Error","initializeApp","error","console","getFirebaseAnalytics","getAnalytics","getFirebaseAuth","getAuth","async","setFirebaseTenant","tenantEmail","log","retries","retryDelay","response","fetch","method","headers","body","JSON","stringify","signal","AbortSignal","timeout","ok","errorData","json","parseError","status","warn","data","firebaseTenantId","auth","tenantId","providerType","provider","fetchError","Promise","resolve","setTimeout","message","getFirebaseAuthToken","refresh","user","currentUser","getIdToken","then","token","logger","info","vertesia","user_email","email","user_name","displayName","user_id","uid","catch","err","fetchComposableToken","accountId","projectId","ttl","account_id","project_id","idToken","endpoints","studio","url","URL","searchParams","set","String","res","Authorization","localStorage","removeItem","idTokenDecoded","jwtDecode","UserNotFoundError","fetchComposableTokenFromFirebaseToken","getComposableToken","initToken","forceRefresh","useInternalAuth","selectedAccount","getItem","undefined","selectedProject","exp","Date","now","rawToken","constructor","super","this","name","useCurrentTenant","useUserSession","currentTenant","setCurrentTenant","useState","isLoading","setIsLoading","setError","useEffect","tenantData","tenantKey","label","domain","logo","loadCurrentTenant","TypeRegistry","types","map","sort","a","b","localeCompare","type","id","getType","getTypeLayout","table_layout","getTypeName","UserSession","client","authError","authToken","typeRegistry","setSession","lastSelectedAccount","lastSelectedProject","onboardingComplete","VertesiaClient","serverUrl","storeUrl","zeno","logout","bind","store","account","project","accounts","authCallback","rawAuthToken","signOut","getAccount","login","withAuthCallback","setItem","onLogin","all","_loadTypes","fetchOnboardingStatus","isLoggedIn","switchAccount","targetAccountId","window","location","replace","switchProject","targetProjectId","list","layout","reloadTypes","clone","fetchAccounts","previousStatus","onboarding","onboardingProgress","Object","values","every","value","session","UserSessionContext","createContext","useContext","AUTH_STATE_KEY","STATE_EXPIRY_KEY","devDomains","shouldRedirectToCentralAuth","isDocker","some","hostname","endsWith","UserSessionProvider","children","hashParams","URLSearchParams","hash","substring","get","state","generateState","verifyState","clearState","useCallback","crypto","randomUUID","expiryTime","sessionStorage","toString","returnedState","savedState","parseInt","reason","redirectToCentralAuth","currentUrl","href","validationError","onAuthStateChanged","firebaseUser","_jsx","Provider","useUXTracking","tagUserSession","signupData","trackEvent","eventName","eventProperties","isProd","debug","logEvent","debug_mode"],"mappings":"+eAAY,MAACA,EAA4B,qCAC5BC,EAA4B,qCCIzC,ICDIC,EACAC,EDAAC,EAAe,KACfC,EAAa,KACbC,EAAgB,KAEb,SAASC,IACZ,IAAKH,EACD,IACI,IAAKI,EAAIC,SACL,MAAM,IAAIC,MAAM,8DAEpBN,EAAeO,EAAcH,EAAIC,SAC7C,CACQ,MAAOG,GAEH,MADAC,QAAQD,MAAM,qCAAsCA,GAC9C,IAAIF,MAAM,+EAC5B,CAEI,OAAON,CACX,CACO,SAASU,IAIZ,OAHKT,IACDA,EAAaU,EAAaR,MAEvBF,CACX,CACO,SAASW,IAIZ,OAHKV,IACDA,EAAgBW,EAAQV,MAErBD,CACX,CACOY,eAAeC,EAAkBC,GACpC,GAAKA,EAIL,GAAKZ,EAAIC,SAIT,IACQW,GACAP,QAAQQ,IAAI,mCAAmCD,KAEnD,IAAIE,EAAU,EACVC,EAAa,IACjB,KAAOD,EAAU,GACb,IAEI,MAAME,QAAiBC,MAAM,sBAAuB,CAChDC,OAAQ,OACRC,QAAS,CACL,eAAgB,oBAEpBC,KAAMC,KAAKC,UAAU,CACjBV,YAAaA,IAGjBW,OAAQC,YAAYC,QAAQ,OAGhC,IAAKT,EACD,MAAM,IAAId,MAAM,wCAGpB,IAAKc,EAASU,GAAI,CAEd,IACI,MAAMC,QAAkBX,EAASY,OACjCvB,QAAQD,MAAM,+BAAgCuB,EAAUvB,MAChF,CACoB,MAAOyB,GACHxB,QAAQD,MAAM,qCAAqCY,EAASc,SACpF,CAEoB,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