| 
									
										
										
										
											2021-04-28 21:22:28 +08:00
										 |  |  | import { css } from '@emotion/css'; | 
					
						
							| 
									
										
										
										
											2024-06-25 19:43:47 +08:00
										 |  |  | import { useMemo } from 'react'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-03 15:45:54 +08:00
										 |  |  | import { GrafanaTheme2 } from '@grafana/data'; | 
					
						
							| 
									
										
										
										
											2025-05-23 16:34:35 +08:00
										 |  |  | import { TFunction, Trans, useTranslate } from '@grafana/i18n'; | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  | import { Grid, Modal, useStyles2, Text } from '@grafana/ui'; | 
					
						
							| 
									
										
										
										
											2023-01-26 20:07:32 +08:00
										 |  |  | import { getModKey } from 'app/core/utils/browser'; | 
					
						
							| 
									
										
										
										
											2019-10-02 21:57:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-15 15:17:14 +08:00
										 |  |  | const getShortcuts = (modKey: string, t: TFunction) => { | 
					
						
							| 
									
										
										
										
											2023-08-30 21:08:18 +08:00
										 |  |  |   return [ | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       category: t('help-modal.shortcuts-category.global', 'Global'), | 
					
						
							|  |  |  |       shortcuts: [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['g', 'h'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.go-to-home-dashboard', 'Go to Home Dashboard'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['g', 'd'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.go-to-dashboards', 'Go to Dashboards'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { keys: ['g', 'e'], description: t('help-modal.shortcuts-description.go-to-explore', 'Go to Explore') }, | 
					
						
							|  |  |  |         { keys: ['g', 'p'], description: t('help-modal.shortcuts-description.go-to-profile', 'Go to Profile') }, | 
					
						
							|  |  |  |         { keys: [`${modKey} + k`], description: t('help-modal.shortcuts-description.open-search', 'Open search') }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['esc'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.exit-edit/setting-views', 'Exit edit/setting views'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-04-02 17:41:46 +08:00
										 |  |  |           keys: ['?'], | 
					
						
							| 
									
										
										
										
											2023-08-30 21:08:18 +08:00
										 |  |  |           description: t('help-modal.shortcuts-description.show-all-shortcuts', 'Show all keyboard shortcuts'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { keys: ['c', 't'], description: t('help-modal.shortcuts-description.change-theme', 'Change theme') }, | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |       category: t('help-modal.shortcuts-category.time-range', 'Time range'), | 
					
						
							|  |  |  |       shortcuts: [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['t', 'z'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.zoom-out-time-range', 'Zoom out time range'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['t', '←'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.move-time-range-back', 'Move time range back'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['t', '→'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.move-time-range-forward', 'Move time range forward'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['t', 'a'], | 
					
						
							|  |  |  |           description: t( | 
					
						
							|  |  |  |             'help-modal.shortcuts-description.make-time-range-permanent', | 
					
						
							|  |  |  |             'Make time range absolute/permanent' | 
					
						
							|  |  |  |           ), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['t', 'c'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.copy-time-range', 'Copy time range'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['t', 'v'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.paste-time-range', 'Paste time range'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2023-08-30 21:08:18 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |       category: t('help-modal.shortcuts-category.dashboard', 'Dashboard'), | 
					
						
							|  |  |  |       shortcuts: [ | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  |         { | 
					
						
							|  |  |  |           keys: [`${modKey} + s`], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.save-dashboard', 'Save dashboard'), | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2023-08-30 21:08:18 +08:00
										 |  |  |         { | 
					
						
							|  |  |  |           keys: ['d', 'r'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.refresh-all-panels', 'Refresh all panels'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['d', 's'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.dashboard-settings', 'Dashboard settings'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['d', 'v'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.toggle-active-mode', 'Toggle in-active / view mode'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['d', 'k'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.toggle-kiosk', 'Toggle kiosk mode (hides top nav)'), | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  |         { | 
					
						
							|  |  |  |           keys: ['d', '⇧ + e'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.expand-all-rows', 'Expand all rows'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['d', '⇧ + c'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.collapse-all-rows', 'Collapse all rows'), | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2023-08-30 21:08:18 +08:00
										 |  |  |         { | 
					
						
							|  |  |  |           keys: ['d', 'a'], | 
					
						
							|  |  |  |           description: t( | 
					
						
							|  |  |  |             'help-modal.shortcuts-description.toggle-auto-fit', | 
					
						
							|  |  |  |             'Toggle auto fit panels (experimental feature)' | 
					
						
							|  |  |  |           ), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: [`${modKey} + o`], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.toggle-graph-crosshair', 'Toggle shared graph crosshair'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['d', 'l'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.toggle-all-panel-legends', 'Toggle all panel legends'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['d', 'x'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.toggle-exemplars', 'Toggle exemplars in all panel'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  |       category: t('help-modal.shortcuts-category.focused-panel', 'Focused panel'), | 
					
						
							| 
									
										
										
										
											2023-08-30 21:08:18 +08:00
										 |  |  |       shortcuts: [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['e'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.toggle-panel-edit', 'Toggle panel edit view'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['v'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.toggle-panel-fullscreen', 'Toggle panel fullscreen view'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['p', 's'], | 
					
						
							| 
									
										
										
										
											2025-03-27 22:11:26 +08:00
										 |  |  |           description: t('help-modal.shortcuts-description.open-shared-modal', 'Share panel snapshot'), | 
					
						
							| 
									
										
										
										
											2023-08-30 21:08:18 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { keys: ['p', 'd'], description: t('help-modal.shortcuts-description.duplicate-panel', 'Duplicate Panel') }, | 
					
						
							|  |  |  |         { keys: ['p', 'r'], description: t('help-modal.shortcuts-description.remove-panel', 'Remove Panel') }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           keys: ['p', 'l'], | 
					
						
							|  |  |  |           description: t('help-modal.shortcuts-description.toggle-panel-legend', 'Toggle panel legend'), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   ]; | 
					
						
							| 
									
										
										
										
											2023-03-30 23:46:43 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-10-02 21:57:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-28 21:22:28 +08:00
										 |  |  | export interface HelpModalProps { | 
					
						
							|  |  |  |   onDismiss: () => void; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-02 21:57:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-28 21:22:28 +08:00
										 |  |  | export const HelpModal = ({ onDismiss }: HelpModalProps): JSX.Element => { | 
					
						
							|  |  |  |   const styles = useStyles2(getStyles); | 
					
						
							| 
									
										
										
										
											2025-05-15 15:17:14 +08:00
										 |  |  |   const { t } = useTranslate(); | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-26 20:07:32 +08:00
										 |  |  |   const modKey = useMemo(() => getModKey(), []); | 
					
						
							| 
									
										
										
										
											2025-05-15 15:17:14 +08:00
										 |  |  |   const shortcuts = useMemo(() => getShortcuts(modKey, t), [modKey, t]); | 
					
						
							| 
									
										
										
										
											2021-04-28 21:22:28 +08:00
										 |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2023-08-30 21:08:18 +08:00
										 |  |  |     <Modal title={t('help-modal.title', 'Shortcuts')} isOpen onDismiss={onDismiss} onClickBackdrop={onDismiss}> | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  |       <Grid columns={{ xs: 1, sm: 2 }} gap={3} tabIndex={0}> | 
					
						
							|  |  |  |         {Object.values(shortcuts).map(({ category, shortcuts }) => ( | 
					
						
							|  |  |  |           <section key={category}> | 
					
						
							|  |  |  |             <table className={styles.table}> | 
					
						
							|  |  |  |               <caption> | 
					
						
							|  |  |  |                 <Text element="p" variant="h5"> | 
					
						
							|  |  |  |                   {category} | 
					
						
							|  |  |  |                 </Text> | 
					
						
							|  |  |  |               </caption> | 
					
						
							|  |  |  |               <thead className="sr-only"> | 
					
						
							| 
									
										
										
										
											2021-04-28 21:22:28 +08:00
										 |  |  |                 <tr> | 
					
						
							| 
									
										
										
										
											2024-11-21 20:59:14 +08:00
										 |  |  |                   <th> | 
					
						
							|  |  |  |                     <Trans i18nKey="help-modal.column-headers.keys">Keys</Trans> | 
					
						
							|  |  |  |                   </th> | 
					
						
							|  |  |  |                   <th> | 
					
						
							|  |  |  |                     <Trans i18nKey="help-modal.column-headers.description">Description</Trans> | 
					
						
							|  |  |  |                   </th> | 
					
						
							| 
									
										
										
										
											2021-04-28 21:22:28 +08:00
										 |  |  |                 </tr> | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  |               </thead> | 
					
						
							|  |  |  |               <tbody> | 
					
						
							|  |  |  |                 {shortcuts.map(({ keys, description }) => ( | 
					
						
							|  |  |  |                   <tr key={keys.join()}> | 
					
						
							|  |  |  |                     <td className={styles.keys}> | 
					
						
							|  |  |  |                       {keys.map((key) => ( | 
					
						
							|  |  |  |                         <Key key={key}>{key}</Key> | 
					
						
							| 
									
										
										
										
											2021-04-28 21:22:28 +08:00
										 |  |  |                       ))} | 
					
						
							|  |  |  |                     </td> | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  |                     <td> | 
					
						
							|  |  |  |                       <Text variant="bodySmall" element="p"> | 
					
						
							|  |  |  |                         {description} | 
					
						
							|  |  |  |                       </Text> | 
					
						
							|  |  |  |                     </td> | 
					
						
							| 
									
										
										
										
											2019-10-02 21:57:40 +08:00
										 |  |  |                   </tr> | 
					
						
							| 
									
										
										
										
											2021-04-28 21:22:28 +08:00
										 |  |  |                 ))} | 
					
						
							|  |  |  |               </tbody> | 
					
						
							|  |  |  |             </table> | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  |           </section> | 
					
						
							| 
									
										
										
										
											2021-04-28 21:22:28 +08:00
										 |  |  |         ))} | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  |       </Grid> | 
					
						
							| 
									
										
										
										
											2021-04-28 21:22:28 +08:00
										 |  |  |     </Modal> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  | interface KeyProps { | 
					
						
							|  |  |  |   children: string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const Key = ({ children }: KeyProps) => { | 
					
						
							|  |  |  |   const styles = useStyles2(getStyles); | 
					
						
							|  |  |  |   const displayText = useMemo(() => replaceCustomKeyNames(children), [children]); | 
					
						
							|  |  |  |   const displayElement = <span dangerouslySetInnerHTML={{ __html: displayText }}></span>; | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <kbd className={styles.shortcutTableKey}> | 
					
						
							|  |  |  |       <Text variant="code">{displayElement}</Text> | 
					
						
							|  |  |  |     </kbd> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function replaceCustomKeyNames(key: string) { | 
					
						
							|  |  |  |   let displayName; | 
					
						
							|  |  |  |   let srName; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (key.includes('ctrl')) { | 
					
						
							|  |  |  |     displayName = 'ctrl'; | 
					
						
							|  |  |  |     srName = 'Control'; | 
					
						
							|  |  |  |   } else if (key.includes('esc')) { | 
					
						
							|  |  |  |     displayName = 'esc'; | 
					
						
							|  |  |  |     srName = 'Escape'; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return key; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return key.replace( | 
					
						
							|  |  |  |     displayName, | 
					
						
							|  |  |  |     `<span class="sr-only">${srName}</span><span aria-hidden="true" role="none">${displayName}</span>` | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-03 15:45:54 +08:00
										 |  |  | function getStyles(theme: GrafanaTheme2) { | 
					
						
							| 
									
										
										
										
											2021-04-28 21:22:28 +08:00
										 |  |  |   return { | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  |     table: css({ | 
					
						
							|  |  |  |       borderCollapse: 'separate', | 
					
						
							|  |  |  |       borderSpacing: theme.spacing(2), | 
					
						
							|  |  |  |       '& caption': { | 
					
						
							|  |  |  |         captionSide: 'top', | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2024-02-12 18:54:24 +08:00
										 |  |  |     }), | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  |     keys: css({ | 
					
						
							|  |  |  |       textAlign: 'end', | 
					
						
							| 
									
										
										
										
											2024-02-12 18:54:24 +08:00
										 |  |  |       whiteSpace: 'nowrap', | 
					
						
							| 
									
										
										
										
											2024-03-12 00:43:55 +08:00
										 |  |  |       minWidth: 83, // To match column widths with the widest
 | 
					
						
							| 
									
										
										
										
											2024-02-12 18:54:24 +08:00
										 |  |  |     }), | 
					
						
							|  |  |  |     shortcutTableKey: css({ | 
					
						
							|  |  |  |       display: 'inline-block', | 
					
						
							|  |  |  |       textAlign: 'center', | 
					
						
							|  |  |  |       marginRight: theme.spacing(0.5), | 
					
						
							|  |  |  |       padding: '3px 5px', | 
					
						
							|  |  |  |       lineHeight: '10px', | 
					
						
							|  |  |  |       verticalAlign: 'middle', | 
					
						
							|  |  |  |       border: `solid 1px ${theme.colors.border.medium}`, | 
					
						
							|  |  |  |       borderRadius: theme.shape.radius.default, | 
					
						
							|  |  |  |       color: theme.colors.text.primary, | 
					
						
							|  |  |  |       backgroundColor: theme.colors.background.secondary, | 
					
						
							|  |  |  |     }), | 
					
						
							| 
									
										
										
										
											2021-04-28 21:22:28 +08:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-10-02 21:57:40 +08:00
										 |  |  | } |