Files
Terry Sutton 6015e53041 Chore/download types schemas (#29480)
Read which schemas to download types for
2024-09-24 14:34:44 -02:30

74 lines
2.7 KiB
TypeScript

import Link from 'next/link'
import { useState } from 'react'
import { toast } from 'sonner'
import { useParams } from 'common'
import { generateTypes } from 'data/projects/project-type-generation-query'
import { Button } from 'ui'
import ContentSnippet from '../ContentSnippet'
import { DOCS_CONTENT } from '../ProjectAPIDocs.constants'
import type { ContentProps } from './Content.types'
import { ExternalLink, Download } from 'lucide-react'
import { useProjectPostgrestConfigQuery } from 'data/config/project-postgrest-config-query'
const Entities = ({ language }: ContentProps) => {
const { ref } = useParams()
const [isGeneratingTypes, setIsGeneratingTypes] = useState(false)
const { data: config } = useProjectPostgrestConfigQuery({ projectRef: ref })
const onClickGenerateTypes = async () => {
try {
setIsGeneratingTypes(true)
const res = await generateTypes({ ref, included_schemas: config?.db_schema })
let element = document.createElement('a')
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(res.types))
element.setAttribute('download', 'supabase.ts')
element.style.display = 'none'
document.body.appendChild(element)
element.click()
document.body.removeChild(element)
toast.success(`Successfully generated types! File is being downloaded`)
} catch (error: any) {
toast.error(`Failed to generate types: ${error.message}`)
} finally {
setIsGeneratingTypes(false)
}
}
return (
<>
<ContentSnippet selectedLanguage={language} snippet={DOCS_CONTENT.entitiesIntroduction} />
<div>
<ContentSnippet selectedLanguage={language} snippet={DOCS_CONTENT.generatingTypes} />
<div className="flex items-center space-x-2 px-4 mt-3">
<Button asChild type="default" icon={<ExternalLink />}>
<Link
href="https://supabase.com/docs/guides/database/api/generating-types"
target="_blank"
rel="noreferrer"
>
Documentation
</Link>
</Button>
<Button
type="default"
disabled={isGeneratingTypes}
loading={isGeneratingTypes}
icon={<Download strokeWidth={1.5} />}
onClick={onClickGenerateTypes}
>
Generate and download types
</Button>
</div>
<p className="text-xs text-foreground-light px-4 mt-2">
Remember to re-generate and download this file as you make changes to your tables.
</p>
</div>
<ContentSnippet selectedLanguage={language} snippet={DOCS_CONTENT.graphql} />
</>
)
}
export default Entities