| 
									
										
										
										
											2024-06-25 19:43:47 +08:00
										 |  |  | import { useEffect, useState } from 'react'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-21 04:01:21 +08:00
										 |  |  | import { RefreshEvent } from '@grafana/runtime'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | import { PanelChrome } from '@grafana/ui'; | 
					
						
							| 
									
										
										
										
											2025-04-03 16:26:24 +08:00
										 |  |  | import { t } from 'app/core/internationalization'; | 
					
						
							| 
									
										
										
										
											2021-06-01 19:52:08 +08:00
										 |  |  | import { applyPanelTimeOverrides } from 'app/features/dashboard/utils/panel'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | import { PanelRenderer } from 'app/features/panel/components/PanelRenderer'; | 
					
						
							| 
									
										
										
										
											2023-05-16 11:07:54 +08:00
										 |  |  | import { Options } from 'app/plugins/panel/table/panelcfg.gen'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { getTimeSrv } from '../../services/TimeSrv'; | 
					
						
							| 
									
										
										
										
											2024-12-06 23:01:51 +08:00
										 |  |  | import { DashboardModel } from '../../state/DashboardModel'; | 
					
						
							|  |  |  | import { PanelModel } from '../../state/PanelModel'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-12 16:54:41 +08:00
										 |  |  | import PanelHeaderCorner from './PanelHeaderCorner'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | import { usePanelLatestData } from './usePanelLatestData'; | 
					
						
							| 
									
										
										
										
											2022-01-31 13:02:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 21:36:42 +08:00
										 |  |  | export interface Props { | 
					
						
							| 
									
										
										
										
											2021-05-07 23:09:06 +08:00
										 |  |  |   width: number; | 
					
						
							|  |  |  |   height: number; | 
					
						
							|  |  |  |   panel: PanelModel; | 
					
						
							| 
									
										
										
										
											2021-06-01 19:52:08 +08:00
										 |  |  |   dashboard: DashboardModel; | 
					
						
							| 
									
										
										
										
											2021-05-07 23:09:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-01 19:52:08 +08:00
										 |  |  | export function PanelEditorTableView({ width, height, panel, dashboard }: Props) { | 
					
						
							|  |  |  |   const { data } = usePanelLatestData(panel, { withTransforms: true, withFieldConfig: false }, false); | 
					
						
							| 
									
										
										
										
											2023-05-16 11:07:54 +08:00
										 |  |  |   const [options, setOptions] = useState<Options>({ | 
					
						
							| 
									
										
										
										
											2021-05-07 23:09:06 +08:00
										 |  |  |     frameIndex: 0, | 
					
						
							|  |  |  |     showHeader: true, | 
					
						
							| 
									
										
										
										
											2021-09-08 15:44:41 +08:00
										 |  |  |     showTypeIcons: true, | 
					
						
							| 
									
										
										
										
											2021-05-07 23:09:06 +08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-01 19:52:08 +08:00
										 |  |  |   // Subscribe to panel event
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							| 
									
										
										
										
											2022-01-31 13:02:24 +08:00
										 |  |  |     const timeSrv = getTimeSrv(); | 
					
						
							| 
									
										
										
										
											2021-06-01 19:52:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const sub = panel.events.subscribe(RefreshEvent, () => { | 
					
						
							| 
									
										
										
										
											2022-08-26 21:36:42 +08:00
										 |  |  |       const timeData = applyPanelTimeOverrides(panel, timeSrv.timeRange()); | 
					
						
							| 
									
										
										
										
											2022-08-10 16:36:19 +08:00
										 |  |  |       panel.runAllPanelQueries({ | 
					
						
							|  |  |  |         dashboardUID: dashboard.uid, | 
					
						
							|  |  |  |         dashboardTimezone: dashboard.getTimezone(), | 
					
						
							|  |  |  |         timeData, | 
					
						
							|  |  |  |         width, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2021-06-01 19:52:08 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  |     return () => { | 
					
						
							|  |  |  |       sub.unsubscribe(); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, [panel, dashboard, width]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-07 23:09:06 +08:00
										 |  |  |   if (!data) { | 
					
						
							|  |  |  |     return null; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-03-08 23:11:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const errorMessage = data?.errors | 
					
						
							|  |  |  |     ? data.errors.length > 1 | 
					
						
							|  |  |  |       ? 'Multiple errors found. Click for more details' | 
					
						
							|  |  |  |       : data.errors[0].message | 
					
						
							|  |  |  |     : data?.error?.message; | 
					
						
							| 
									
										
										
										
											2021-05-07 23:09:06 +08:00
										 |  |  |   return ( | 
					
						
							|  |  |  |     <PanelChrome width={width} height={height} padding="none"> | 
					
						
							|  |  |  |       {(innerWidth, innerHeight) => ( | 
					
						
							| 
									
										
										
										
											2022-01-31 13:02:24 +08:00
										 |  |  |         <> | 
					
						
							| 
									
										
										
										
											2023-03-08 23:11:38 +08:00
										 |  |  |           <PanelHeaderCorner panel={panel} error={errorMessage} /> | 
					
						
							| 
									
										
										
										
											2022-01-31 13:02:24 +08:00
										 |  |  |           <PanelRenderer | 
					
						
							| 
									
										
										
										
											2025-04-03 16:26:24 +08:00
										 |  |  |             title={t('dashboard.panel-editor-table-view.title-raw-data', 'Raw data')} | 
					
						
							| 
									
										
										
										
											2022-01-31 13:02:24 +08:00
										 |  |  |             pluginId="table" | 
					
						
							|  |  |  |             width={innerWidth} | 
					
						
							|  |  |  |             height={innerHeight} | 
					
						
							|  |  |  |             data={data} | 
					
						
							|  |  |  |             options={options} | 
					
						
							|  |  |  |             onOptionsChange={setOptions} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         </> | 
					
						
							| 
									
										
										
										
											2021-05-07 23:09:06 +08:00
										 |  |  |       )} | 
					
						
							|  |  |  |     </PanelChrome> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | } |