11"use client" ;
22
3- import {
4- createContext , useContext , useState , useEffect ,
5- } from "react" ;
3+ import { createContext , useContext , useState , useEffect } from "react" ;
64import { createFrontendClient } from "@pipedream/sdk/browser" ;
7- import {
8- getServerCodeSnippet , getClientCodeSnippet ,
9- } from "./ConnectCodeSnippets" ;
5+ import { getServerCodeSnippet , getClientCodeSnippet } from "./ConnectCodeSnippets" ;
6+ import { generateConnectToken , fetchAccountDetails } from "./api" ;
107
11- // Generate a UUID v4 for use as external_user_id
8+ /**
9+ * Generate a UUID v4 for use as external_user_id
10+ */
1211function generateUUID ( ) {
1312 return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" . replace ( / [ x y ] / g, function ( c ) {
1413 const r = Math . random ( ) * 16 | 0 ;
15- const v = c === "x"
16- ? r
17- : ( r & 0x3 | 0x8 ) ;
14+ const v = c === "x" ? r : ( r & 0x3 | 0x8 ) ;
1815 return v . toString ( 16 ) ;
1916 } ) ;
2017}
2118
2219// Create the context
2320const GlobalConnectContext = createContext ( null ) ;
2421
25- // Provider component
22+ /**
23+ * Provider component for Connect demo state management
24+ */
2625export function GlobalConnectProvider ( { children } ) {
27- // User and account state
28- const [
29- appSlug ,
30- setAppSlug ,
31- ] = useState ( "slack" ) ;
32- const [
33- externalUserId ,
34- setExternalUserId ,
35- ] = useState ( "" ) ;
36- const [
37- connectedAccount ,
38- setConnectedAccount ,
39- ] = useState ( null ) ;
26+ // User and app state
27+ const [ appSlug , setAppSlug ] = useState ( "slack" ) ;
28+ const [ externalUserId , setExternalUserId ] = useState ( "" ) ;
29+ const [ connectedAccount , setConnectedAccount ] = useState ( null ) ;
4030
4131 // Token state
42- const [
43- tokenData ,
44- setTokenData ,
45- ] = useState ( null ) ;
46-
32+ const [ tokenData , setTokenData ] = useState ( null ) ;
33+
4734 // UI state
48- const [
49- tokenLoading ,
50- setTokenLoading ,
51- ] = useState ( false ) ;
52- const [
53- error ,
54- setError ,
55- ] = useState ( null ) ;
35+ const [ tokenLoading , setTokenLoading ] = useState ( false ) ;
36+ const [ error , setError ] = useState ( null ) ;
5637
5738 // Generate a new UUID when the component mounts
5839 useEffect ( ( ) => {
5940 setExternalUserId ( generateUUID ( ) ) ;
6041 } , [ ] ) ;
6142
62- // Get server code snippet wrapper function
43+ // Get code snippet wrapper functions
6344 const getServerSnippet = ( ) => getServerCodeSnippet ( externalUserId ) ;
64-
65- // Get client code snippet wrapper function
6645 const getClientSnippet = ( ) => getClientCodeSnippet ( appSlug , tokenData ) ;
6746
6847 /**
69- * Generate a request token based on the browser environment
70- * This creates a token that matches what the API will generate
48+ * Generate a token for the Connect demo
7149 */
72- function generateRequestToken ( ) {
73- const baseString = `${ navigator . userAgent } :${ window . location . host } :connect-demo` ;
74- return btoa ( baseString ) ;
75- }
76-
77- // Generate token async function
7850 async function generateToken ( ) {
7951 setTokenLoading ( true ) ;
8052 setError ( null ) ;
8153 // Clear any previously connected account when generating a new token
8254 setConnectedAccount ( null ) ;
8355
8456 try {
85- const requestToken = generateRequestToken ( ) ;
86- const response = await fetch ( "/docs/api-demo-connect/token" , {
87- method : "POST" ,
88- headers : {
89- "Content-Type" : "application/json" ,
90- "X-Request-Token" : requestToken ,
91- } ,
92- body : JSON . stringify ( {
93- external_user_id : externalUserId ,
94- } ) ,
95- } ) ;
96-
97- if ( ! response . ok ) {
98- const errorData = await response . json ( ) ;
99- throw new Error ( errorData . error || "Failed to get token" ) ;
100- }
101-
102- const data = await response . json ( ) ;
57+ const data = await generateConnectToken ( externalUserId ) ;
10358 setTokenData ( data ) ;
10459 } catch ( err ) {
10560 setError ( err . message || "An error occurred" ) ;
@@ -108,37 +63,9 @@ export function GlobalConnectProvider({ children }) {
10863 }
10964 }
11065
111- // Fetch account details from API
112- async function fetchAccountDetails ( accountId ) {
113- try {
114- // Fetch the account details from our API endpoint
115- const requestToken = generateRequestToken ( ) ;
116- const response = await fetch ( `/docs/api-demo-connect/accounts/${ accountId } ` , {
117- method : "GET" ,
118- headers : {
119- "Content-Type" : "application/json" ,
120- "X-Request-Token" : requestToken ,
121- } ,
122- } ) ;
123-
124- if ( ! response . ok ) {
125- console . warn ( "Failed to fetch account details" , await response . text ( ) ) ;
126- return {
127- id : accountId ,
128- } ; // Fall back to just the ID
129- }
130-
131- const data = await response . json ( ) ;
132- return data ; // Return the full account details
133- } catch ( err ) {
134- console . warn ( "Error fetching account details:" , err ) ;
135- return {
136- id : accountId ,
137- } ; // Fall back to just the ID
138- }
139- }
140-
141- // Connect account function
66+ /**
67+ * Connect an account using the Pipedream Connect SDK
68+ */
14269 function connectAccount ( ) {
14370 if ( ! tokenData ?. token ) {
14471 setError ( "Please generate a token first" ) ;
@@ -153,7 +80,7 @@ export function GlobalConnectProvider({ children }) {
15380 app : appSlug ,
15481 token : tokenData . token ,
15582 onSuccess : async ( account ) => {
156- // Initialize with just the ID
83+ // Initialize with just the ID and loading state
15784 setConnectedAccount ( {
15885 id : account . id ,
15986 loading : true ,
@@ -172,7 +99,7 @@ export function GlobalConnectProvider({ children }) {
17299 setTokenData ( null ) ;
173100 } ,
174101 onError : ( err ) => {
175- setError ( err . message || "Failed to connect account, please refresh the page and try again. " ) ;
102+ setError ( err . message || "Failed to connect account" ) ;
176103 } ,
177104 onClose : ( ) => {
178105 // Dialog closed by user - no action needed
@@ -183,8 +110,8 @@ export function GlobalConnectProvider({ children }) {
183110 }
184111 }
185112
186- // Create value object
187- const value = {
113+ // Create context value object
114+ const contextValue = {
188115 // State
189116 appSlug,
190117 externalUserId,
@@ -204,13 +131,15 @@ export function GlobalConnectProvider({ children }) {
204131 } ;
205132
206133 return (
207- < GlobalConnectContext . Provider value = { value } >
134+ < GlobalConnectContext . Provider value = { contextValue } >
208135 { children }
209136 </ GlobalConnectContext . Provider >
210137 ) ;
211138}
212139
213- // Custom hook for using the context
140+ /**
141+ * Custom hook for accessing the Connect demo context
142+ */
214143export function useGlobalConnect ( ) {
215144 const context = useContext ( GlobalConnectContext ) ;
216145 if ( ! context ) {
0 commit comments