File tree Expand file tree Collapse file tree 5 files changed +29
-15
lines changed
Expand file tree Collapse file tree 5 files changed +29
-15
lines changed Original file line number Diff line number Diff line change @@ -19,12 +19,11 @@ export default function AlertProvider({
1919 alertManagerUrl : string ;
2020 children : React . ReactNode ;
2121} ) {
22- const { userData } = useAuth ( ) ;
22+ const { getToken } = useAuth ( ) ;
2323 const query = useQuery (
24- [ 'activeAlerts' , userData ?. token ] ,
25- ( ) => getAlerts ( alertManagerUrl , userData ?. token ) ,
24+ [ 'activeAlerts' ] ,
25+ async ( ) => getAlerts ( alertManagerUrl , await getToken ( ) ) ,
2626 {
27- // refetch the alerts every 30 seconds
2827 refetchInterval : 30000 ,
2928 // TODO manage this refresh interval globally
3029 // avoid stucking at the hard loading state before alertmanager is ready
Original file line number Diff line number Diff line change @@ -7,6 +7,16 @@ import AlertProvider from './AlertProvider';
77import { useHighestSeverityAlerts } from './alertHooks' ;
88import { afterAll , beforeAll , jest } from '@jest/globals' ;
99import { QueryClientProvider } from '../QueryClientProvider' ;
10+
11+ jest . mock ( '../auth/AuthProvider' , ( ) => ( {
12+ useAuth : ( ) => ( {
13+ userData : {
14+ token : 'test-token' ,
15+ } ,
16+ getToken : ( ) => Promise . resolve ( 'test-token' ) ,
17+ } ) ,
18+ } ) ) ;
19+
1020const testService = 'http://10.0.0.1/api/alertmanager' ;
1121
1222const VOLUME_DEGRADED_ALERT = {
Original file line number Diff line number Diff line change @@ -29,9 +29,9 @@ export type AlertLabels = {
2929 selectors ?: string [ ] ;
3030 [ labelName : string ] : string ;
3131} ;
32- export function getAlerts ( alertManagerUrl : string , token ? : string ) {
32+ export function getAlerts ( alertManagerUrl : string , token : string ) {
3333 return fetch ( alertManagerUrl + '/api/v2/alerts' , {
34- headers : token ? { Authorization : `Bearer ${ token } ` } : { } ,
34+ headers : { Authorization : `Bearer ${ token } ` } ,
3535 } )
3636 . then ( ( r ) => {
3737 if ( r . ok ) {
Original file line number Diff line number Diff line change @@ -61,7 +61,7 @@ const DashboardAlerts = () => {
6161 const totalAlerts = criticalAlerts . length + warningAlerts . length ;
6262 return (
6363 < AlertsContainer >
64- < div style = { { display : ' flex' , alignItems : ' center' , gap : spacing . r8 } } >
64+ < Box display = " flex" alignItems = " center" gap = { spacing . r8 } >
6565 < div >
6666 < Text isEmphazed >
6767 { intl . formatMessage ( {
@@ -98,7 +98,7 @@ const DashboardAlerts = () => {
9898 </ Banner >
9999 </ div >
100100 ) }
101- </ div >
101+ </ Box >
102102 { totalAlerts === 0 ? null : (
103103 < Box pr = { 24 } >
104104 < BadgesContainer >
Original file line number Diff line number Diff line change 1- import { ReactNode , useEffect , useState } from 'react' ;
1+ import { ReactNode , useEffect } from 'react' ;
22import { useAuth } from './PrivateRoute' ;
33import { setHeaders } from '../services/prometheus/api' ;
44
5+ // Temporary solution: The Prometheus API client is initialized with the URL in the saga
6+ // (setApiConfig), but the auth token is not available at that point. Rather than modifying
7+ // the saga (which will be removed soon), this provider sets the auth header on the shared
8+ // Prometheus client once the token is available, and blocks rendering until it's ready.
9+ // TODO: Remove this provider once the saga is removed.
10+
511export default function PrometheusAuthProvider ( {
612 children,
713} : {
814 children : ReactNode ;
915} ) {
1016 const { userData } = useAuth ( ) ;
11- const [ ready , setReady ] = useState ( false ) ;
17+ const token = userData ?. token ;
1218
1319 useEffect ( ( ) => {
14- if ( userData ?. token ) {
15- setHeaders ( { Authorization : `Bearer ${ userData . token } ` } ) ;
16- setReady ( true ) ;
20+ if ( token ) {
21+ setHeaders ( { Authorization : `Bearer ${ token } ` } ) ;
1722 }
18- } , [ userData ?. token ] ) ;
23+ } , [ token ] ) ;
1924
20- if ( ! ready ) return null ;
25+ if ( ! token ) return null ;
2126
2227 return < > { children } </ > ;
2328}
You can’t perform that action at this time.
0 commit comments