MessageSelection.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // @ts-nocheck
  2. import { useParams } from 'common'
  3. import { X } from 'lucide-react'
  4. import { useMemo } from 'react'
  5. import { toast } from 'sonner'
  6. import { Button, cn, copyToClipboard } from 'ui'
  7. import type { LogData } from './Messages.types'
  8. import { SelectedRealtimeMessagePanel } from './SelectedRealtimeMessagePanel'
  9. import CopyButton from '@/components/ui/CopyButton'
  10. import { ShortcutTooltip } from '@/components/ui/ShortcutTooltip'
  11. import { useSendEventMutation } from '@/data/telemetry/send-event-mutation'
  12. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  13. import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
  14. import { useShortcut } from '@/state/shortcuts/useShortcut'
  15. export interface MessageSelectionProps {
  16. log: LogData | null
  17. onClose: () => void
  18. }
  19. const MessageSelection = ({ log, onClose }: MessageSelectionProps) => {
  20. const selectionText = useMemo(() => {
  21. return JSON.stringify(log, null, 2)
  22. }, [log])
  23. const { ref } = useParams()
  24. const { data: org } = useSelectedOrganizationQuery()
  25. const { mutate: sendEvent } = useSendEventMutation()
  26. const handleCopy = () => {
  27. if (!log) return
  28. copyToClipboard(selectionText)
  29. toast.success('Message copied to clipboard')
  30. }
  31. useShortcut(SHORTCUT_IDS.INSPECTOR_COPY_MESSAGE, handleCopy, {
  32. enabled: !!log,
  33. ignoreInputs: true,
  34. registerInCommandMenu: true,
  35. })
  36. const copyButton = (
  37. <CopyButton
  38. text={selectionText}
  39. type="default"
  40. title="Copy log to clipboard"
  41. onClick={() => {
  42. sendEvent({
  43. action: 'realtime_inspector_copy_message_clicked',
  44. groups: { project: ref ?? 'Unknown', organization: org?.slug ?? 'Unknown' },
  45. })
  46. }}
  47. />
  48. )
  49. return (
  50. <div className={cn('relative flex h-full grow flex-col border-l overflow-y-scroll bg-200')}>
  51. <div
  52. className={cn(
  53. 'absolute flex h-full w-full flex-col items-center justify-center gap-2 bg-200 text-center opacity-0 transition-all',
  54. log ? 'z-0 opacity-0' : 'z-10 opacity-100'
  55. )}
  56. >
  57. <div
  58. className={cn(
  59. 'flex w-full max-w-sm scale-95 flex-col items-center justify-center gap-6 text-center opacity-0 transition-all delay-300 duration-500',
  60. log ? 'mt-0 scale-95 opacity-0' : 'mt-8 scale-100 opacity-100'
  61. )}
  62. >
  63. <div className="relative flex h-4 w-32 items-center rounded-sm border border-default px-2">
  64. <div className="h-0.5 w-2/3 rounded-full bg-overlay-hover"></div>
  65. <div className="absolute right-1 -bottom-4">
  66. <svg
  67. xmlns="http://www.w3.org/2000/svg"
  68. className="h-6 w-6"
  69. fill="none"
  70. viewBox="0 0 24 24"
  71. stroke="currentColor"
  72. strokeWidth="1"
  73. >
  74. <path
  75. strokeLinecap="round"
  76. strokeLinejoin="round"
  77. d="M15 15l-2 5L9 9l11 4-5 2zm0 0l5 5M7.188 2.239l.777 2.897M5.136 7.965l-2.898-.777M13.95 4.05l-2.122 2.122m-5.657 5.656l-2.12 2.122"
  78. />
  79. </svg>
  80. </div>
  81. </div>
  82. <div className="flex flex-col gap-1">
  83. <h3 className="text-sm text-foreground">Select a message</h3>
  84. <p className="text-xs text-foreground-lighter">
  85. Click on a message on the left to view details.
  86. </p>
  87. </div>
  88. </div>
  89. </div>
  90. <div className="relative h-px grow">
  91. <div className="pt-4 flex flex-col gap-4">
  92. <div className="px-4 flex flex-row justify-between items-center">
  93. <div className="transition">
  94. {log ? (
  95. <ShortcutTooltip shortcutId={SHORTCUT_IDS.INSPECTOR_COPY_MESSAGE} side="bottom">
  96. {copyButton}
  97. </ShortcutTooltip>
  98. ) : (
  99. copyButton
  100. )}
  101. </div>
  102. <Button
  103. type="text"
  104. className="cursor-pointer transition hover:text-scale-1200 h-8 w-8 px-0 py-0 flex items-center justify-center"
  105. onClick={onClose}
  106. >
  107. <X size={14} strokeWidth={2} className="text-scale-900" />
  108. </Button>
  109. </div>
  110. <div className="h-px w-full bg-scale-600 rounded-sm" />
  111. </div>
  112. <div className="flex flex-col space-y-6 py-4">
  113. {log && <SelectedRealtimeMessagePanel log={log} />}
  114. </div>
  115. </div>
  116. </div>
  117. )
  118. }
  119. export default MessageSelection