1- import { useCallback , useEffect , useRef , useState } from "react"
1+ import { useCallback } from "react"
22import { VSCodeButton } from "@vscode/webview-ui-toolkit/react"
33
4- import type { ProviderSettings , ZgsmUserInfo } from "@roo-code/types"
4+ import type { ProviderSettings } from "@roo-code/types"
55import { TelemetryEventName } from "@roo-code/types"
66
77import { useAppTranslation } from "@src/i18n/TranslationContext"
88import { vscode } from "@src/utils/vscode"
99import { telemetryClient } from "@src/utils/TelemetryClient"
10- import axios from "axios"
1110import { useEvent } from "react-use"
1211import { ExtensionMessage } from "@roo/ExtensionMessage"
12+ import { useZgsmUserInfo } from "@src/hooks/useZgsmUserInfo"
1313
1414type AccountViewProps = {
1515 apiConfiguration ?: ProviderSettings
@@ -18,52 +18,10 @@ type AccountViewProps = {
1818
1919export const ZgsmAccountView = ( { apiConfiguration, onDone } : AccountViewProps ) => {
2020 const { t } = useAppTranslation ( )
21- const [ userInfo , setUserInfo ] = useState < ZgsmUserInfo | null > ( null )
22- const [ hash , setHash ] = useState ( "" )
23- const [ logoPic , setLogoPic ] = useState ( "" )
24-
25- const wasAuthenticatedRef = useRef ( false )
21+ const { userInfo, logoPic, hash } = useZgsmUserInfo ( apiConfiguration )
2622
2723 const rooLogoUri = ( window as any ) . COSTRICT_BASE_URI + "/logo.svg"
2824
29- // Track authentication state changes to detect successful logout
30- useEffect ( ( ) => {
31- const token = apiConfiguration ?. zgsmAccessToken
32-
33- if ( token ) {
34- wasAuthenticatedRef . current = true
35-
36- const jwt = parseJwt ( token )
37-
38- const basicInfo : ZgsmUserInfo = {
39- id : jwt . id ,
40- name : jwt ?. properties ?. oauth_GitHub_username || jwt . id ,
41- picture : undefined ,
42- email : jwt . email ,
43- phone : jwt . phone ,
44- organizationName : jwt . organizationName ,
45- organizationImageUrl : jwt . organizationImageUrl ,
46- }
47- setUserInfo ( basicInfo )
48-
49- if ( jwt . avatar ) {
50- imageUrlToBase64 ( jwt . avatar ) . then ( ( base64 ) => {
51- if ( ! base64 ) return
52- // Step 3: Update userInfo.picture, only update the picture field
53- setLogoPic ( base64 )
54- } )
55- }
56-
57- hashToken ( token ) . then ( ( result ) => {
58- console . log ( "New Credit hash: " , result )
59- setHash ( result )
60- } )
61- } else if ( wasAuthenticatedRef . current && ! token ) {
62- telemetryClient . capture ( TelemetryEventName . ACCOUNT_LOGOUT_SUCCESS )
63- wasAuthenticatedRef . current = false
64- }
65- } , [ apiConfiguration ?. zgsmAccessToken ] )
66-
6725 const handleConnectClick = ( ) => {
6826 // Send telemetry for account connect action
6927 telemetryClient . capture ( TelemetryEventName . ACCOUNT_CONNECT_CLICKED )
@@ -193,42 +151,3 @@ export const ZgsmAccountView = ({ apiConfiguration, onDone }: AccountViewProps)
193151 </ div >
194152 )
195153}
196-
197- function parseJwt ( token : string ) {
198- const parts = token . split ( "." )
199- if ( parts . length !== 3 ) {
200- throw new Error ( "Invalid JWT" )
201- }
202- const payload = parts [ 1 ]
203- const decoded = atob ( payload . replace ( / - / g, "+" ) . replace ( / _ / g, "/" ) ) // base64url → base64 → decode
204- return JSON . parse ( decoded )
205- }
206-
207- async function hashToken ( token : string ) {
208- const encoder = new TextEncoder ( )
209- const data = encoder . encode ( token )
210- const hashBuffer = await crypto . subtle . digest ( "SHA-256" , data )
211- return Array . from ( new Uint8Array ( hashBuffer ) )
212- . map ( ( b ) => b . toString ( 16 ) . padStart ( 2 , "0" ) )
213- . join ( "" )
214- }
215-
216- export async function imageUrlToBase64 ( url : string ) : Promise < string | null > {
217- try {
218- const response = await axios . get ( url , {
219- responseType : "blob" , // Key! Ensure axios returns Blob
220- } )
221-
222- const blob = response . data as Blob
223-
224- return await new Promise < string > ( ( resolve , reject ) => {
225- const reader = new FileReader ( )
226- reader . onloadend = ( ) => resolve ( reader . result as string )
227- reader . onerror = ( ) => reject ( "Failed to convert blob to base64" )
228- reader . readAsDataURL ( blob ) // Automatically adds data:image/png;base64,...
229- } )
230- } catch ( error ) {
231- console . error ( "Failed to convert image to base64" , error )
232- return null
233- }
234- }
0 commit comments