1- import { type JSX , useEffect , useMemo , useRef , useState } from 'react' ;
1+ import { type JSX , useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
22import { useTranslation } from 'react-i18next' ;
33import {
44 Fingerprint ,
@@ -48,11 +48,14 @@ function formatError(error: unknown): string {
4848 return String ( error ) ;
4949}
5050
51- function renderProfile ( profile ?: DeviceProfile ) : JSX . Element {
51+ function renderProfile (
52+ profile : DeviceProfile | undefined ,
53+ t : ReturnType < typeof useTranslation > [ 't' ] ,
54+ ) : JSX . Element {
5255 if ( ! profile ) {
5356 return (
5457 < div className = "text-muted-foreground rounded-lg border border-dashed px-3 py-4 text-xs" >
55- N/A
58+ { t ( 'common.notAvailable' ) }
5659 </ div >
5760 ) ;
5861 }
@@ -95,41 +98,44 @@ export function IdentityProfileDialog({ account, open, onOpenChange }: IdentityP
9598 const history = useMemo ( ( ) => sortHistory ( snapshot ?. history || [ ] ) , [ snapshot ?. history ] ) ;
9699 const showLoadingPlaceholder = initialLoading && ! snapshot ;
97100
98- const refreshProfiles = async ( options ?: { silent ?: boolean } ) => {
99- if ( ! account ) {
100- setSnapshot ( null ) ;
101- return ;
102- }
103- const silent = options ?. silent === true ;
104- if ( silent ) {
105- setRefreshing ( true ) ;
106- } else {
107- setInitialLoading ( true ) ;
108- }
109- try {
110- const result = await getCloudIdentityProfiles ( { accountId : account . id } ) ;
111- setSnapshot ( result ) ;
112- } catch ( error ) {
113- toast ( {
114- title : t ( 'cloud.toast.actionFailed' , { defaultValue : 'Action failed' } ) ,
115- description : formatError ( error ) ,
116- variant : 'destructive' ,
117- } ) ;
118- } finally {
101+ const refreshProfiles = useCallback (
102+ async ( options ?: { silent ?: boolean } ) => {
103+ if ( ! account ) {
104+ setSnapshot ( null ) ;
105+ return ;
106+ }
107+ const silent = options ?. silent === true ;
119108 if ( silent ) {
120- setRefreshing ( false ) ;
109+ setRefreshing ( true ) ;
121110 } else {
122- setInitialLoading ( false ) ;
111+ setInitialLoading ( true ) ;
123112 }
124- }
125- } ;
113+ try {
114+ const result = await getCloudIdentityProfiles ( { accountId : account . id } ) ;
115+ setSnapshot ( result ) ;
116+ } catch ( error ) {
117+ toast ( {
118+ title : t ( 'cloud.toast.actionFailed' ) ,
119+ description : formatError ( error ) ,
120+ variant : 'destructive' ,
121+ } ) ;
122+ } finally {
123+ if ( silent ) {
124+ setRefreshing ( false ) ;
125+ } else {
126+ setInitialLoading ( false ) ;
127+ }
128+ }
129+ } ,
130+ [ account , t , toast ] ,
131+ ) ;
126132
127133 useEffect ( ( ) => {
128134 if ( ! open ) {
129135 return ;
130136 }
131- refreshProfiles ( ) ;
132- } , [ open , account ?. id ] ) ;
137+ void refreshProfiles ( ) ;
138+ } , [ open , refreshProfiles ] ) ;
133139
134140 const runAction = async ( key : string , action : ( ) => Promise < void > ) => {
135141 if ( actionLockRef . current ) {
@@ -142,7 +148,7 @@ export function IdentityProfileDialog({ account, open, onOpenChange }: IdentityP
142148 await refreshProfiles ( { silent : true } ) ;
143149 } catch ( error ) {
144150 toast ( {
145- title : t ( 'cloud.toast.actionFailed' , { defaultValue : 'Action failed' } ) ,
151+ title : t ( 'cloud.toast.actionFailed' ) ,
146152 description : formatError ( error ) ,
147153 variant : 'destructive' ,
148154 } ) ;
@@ -247,7 +253,7 @@ export function IdentityProfileDialog({ account, open, onOpenChange }: IdentityP
247253 < CardTitle className = "text-sm" > { t ( 'cloud.identity.previewTitle' ) } </ CardTitle >
248254 </ CardHeader >
249255 < CardContent className = "space-y-4" >
250- { renderProfile ( previewProfile ) }
256+ { renderProfile ( previewProfile , t ) }
251257 < div className = "flex flex-wrap gap-2" >
252258 < Button
253259 size = "sm"
@@ -295,7 +301,7 @@ export function IdentityProfileDialog({ account, open, onOpenChange }: IdentityP
295301 { t ( 'cloud.identity.loading' ) }
296302 </ div >
297303 ) : (
298- renderProfile ( snapshot ?. currentStorage )
304+ renderProfile ( snapshot ?. currentStorage , t )
299305 ) }
300306 </ CardContent >
301307 </ Card >
@@ -313,7 +319,7 @@ export function IdentityProfileDialog({ account, open, onOpenChange }: IdentityP
313319 { t ( 'cloud.identity.loading' ) }
314320 </ div >
315321 ) : (
316- renderProfile ( snapshot ?. boundProfile )
322+ renderProfile ( snapshot ?. boundProfile , t )
317323 ) }
318324 </ CardContent >
319325 </ Card >
@@ -353,7 +359,7 @@ export function IdentityProfileDialog({ account, open, onOpenChange }: IdentityP
353359 ) : null }
354360 </ div >
355361
356- { renderProfile ( version . profile ) }
362+ { renderProfile ( version . profile , t ) }
357363
358364 < div className = "mt-3 flex flex-wrap gap-2" >
359365 < Button
@@ -402,15 +408,13 @@ export function IdentityProfileDialog({ account, open, onOpenChange }: IdentityP
402408
403409 < Card className = "shadow-sm" >
404410 < CardHeader className = "pb-3" >
405- < CardTitle className = "text-sm" >
406- { t ( 'cloud.identity.baseline' , { defaultValue : 'Reference Profile' } ) }
407- </ CardTitle >
411+ < CardTitle className = "text-sm" > { t ( 'cloud.identity.baseline' ) } </ CardTitle >
408412 </ CardHeader >
409413 < CardContent >
410414 { showLoadingPlaceholder ? (
411415 < div className = "text-muted-foreground text-xs" > { t ( 'cloud.identity.loading' ) } </ div >
412416 ) : (
413- renderProfile ( snapshot ?. baseline )
417+ renderProfile ( snapshot ?. baseline , t )
414418 ) }
415419 </ CardContent >
416420 </ Card >
0 commit comments