MessagesFormatters.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { PropsWithChildren } from 'react'
  2. import { unixMicroToIsoTimestamp } from './Messages.utils'
  3. import CopyButton from '@/components/ui/CopyButton'
  4. export const RowLayout = ({ children }: PropsWithChildren<{}>) => (
  5. <div className="flex h-full w-full items-center gap-4">{children}</div>
  6. )
  7. // renders a timestamp (either unix microsecond or iso timestamp)
  8. export const SelectionDetailedTimestampRow = ({
  9. value,
  10. hideCopy = false,
  11. }: {
  12. value: string | number
  13. hideCopy?: boolean
  14. }) => (
  15. <SelectionDetailedRow
  16. label="Timestamp"
  17. value={unixMicroToIsoTimestamp(value)}
  18. hideCopy={hideCopy}
  19. />
  20. )
  21. export const SelectionDetailedRow = ({
  22. label,
  23. value,
  24. valueRender,
  25. hideCopy = false,
  26. }: {
  27. label: string
  28. value: string
  29. valueRender?: React.ReactNode
  30. hideCopy?: boolean
  31. }) => {
  32. return (
  33. <div className="grid grid-cols-12 group">
  34. <span className="text-scale-900 text-sm col-span-4 whitespace-pre-wrap">{label}</span>
  35. <span className="text-scale-1200 text-sm col-span-6 whitespace-pre-wrap break-all">
  36. {valueRender ?? value}
  37. </span>
  38. {!hideCopy && (
  39. <CopyButton
  40. text={value}
  41. className="group-hover:opacity-100 opacity-0 my-auto transition col-span-2 h-4 w-4 px-0 py-0"
  42. type="text"
  43. title="Copy to clipboard"
  44. />
  45. )}
  46. </div>
  47. )
  48. }
  49. /*
  50. * JSON Syntax Highlighter
  51. *
  52. * for http response codes
  53. */
  54. export function jsonSyntaxHighlight(input: Object) {
  55. let json: string = JSON.stringify(input, null, 2)
  56. json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
  57. const newJson = json.replace(
  58. /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
  59. function (match) {
  60. var cls = 'number text-tomato-900'
  61. if (/^"/.test(match)) {
  62. if (/:$/.test(match)) {
  63. cls = 'key text-scale-1200'
  64. } else {
  65. cls = 'string text-brand-600'
  66. }
  67. } else if (/true|false/.test(match)) {
  68. cls = 'boolean text-blue-900'
  69. } else if (/null/.test(match)) {
  70. cls = 'null text-amber-1100'
  71. }
  72. return '<span class="' + cls + '">' + match + '</span>'
  73. }
  74. )
  75. const jsonWithLineWraps = newJson.split(`\n`).map((x) => {
  76. return `<span class="line text-xs">${x}</span>`
  77. })
  78. return jsonWithLineWraps.join('\n')
  79. }