ResourceContent.tsx 2.4 KB

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