AlertRuleItem.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React, { useCallback } from 'react';
  2. // @ts-ignore
  3. import Highlighter from 'react-highlight-words';
  4. import { Icon, IconName, Button, LinkButton, Card } from '@grafana/ui';
  5. import { AlertRule } from '../../types';
  6. export interface Props {
  7. rule: AlertRule;
  8. search: string;
  9. onTogglePause: () => void;
  10. }
  11. const AlertRuleItem = ({ rule, search, onTogglePause }: Props) => {
  12. const ruleUrl = `${rule.url}?editPanel=${rule.panelId}&tab=alert`;
  13. const renderText = useCallback(
  14. (text) => (
  15. <Highlighter
  16. key={text}
  17. highlightClassName="highlight-search-match"
  18. textToHighlight={text}
  19. searchWords={[search]}
  20. />
  21. ),
  22. [search]
  23. );
  24. return (
  25. <Card>
  26. <Card.Heading>{renderText(rule.name)}</Card.Heading>
  27. <Card.Figure>
  28. <Icon size="xl" name={rule.stateIcon as IconName} className={`alert-rule-item__icon ${rule.stateClass}`} />
  29. </Card.Figure>
  30. <Card.Meta>
  31. <span key="state">
  32. <span key="text" className={`${rule.stateClass}`}>
  33. {renderText(rule.stateText)}{' '}
  34. </span>
  35. for {rule.stateAge}
  36. </span>
  37. {rule.info ? renderText(rule.info) : null}
  38. </Card.Meta>
  39. <Card.Actions>
  40. <Button
  41. key="play"
  42. variant="secondary"
  43. icon={rule.state === 'paused' ? 'play' : 'pause'}
  44. onClick={onTogglePause}
  45. >
  46. {rule.state === 'paused' ? 'Resume' : 'Pause'}
  47. </Button>
  48. <LinkButton key="edit" variant="secondary" href={ruleUrl} icon="cog">
  49. Edit alert
  50. </LinkButton>
  51. </Card.Actions>
  52. </Card>
  53. );
  54. };
  55. export default AlertRuleItem;