| 
									
										
										
										
											2025-05-23 00:12:17 +08:00
										 |  |  | import { DEFAULT_LANGUAGE, PSEUDO_LOCALE } from '@grafana/i18n'; | 
					
						
							| 
									
										
										
										
											2025-05-09 01:03:51 +08:00
										 |  |  | import store from 'app/core/store'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { sendAppNotification } from './core/copy/appNotification'; | 
					
						
							| 
									
										
										
										
											2025-05-23 00:12:17 +08:00
										 |  |  | import { PreferencesService } from './core/services/PreferencesService'; | 
					
						
							| 
									
										
										
										
											2025-07-09 12:15:33 +08:00
										 |  |  | import { AppNotificationSeverity } from './types/appNotifications'; | 
					
						
							| 
									
										
										
										
											2025-05-09 01:03:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const STORAGE_MOCK_API_KEY = 'grafana.dev.mockApi'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const currentMockApiState = () => { | 
					
						
							|  |  |  |   return store.getBool(STORAGE_MOCK_API_KEY, false); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const toggleMockApiAndReload = () => { | 
					
						
							|  |  |  |   const currentState = currentMockApiState(); | 
					
						
							|  |  |  |   store.set(STORAGE_MOCK_API_KEY, String(!currentState)); | 
					
						
							|  |  |  |   const action = currentState ? 'Disabling' : 'Enabling'; | 
					
						
							|  |  |  |   sendAppNotification(`${action} Mock API`, 'Reloading...', AppNotificationSeverity.Info); | 
					
						
							|  |  |  |   setTimeout(() => { | 
					
						
							|  |  |  |     window.location.reload(); | 
					
						
							|  |  |  |   }, 200); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const potentiallySetupMockApi = async () => { | 
					
						
							|  |  |  |   const mockApiEnabled = currentMockApiState(); | 
					
						
							|  |  |  |   if (process.env.NODE_ENV === 'development' && mockApiEnabled) { | 
					
						
							| 
									
										
										
										
											2025-05-14 21:50:34 +08:00
										 |  |  |     const { default: worker } = await import('@grafana/test-utils/worker'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // TODO: Generalise and move Alerting handlers into @grafana/test-utils or @grafana/alerting package
 | 
					
						
							|  |  |  |     const { default: alertingHandlers } = await import('./features/alerting/unified/mocks/server/all-handlers'); | 
					
						
							|  |  |  |     worker.use(...alertingHandlers); | 
					
						
							| 
									
										
										
										
											2025-05-09 01:03:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     worker.start({ onUnhandledRequest: 'bypass' }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const notifyIfMockApiEnabled = () => { | 
					
						
							|  |  |  |   if (process.env.NODE_ENV === 'development' && currentMockApiState()) { | 
					
						
							|  |  |  |     sendAppNotification( | 
					
						
							|  |  |  |       'Mock API currently enabled', | 
					
						
							|  |  |  |       'Some network requests will be intercepted', | 
					
						
							|  |  |  |       AppNotificationSeverity.Info | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2025-05-23 00:12:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const togglePseudoLocale = async () => { | 
					
						
							|  |  |  |   const prefsService = new PreferencesService('user'); | 
					
						
							|  |  |  |   const prefs = await prefsService.load(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const isPseudoEnabled = prefs.language === PSEUDO_LOCALE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const action = isPseudoEnabled ? 'Disabling' : 'Enabling'; | 
					
						
							|  |  |  |   sendAppNotification(`${action} pseudo locale`, 'Reloading...', AppNotificationSeverity.Info); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await prefsService.update({ | 
					
						
							|  |  |  |     ...prefs, | 
					
						
							|  |  |  |     language: isPseudoEnabled ? DEFAULT_LANGUAGE : PSEUDO_LOCALE, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   setTimeout(() => { | 
					
						
							|  |  |  |     window.location.reload(); | 
					
						
							|  |  |  |   }, 200); | 
					
						
							|  |  |  | }; |