Entities.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { useParams } from 'common'
  2. import { Download } from 'lucide-react'
  3. import { useState } from 'react'
  4. import { toast } from 'sonner'
  5. import { Button } from 'ui'
  6. import ContentSnippet from '../ContentSnippet'
  7. import { DOCS_CONTENT } from '../ProjectAPIDocs.constants'
  8. import type { ContentProps } from './Content.types'
  9. import { DocsButton } from '@/components/ui/DocsButton'
  10. import { useProjectPostgrestConfigQuery } from '@/data/config/project-postgrest-config-query'
  11. import { generateTypes } from '@/data/projects/project-type-generation-query'
  12. import { DOCS_URL } from '@/lib/constants'
  13. export const Entities = ({ language }: ContentProps) => {
  14. const { ref } = useParams()
  15. const [isGeneratingTypes, setIsGeneratingTypes] = useState(false)
  16. const { data: config } = useProjectPostgrestConfigQuery({ projectRef: ref })
  17. const onClickGenerateTypes = async () => {
  18. try {
  19. setIsGeneratingTypes(true)
  20. const res = await generateTypes({ ref, included_schemas: config?.db_schema })
  21. let element = document.createElement('a')
  22. element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(res.types))
  23. element.setAttribute('download', 'briven.ts')
  24. element.style.display = 'none'
  25. document.body.appendChild(element)
  26. element.click()
  27. document.body.removeChild(element)
  28. toast.success(`Successfully generated types! File is being downloaded`)
  29. } catch (error: any) {
  30. toast.error(`Failed to generate types: ${error.message}`)
  31. } finally {
  32. setIsGeneratingTypes(false)
  33. }
  34. }
  35. return (
  36. <>
  37. <ContentSnippet selectedLanguage={language} snippet={DOCS_CONTENT.entitiesIntroduction} />
  38. <div>
  39. <ContentSnippet selectedLanguage={language} snippet={DOCS_CONTENT.generatingTypes} />
  40. <div className="flex items-center gap-x-2 px-4 mt-3">
  41. <DocsButton href={`${DOCS_URL}/guides/database/api/generating-types`} />
  42. <Button
  43. type="default"
  44. disabled={isGeneratingTypes}
  45. loading={isGeneratingTypes}
  46. icon={<Download strokeWidth={1.5} />}
  47. onClick={onClickGenerateTypes}
  48. >
  49. Generate and download types
  50. </Button>
  51. </div>
  52. <p className="text-xs text-foreground-light px-4 mt-2">
  53. Remember to re-generate and download this file as you make changes to your tables.
  54. </p>
  55. </div>
  56. <ContentSnippet selectedLanguage={language} snippet={DOCS_CONTENT.graphql} />
  57. </>
  58. )
  59. }