1
- import { SeamHttp , SeamHttpEndpoints } from '@seamapi/http/connect'
1
+ import {
2
+ SeamHttp ,
3
+ SeamHttpEndpoints ,
4
+ SeamHttpMultiWorkspace ,
5
+ } from '@seamapi/http/connect'
2
6
import { useQuery } from '@tanstack/react-query'
3
7
import { useEffect } from 'react'
4
8
import { v4 as uuidv4 } from 'uuid'
@@ -8,6 +12,7 @@ import { useSeamQueryContext } from './SeamQueryProvider.js'
8
12
export function useSeamClient ( ) : {
9
13
client : SeamHttp | null
10
14
endpointClient : SeamHttpEndpoints | null
15
+ clientWithoutWorkspace : SeamHttpMultiWorkspace | null
11
16
queryKeyPrefixes : string [ ]
12
17
isPending : boolean
13
18
isError : boolean
@@ -18,16 +23,20 @@ export function useSeamClient(): {
18
23
clientOptions,
19
24
publishableKey,
20
25
clientSessionToken,
26
+ consoleSessionToken,
27
+ workspaceId,
21
28
queryKeyPrefix,
22
29
...context
23
30
} = useSeamQueryContext ( )
24
31
const userIdentifierKey = useUserIdentifierKeyOrFingerprint (
25
32
clientSessionToken != null ? '' : context . userIdentifierKey
26
33
)
27
34
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
+ } > ( {
31
40
queryKey : [
32
41
...getQueryKeyPrefixes ( { queryKeyPrefix } ) ,
33
42
'client' ,
@@ -41,46 +50,81 @@ export function useSeamClient(): {
41
50
] ,
42
51
queryFn : async ( ) => {
43
52
if ( client != null )
44
- return [ client , SeamHttpEndpoints . fromClient ( client . client ) ]
53
+ return {
54
+ client,
55
+ endpointClient : SeamHttpEndpoints . fromClient ( client . client ) ,
56
+ clientWithoutWorkspace : null ,
57
+ }
45
58
46
59
if ( clientSessionToken != null ) {
47
- const clientSessionTokenClient = SeamHttp . fromClientSessionToken (
60
+ const seam = SeamHttp . fromClientSessionToken (
48
61
clientSessionToken ,
49
62
clientOptions
50
63
)
51
64
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
+ }
56
70
}
57
71
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
61
77
)
78
+
79
+ return {
80
+ client : seam ,
81
+ endpointClient : SeamHttpEndpoints . fromClient ( seam . client ) ,
82
+ clientWithoutWorkspace : null ,
83
+ }
62
84
}
63
85
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.'
68
113
)
69
- return [
70
- publishableKeyClient ,
71
- SeamHttpEndpoints . fromClient ( publishableKeyClient . client ) ,
72
- ]
73
114
} ,
74
115
} )
75
116
76
117
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 ,
79
121
queryKeyPrefixes : getQueryKeyPrefixes ( {
80
122
queryKeyPrefix,
81
123
userIdentifierKey,
82
124
publishableKey,
83
125
clientSessionToken,
126
+ consoleSessionToken,
127
+ workspaceId,
84
128
} ) ,
85
129
isPending,
86
130
isError,
@@ -132,11 +176,15 @@ const getQueryKeyPrefixes = ({
132
176
userIdentifierKey,
133
177
publishableKey,
134
178
clientSessionToken,
179
+ consoleSessionToken,
180
+ workspaceId,
135
181
} : {
136
182
queryKeyPrefix : string | undefined
137
183
userIdentifierKey ?: string
138
184
publishableKey ?: string | undefined
139
185
clientSessionToken ?: string | undefined
186
+ consoleSessionToken ?: string | undefined
187
+ workspaceId ?: string | undefined
140
188
} ) : string [ ] => {
141
189
const seamPrefix = 'seam'
142
190
@@ -150,5 +198,9 @@ const getQueryKeyPrefixes = ({
150
198
return [ seamPrefix , publishableKey , userIdentifierKey ]
151
199
}
152
200
201
+ if ( consoleSessionToken != null && workspaceId != null ) {
202
+ return [ seamPrefix , consoleSessionToken , workspaceId ]
203
+ }
204
+
153
205
return [ seamPrefix ]
154
206
}
0 commit comments