ContentSnippet.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // @ts-nocheck
  2. import { useParams } from 'common'
  3. import { PropsWithChildren } from 'react'
  4. import { SimpleCodeBlock } from 'ui-patterns/SimpleCodeBlock'
  5. import { Markdown } from '../Markdown'
  6. import { useSendEventMutation } from '@/data/telemetry/send-event-mutation'
  7. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  8. interface ContentSnippetProps {
  9. apikey?: string
  10. endpoint?: string
  11. selectedLanguage: 'js' | 'bash'
  12. snippet: {
  13. key: string
  14. category: string
  15. title: string
  16. description?: string
  17. js?: (apikey?: string, endpoint?: string) => string
  18. bash?: (apikey?: string, endpoint?: string) => string
  19. }
  20. }
  21. const ContentSnippet = ({
  22. apikey,
  23. endpoint,
  24. selectedLanguage,
  25. snippet,
  26. children,
  27. }: PropsWithChildren<ContentSnippetProps>) => {
  28. const { ref: projectRef } = useParams()
  29. const { data: org } = useSelectedOrganizationQuery()
  30. const { mutate: sendEvent } = useSendEventMutation()
  31. const codeSnippet = snippet[selectedLanguage]?.(apikey, endpoint).replaceAll(
  32. '[ref]',
  33. projectRef ?? ''
  34. )
  35. const handleCopy = () => {
  36. sendEvent({
  37. action: 'api_docs_code_copy_button_clicked',
  38. properties: {
  39. title: snippet.title,
  40. selectedLanguage,
  41. },
  42. groups: {
  43. project: projectRef ?? 'Unknown',
  44. organization: org?.slug ?? 'Unknown',
  45. },
  46. })
  47. }
  48. return (
  49. <div className="space-y-4 py-6 pb-2 last:pb-6">
  50. <div className="px-4 space-y-4">
  51. <h2 id={snippet.key} tabIndex={-1} className="doc-heading">
  52. {snippet.title}
  53. </h2>
  54. {snippet.description !== undefined && (
  55. <div className="doc-section">
  56. <article className="text text-sm text-foreground-light">
  57. <Markdown
  58. className="max-w-none"
  59. content={snippet.description.replaceAll('[ref]', projectRef ?? '_')}
  60. />
  61. </article>
  62. </div>
  63. )}
  64. </div>
  65. {children}
  66. {codeSnippet !== undefined && (
  67. <div className="px-4 codeblock-container">
  68. <div className="bg rounded-sm p-2">
  69. <SimpleCodeBlock className={selectedLanguage} onCopy={handleCopy}>
  70. {codeSnippet}
  71. </SimpleCodeBlock>
  72. </div>
  73. </div>
  74. )}
  75. </div>
  76. )
  77. }
  78. export default ContentSnippet