Comment.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { css } from '@emotion/css';
  2. import DangerouslySetHtmlContent from 'dangerously-set-html-content';
  3. import React from 'react';
  4. import { GrafanaTheme2, renderMarkdown } from '@grafana/data';
  5. import { useStyles2 } from '@grafana/ui';
  6. import { Message } from './types';
  7. type Props = {
  8. message: Message;
  9. };
  10. export const Comment = ({ message }: Props) => {
  11. const styles = useStyles2(getStyles);
  12. let senderColor = '#34BA18';
  13. let senderName = 'System';
  14. let avatarUrl = '/public/img/grafana_icon.svg';
  15. if (message.userId > 0) {
  16. senderColor = '#19a2e7';
  17. senderName = message.user.login;
  18. avatarUrl = message.user.avatarUrl;
  19. }
  20. const timeColor = '#898989';
  21. const timeFormatted = new Date(message.created * 1000).toLocaleTimeString();
  22. const markdownContent = renderMarkdown(message.content, { breaks: true });
  23. return (
  24. <div className={styles.comment}>
  25. <div className={styles.avatarContainer}>
  26. <img src={avatarUrl} alt="User avatar" className={styles.avatar} />
  27. </div>
  28. <div>
  29. <div>
  30. <span style={{ color: senderColor }}>{senderName}</span>
  31. &nbsp;
  32. <span style={{ color: timeColor }}>{timeFormatted}</span>
  33. </div>
  34. <div>
  35. <DangerouslySetHtmlContent html={markdownContent} className={styles.content} />
  36. </div>
  37. </div>
  38. </div>
  39. );
  40. };
  41. const getStyles = (theme: GrafanaTheme2) => ({
  42. comment: css`
  43. margin-bottom: 10px;
  44. padding-top: 3px;
  45. padding-bottom: 3px;
  46. word-break: break-word;
  47. display: flex;
  48. flex-direction: row;
  49. align-items: top;
  50. :hover {
  51. background-color: #1e1f24;
  52. }
  53. blockquote {
  54. padding: 0 0 0 10px;
  55. margin: 0 0 10px;
  56. }
  57. `,
  58. avatarContainer: css`
  59. align-self: left;
  60. margin-top: 6px;
  61. margin-right: 10px;
  62. `,
  63. avatar: css`
  64. width: 30px;
  65. height: 30px;
  66. `,
  67. content: css`
  68. display: block;
  69. overflow: hidden;
  70. p {
  71. margin: 0;
  72. padding: 0;
  73. }
  74. blockquote p {
  75. font-size: 14px;
  76. padding-top: 4px;
  77. }
  78. a {
  79. color: #43c57e;
  80. }
  81. a:hover {
  82. text-decoration: underline;
  83. }
  84. `,
  85. });