CommentManager.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React, { PureComponent } from 'react';
  2. import { Unsubscribable } from 'rxjs';
  3. import { isLiveChannelMessageEvent, LiveChannelScope } from '@grafana/data';
  4. import { getBackendSrv, getGrafanaLiveSrv } from '@grafana/runtime';
  5. import { CommentView } from './CommentView';
  6. import { Message, MessagePacket } from './types';
  7. export interface Props {
  8. objectType: string;
  9. objectId: string;
  10. }
  11. export interface State {
  12. messages: Message[];
  13. value: string;
  14. }
  15. export class CommentManager extends PureComponent<Props, State> {
  16. subscription?: Unsubscribable;
  17. packetCounter = 0;
  18. constructor(props: Props) {
  19. super(props);
  20. this.state = {
  21. messages: [],
  22. value: '',
  23. };
  24. }
  25. async componentDidMount() {
  26. const resp = await getBackendSrv().post('/api/comments/get', {
  27. objectType: this.props.objectType,
  28. objectId: this.props.objectId,
  29. });
  30. this.packetCounter++;
  31. this.setState({
  32. messages: resp.comments,
  33. });
  34. this.updateSubscription();
  35. }
  36. getLiveChannel = () => {
  37. const live = getGrafanaLiveSrv();
  38. if (!live) {
  39. console.error('Grafana live not running, enable "live" feature toggle');
  40. return undefined;
  41. }
  42. const address = this.getLiveAddress();
  43. if (!address) {
  44. return undefined;
  45. }
  46. return live.getStream<MessagePacket>(address);
  47. };
  48. getLiveAddress = () => {
  49. return {
  50. scope: LiveChannelScope.Grafana,
  51. namespace: 'comment',
  52. path: `${this.props.objectType}/${this.props.objectId}`,
  53. };
  54. };
  55. updateSubscription = () => {
  56. if (this.subscription) {
  57. this.subscription.unsubscribe();
  58. this.subscription = undefined;
  59. }
  60. const channel = this.getLiveChannel();
  61. if (channel) {
  62. this.subscription = channel.subscribe({
  63. next: (msg) => {
  64. if (isLiveChannelMessageEvent(msg)) {
  65. const { commentCreated } = msg.message;
  66. if (commentCreated) {
  67. this.setState((prevState) => ({
  68. messages: [...prevState.messages, commentCreated],
  69. }));
  70. this.packetCounter++;
  71. }
  72. }
  73. },
  74. });
  75. }
  76. };
  77. addComment = async (comment: string): Promise<boolean> => {
  78. const response = await getBackendSrv().post('/api/comments/create', {
  79. objectType: this.props.objectType,
  80. objectId: this.props.objectId,
  81. content: comment,
  82. });
  83. // TODO: set up error handling
  84. console.log(response);
  85. return true;
  86. };
  87. render() {
  88. return (
  89. <CommentView comments={this.state.messages} packetCounter={this.packetCounter} addComment={this.addComment} />
  90. );
  91. }
  92. }