Files
supabase/studio/components/interfaces/TableGridEditor/TableDefinition.tsx
T
8057309e51 chore: upgrade next 13 + react 18 (#17839)
* update deps + image codemod (studio)

* update next links (studio)

* update deps

* update links (ui)

* remove next-transpile-modules

* move next-themes dependency

* chore: update ConfirmDialog

* chore: remove old ConfirmModal js file. migrated to TS

* dependency wrangling

* remove empty page

* update next links (www)

* First run bump react-data-grid-v7 beta 4

* fix package-lock.json

* more deps wrangling

* update recharts

* update sentry options

* fix some broken things in www

* studio fixes

* fix graphiql

* fix studio build

* fix menu hydration

* small build error

* update turbo

* fix www typescript errors

* docs image codemod

* links codemod docs

* fix docs typescript errors

* move useConsent to ui to prevent circular deps

* Fix links

* Fix homepage

* Fix links

* move studio/ to apps/

* Revert "move studio/ to apps/"

This reverts commit 1b0a985fcb.

* disable outputFileTracingRoot

* remove outputFileTracingRoot

* fix homepage product cards

* fix PrivacySettings links

* Fix links

* Fix the build for www.

* Minor fixes for JWTGenerator.

* Fix the docs and ui tests.

* Revert codehike back to 0.8.3

* remove ConfirmAlert()

* reenable babel because mobx hates me

* fix blog image and comparison page avatar

* Fix svg errors

* update image synthax

* Fix code hike

* Move the button in a div so that it doesn't inherit its parent height and make the button look weird.

* When components are defined in a component, they get recreated on each render. This makes them unstable in certain cases and causes infinite rerenders.

* Replace the next/head usage with next/script.

* Chore/upgrade next 13 fix table editor (#18431)

* fix table editor styling and fix row deletion logic

* Fix deleting selected rows from header, and fix checkboxes not clearing up

* Fix deleting all rows when filter applied, and fix deleting all rows

* Fix grid size styling issue

* Fix TS error

* Hydration errors

* studio org pages fixes

* fix more studio links

* audit logs fixes

* dropdown icon styling fixes

* fix some images in www

* upgrade to next 14

* try new sentry wrapper for api

* see if this is even invoked

* Revert "see if this is even invoked"

This reverts commit 86c3973ffa.

* Revert "try new sentry wrapper for api"

This reverts commit f67623ebad.

* Revert "upgrade to next 14"

This reverts commit a24dd6131e.

* chore: allow node version 19/20

* Try to fix the LogTable so that it renders with the newer "react-data-grid" version.

* Fix type errors in the log renderer code.

* Fix the replication screen.

* Add the CSS for the GraphiQL.

* Fix SQL editor results rendering

* Lint

* Fix SQL editor results height issue

* Fix auth RLS not invalidating RQ when toggling RLS

* Fix database tables new/edit column regressed

* Fix migrations page empty state if migrations schema not yet created

* Fix API side panel docs temp remove postgrest text for column description PK and FK

* Fix + improve timeout handling in SQL editor

---------

Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Francesco Sansalvadore <f.sansalvadore@gmail.com>
Co-authored-by: Terry Sutton <saltcod@gmail.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>
2023-10-31 05:51:46 +00:00

135 lines
3.8 KiB
TypeScript

import Editor from '@monaco-editor/react'
import { useTheme } from 'next-themes'
import { useParams } from 'common'
import { observer } from 'mobx-react-lite'
import { useMemo, useRef } from 'react'
import { format } from 'sql-formatter'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'
import { useTableDefinitionQuery } from 'data/database/table-definition-query'
import { useViewDefinitionQuery } from 'data/database/view-definition-query'
import { ENTITY_TYPE } from 'data/entity-types/entity-type-constants'
import useEntityType from 'hooks/misc/useEntityType'
import { timeout } from 'lib/helpers'
import Link from 'next/link'
import { Button } from 'ui'
export interface TableDefinitionProps {
id?: number
}
const TableDefinition = ({ id }: TableDefinitionProps) => {
const { ref } = useParams()
const editorRef = useRef(null)
const monacoRef = useRef(null)
const { resolvedTheme } = useTheme()
const entityType = useEntityType(id)
const { project } = useProjectContext()
const viewResult = useViewDefinitionQuery(
{
schema: entityType?.schema,
name: entityType?.name,
projectRef: project?.ref,
connectionString: project?.connectionString,
},
{
enabled:
entityType?.type === ENTITY_TYPE.VIEW || entityType?.type === ENTITY_TYPE.MATERIALIZED_VIEW,
}
)
const tableResult = useTableDefinitionQuery(
{
schema: entityType?.schema,
name: entityType?.name,
projectRef: project?.ref,
connectionString: project?.connectionString,
},
{
enabled: entityType?.type === ENTITY_TYPE.TABLE,
}
)
const { data: definition, isLoading } =
entityType?.type === ENTITY_TYPE.VIEW || entityType?.type === ENTITY_TYPE.MATERIALIZED_VIEW
? viewResult
: tableResult
const prepend =
entityType?.type === ENTITY_TYPE.VIEW
? `create view ${entityType.schema}.${entityType.name} as\n`
: entityType?.type === ENTITY_TYPE.MATERIALIZED_VIEW
? `create materialized view ${entityType.schema}.${entityType.name} as\n`
: ''
const formattedDefinition = useMemo(
() =>
definition
? format(prepend + definition, {
language: 'postgresql',
keywordCase: 'lower',
})
: undefined,
[definition]
)
const handleEditorOnMount = async (editor: any, monaco: any) => {
editorRef.current = editor
monacoRef.current = monaco
// add margin above first line
editor.changeViewZones((accessor: any) => {
accessor.addZone({
afterLineNumber: 0,
heightInPx: 4,
domNode: document.createElement('div'),
})
})
// when editor did mount, it will need a delay before focus() works properly
await timeout(500)
editor?.focus()
}
if (isLoading) {
return (
<div className="p-4">
<GenericSkeletonLoader />
</div>
)
}
return (
<div className="flex-grow overflow-y-auto border-t border-scale-400 relative">
<Button asChild type="default" className="absolute top-2 right-5 z-10">
<Link
href={`/project/${ref}/sql/new?content=${encodeURIComponent(formattedDefinition ?? '')}`}
>
Open in SQL Editor
</Link>
</Button>
<Editor
className="monaco-editor"
theme={resolvedTheme === 'dark' ? 'vs-dark' : 'vs'}
onMount={handleEditorOnMount}
defaultLanguage="pgsql"
value={formattedDefinition}
path={''}
options={{
domReadOnly: true,
readOnly: true,
tabSize: 2,
fontSize: 13,
minimap: { enabled: false },
wordWrap: 'on',
fixedOverflowWidgets: true,
}}
/>
</div>
)
}
export default observer(TableDefinition)