mirror of https://github.com/grafana/grafana
Alerting: Add actions extension point to alert instances table view (#77900)
This PR adds a new [extension point][] to each row of the alert instances table. This allows plugins to add actions to a dropdown menu in the rightmost column of the table. Those actions are passed the alert instance so can use it for contextual handling. See https://github.com/grafana/machine-learning/pull/3461 for an example of how this can be used (e.g. by Grafana Sift here).pull/77895/head^2
parent
259ecb1793
commit
35b1e49024
@ -0,0 +1,65 @@ |
||||
import React, { ReactElement, useMemo, useState } from 'react'; |
||||
|
||||
import { PluginExtensionLink, PluginExtensionPoints } from '@grafana/data'; |
||||
import { getPluginLinkExtensions } from '@grafana/runtime'; |
||||
import { Dropdown, IconButton } from '@grafana/ui'; |
||||
import { ConfirmNavigationModal } from 'app/features/explore/extensions/ConfirmNavigationModal'; |
||||
import { Alert, CombinedRule } from 'app/types/unified-alerting'; |
||||
|
||||
import { AlertExtensionPointMenu } from './AlertInstanceExtensionPointMenu'; |
||||
|
||||
interface AlertInstanceExtensionPointProps { |
||||
rule?: CombinedRule; |
||||
instance: Alert; |
||||
extensionPointId: PluginExtensionPoints; |
||||
} |
||||
|
||||
export const AlertInstanceExtensionPoint = ({ |
||||
rule, |
||||
instance, |
||||
extensionPointId, |
||||
}: AlertInstanceExtensionPointProps): ReactElement | null => { |
||||
const [selectedExtension, setSelectedExtension] = useState<PluginExtensionLink | undefined>(); |
||||
const context = { instance, rule }; |
||||
const extensions = useExtensionLinks(context, extensionPointId); |
||||
|
||||
if (extensions.length === 0) { |
||||
return null; |
||||
} |
||||
|
||||
const menu = <AlertExtensionPointMenu extensions={extensions} onSelect={setSelectedExtension} />; |
||||
return ( |
||||
<> |
||||
<Dropdown placement="bottom-start" overlay={menu}> |
||||
<IconButton name="ellipsis-v" aria-label="Actions" variant="secondary" /> |
||||
</Dropdown> |
||||
{!!selectedExtension && !!selectedExtension.path && ( |
||||
<ConfirmNavigationModal |
||||
path={selectedExtension.path} |
||||
title={selectedExtension.title} |
||||
onDismiss={() => setSelectedExtension(undefined)} |
||||
/> |
||||
)} |
||||
</> |
||||
); |
||||
}; |
||||
|
||||
export type PluginExtensionAlertInstanceContext = { |
||||
rule?: CombinedRule; |
||||
instance: Alert; |
||||
}; |
||||
|
||||
function useExtensionLinks( |
||||
context: PluginExtensionAlertInstanceContext, |
||||
extensionPointId: PluginExtensionPoints |
||||
): PluginExtensionLink[] { |
||||
return useMemo(() => { |
||||
const { extensions } = getPluginLinkExtensions({ |
||||
extensionPointId, |
||||
context, |
||||
limitPerPlugin: 3, |
||||
}); |
||||
|
||||
return extensions; |
||||
}, [context, extensionPointId]); |
||||
} |
@ -0,0 +1,2 @@ |
||||
// We might want to customise this in future but right now the toolbar menu from the Explore view is fine.
|
||||
export { ToolbarExtensionPointMenu as AlertExtensionPointMenu } from 'app/features/explore/extensions/ToolbarExtensionPointMenu'; |
Loading…
Reference in new issue