1- import { SeamHttp , SeamHttpEndpoints } from '@seamapi/http/connect'
1+ import {
2+ SeamHttp ,
3+ SeamHttpEndpoints ,
4+ SeamHttpMultiWorkspace ,
5+ } from '@seamapi/http/connect'
26import { useQuery } from '@tanstack/react-query'
37import { useEffect } from 'react'
48import { v4 as uuidv4 } from 'uuid'
@@ -8,6 +12,7 @@ import { useSeamQueryContext } from './SeamQueryProvider.js'
812export function useSeamClient ( ) : {
913 client : SeamHttp | null
1014 endpointClient : SeamHttpEndpoints | null
15+ clientWithoutWorkspace : SeamHttpMultiWorkspace | null
1116 queryKeyPrefixes : string [ ]
1217 isPending : boolean
1318 isError : boolean
@@ -18,16 +23,20 @@ export function useSeamClient(): {
1823 clientOptions,
1924 publishableKey,
2025 clientSessionToken,
26+ consoleSessionToken,
27+ workspaceId,
2128 queryKeyPrefix,
2229 ...context
2330 } = useSeamQueryContext ( )
2431 const userIdentifierKey = useUserIdentifierKeyOrFingerprint (
2532 clientSessionToken != null ? '' : context . userIdentifierKey
2633 )
2734
28- const { isPending, isError, error, data } = useQuery <
29- [ SeamHttp , SeamHttpEndpoints ]
30- > ( {
35+ const { isPending, isError, error, data } = useQuery < {
36+ client : SeamHttp | null
37+ endpointClient : SeamHttpEndpoints | null
38+ clientWithoutWorkspace : SeamHttpMultiWorkspace | null
39+ } > ( {
3140 queryKey : [
3241 ...getQueryKeyPrefixes ( { queryKeyPrefix } ) ,
3342 'client' ,
@@ -41,46 +50,81 @@ export function useSeamClient(): {
4150 ] ,
4251 queryFn : async ( ) => {
4352 if ( client != null )
44- return [ client , SeamHttpEndpoints . fromClient ( client . client ) ]
53+ return {
54+ client,
55+ endpointClient : SeamHttpEndpoints . fromClient ( client . client ) ,
56+ clientWithoutWorkspace : null ,
57+ }
4558
4659 if ( clientSessionToken != null ) {
47- const clientSessionTokenClient = SeamHttp . fromClientSessionToken (
60+ const seam = SeamHttp . fromClientSessionToken (
4861 clientSessionToken ,
4962 clientOptions
5063 )
5164
52- return [
53- clientSessionTokenClient ,
54- SeamHttpEndpoints . fromClient ( clientSessionTokenClient . client ) ,
55- ]
65+ return {
66+ client : seam ,
67+ endpointClient : SeamHttpEndpoints . fromClient ( seam . client ) ,
68+ clientWithoutWorkspace : null ,
69+ }
5670 }
5771
58- if ( publishableKey == null ) {
59- throw new Error (
60- 'Missing either a client, publishableKey, or clientSessionToken'
72+ if ( publishableKey != null ) {
73+ const seam = await SeamHttp . fromPublishableKey (
74+ publishableKey ,
75+ userIdentifierKey ,
76+ clientOptions
6177 )
78+
79+ return {
80+ client : seam ,
81+ endpointClient : SeamHttpEndpoints . fromClient ( seam . client ) ,
82+ clientWithoutWorkspace : null ,
83+ }
6284 }
6385
64- const publishableKeyClient = await SeamHttp . fromPublishableKey (
65- publishableKey ,
66- userIdentifierKey ,
67- clientOptions
86+ if ( consoleSessionToken != null ) {
87+ const clientWithoutWorkspace =
88+ SeamHttpMultiWorkspace . fromConsoleSessionToken ( consoleSessionToken )
89+
90+ if ( workspaceId == null ) {
91+ return {
92+ client : null ,
93+ endpointClient : null ,
94+ clientWithoutWorkspace,
95+ }
96+ }
97+
98+ const seam = SeamHttp . fromConsoleSessionToken (
99+ consoleSessionToken ,
100+ workspaceId ,
101+ clientOptions
102+ )
103+
104+ return {
105+ client : seam ,
106+ endpointClient : SeamHttpEndpoints . fromClient ( seam . client ) ,
107+ clientWithoutWorkspace,
108+ }
109+ }
110+
111+ throw new Error (
112+ 'Missing either a client, publishableKey, clientSessionToken, or consoleSessionToken.'
68113 )
69- return [
70- publishableKeyClient ,
71- SeamHttpEndpoints . fromClient ( publishableKeyClient . client ) ,
72- ]
73114 } ,
74115 } )
75116
76117 return {
77- client : data ?. [ 0 ] ?? null ,
78- endpointClient : data ?. [ 1 ] ?? null ,
118+ client : data ?. client ?? null ,
119+ endpointClient : data ?. endpointClient ?? null ,
120+ clientWithoutWorkspace : data ?. clientWithoutWorkspace ?? null ,
79121 queryKeyPrefixes : getQueryKeyPrefixes ( {
80122 queryKeyPrefix,
81123 userIdentifierKey,
82124 publishableKey,
83125 clientSessionToken,
126+ consoleSessionToken,
127+ workspaceId,
84128 } ) ,
85129 isPending,
86130 isError,
@@ -132,11 +176,15 @@ const getQueryKeyPrefixes = ({
132176 userIdentifierKey,
133177 publishableKey,
134178 clientSessionToken,
179+ consoleSessionToken,
180+ workspaceId,
135181} : {
136182 queryKeyPrefix : string | undefined
137183 userIdentifierKey ?: string
138184 publishableKey ?: string | undefined
139185 clientSessionToken ?: string | undefined
186+ consoleSessionToken ?: string | undefined
187+ workspaceId ?: string | undefined
140188} ) : string [ ] => {
141189 const seamPrefix = 'seam'
142190
@@ -150,5 +198,9 @@ const getQueryKeyPrefixes = ({
150198 return [ seamPrefix , publishableKey , userIdentifierKey ]
151199 }
152200
201+ if ( consoleSessionToken != null && workspaceId != null ) {
202+ return [ seamPrefix , consoleSessionToken , workspaceId ]
203+ }
204+
153205 return [ seamPrefix ]
154206}
0 commit comments