SnippetRow.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { X } from 'lucide-react'
  2. import React from 'react'
  3. import { Button, HoverCard, HoverCardContent, HoverCardTrigger } from 'ui'
  4. import { CodeBlock } from 'ui-patterns/CodeBlock'
  5. import { type SqlSnippet } from './AIAssistant.types'
  6. export const getSnippetLabel = (snippet: SqlSnippet, index: number): string => {
  7. if (typeof snippet === 'string') {
  8. return `Snippet ${index + 1}`
  9. }
  10. return snippet.label
  11. }
  12. export const getSnippetContent = (snippet: SqlSnippet): string => {
  13. if (typeof snippet === 'string') {
  14. return snippet
  15. }
  16. return snippet.content
  17. }
  18. interface SnippetRowProps {
  19. snippets: SqlSnippet[]
  20. onRemoveSnippet?: (index: number) => void
  21. className?: string
  22. }
  23. export const SnippetRow: React.FC<SnippetRowProps> = ({
  24. snippets,
  25. onRemoveSnippet,
  26. className = '',
  27. }) => {
  28. if (!snippets || snippets.length === 0) return null
  29. return (
  30. <div className={`w-full overflow-x-auto flex ${className}`}>
  31. {snippets.map((snippet, idx) => (
  32. <HoverCard key={idx}>
  33. <HoverCardTrigger asChild>
  34. <div
  35. tabIndex={0}
  36. className="border bg inline-flex gap-1 items-center shrink-0 py-1 pl-2 rounded-full pr-1 text-xs cursor-pointer"
  37. >
  38. {getSnippetLabel(snippet, idx)}
  39. {onRemoveSnippet && (
  40. <Button
  41. size="tiny"
  42. type="text"
  43. className="h-4! w-4! rounded-full p-0"
  44. onClick={(e) => {
  45. e.stopPropagation()
  46. onRemoveSnippet(idx)
  47. }}
  48. aria-label={`Remove snippet ${idx + 1}`}
  49. icon={<X strokeWidth={1.5} className="h-3! w-3!" />}
  50. />
  51. )}
  52. </div>
  53. </HoverCardTrigger>
  54. <HoverCardContent className="w-96 max-h-64 overflow-auto p-0">
  55. <CodeBlock
  56. hideLineNumbers
  57. className="text-xs font-mono whitespace-pre-wrap wrap-break-word p-2 border-0"
  58. language="sql"
  59. >
  60. {getSnippetContent(snippet)}
  61. </CodeBlock>
  62. </HoverCardContent>
  63. </HoverCard>
  64. ))}
  65. </div>
  66. )
  67. }