Files
supabase/apps/studio/components/interfaces/TableGridEditor/TableDefinition.tsx
Joshen Lim 3db42a805f Joshen/fe 3652 replace direct renders of editor component from monaco to (#47268)
## Context

Part of consolidating all our code editors - removes all direct renders
of the `Editor` component and use `CodeEditor` instead

## UIs affected
- [ ] Query performance advisor -> query block
- [ ] Table Editor -> Table definition
- [ ] Table Editor -> Text + JSON editor (From RowEditorSidePanel,
expand input field)
- [ ] Auth -> RLS -> Create/edit policy code sections
- [ ] Storage policies -> Anywhere that has a code section

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Rolled out a consistent PostgreSQL code editor experience across
policy, storage policy, trigger function, table definitions, and query
performance screens.
* Updated policy/template previews to use the shared editor for cleaner
read-only viewing.

* **Bug Fixes**
  * Removed extra left padding in the query performance editor wrapper.
  * Improved the JSON editor action control with clearer icon behavior.

* **Refactor**
* Standardized editor usage by replacing legacy SQL/Monaco-based editors
with the shared CodeEditor and simplifying related editor components.
* Updated CodeEditor capabilities (read-only handling, wrapper styling,
markdown support) and tightened editor prop contracts.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-06-25 14:48:19 +08:00

86 lines
2.3 KiB
TypeScript

import { useParams } from 'common'
import Link from 'next/link'
import { useMemo } from 'react'
import { Button } from 'ui'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
import { Footer } from '@/components/grid/components/footer/Footer'
import { CodeEditor } from '@/components/ui/CodeEditor/CodeEditor'
import { useTableDefinitionQuery } from '@/data/database/table-definition-query'
import { useViewDefinitionQuery } from '@/data/database/view-definition-query'
import { Entity, isTableLike, isViewLike } from '@/data/table-editor/table-editor-types'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { formatSql } from '@/lib/formatSql'
export interface TableDefinitionProps {
entity?: Entity
}
export const TableDefinition = ({ entity }: TableDefinitionProps) => {
const { ref } = useParams()
const { data: project } = useSelectedProjectQuery()
const viewResult = useViewDefinitionQuery(
{
id: entity?.id,
includeCreateStatement: true,
projectRef: project?.ref,
connectionString: project?.connectionString,
},
{
enabled: isViewLike(entity),
}
)
const tableResult = useTableDefinitionQuery(
{
id: entity?.id,
projectRef: project?.ref,
connectionString: project?.connectionString,
},
{
enabled: isTableLike(entity),
}
)
const { data: definition, isLoading } = isViewLike(entity) ? viewResult : tableResult
const formattedDefinition = useMemo(
() => (definition ? formatSql(definition) : undefined),
[definition]
)
if (isLoading) {
return (
<div className="h-full grid">
<div className="p-4">
<GenericSkeletonLoader />
</div>
<div className="mt-auto">
<Footer />
</div>
</div>
)
}
return (
<>
<div className="grow overflow-y-auto border-t border-muted relative">
<Button asChild variant="default" className="absolute top-2 right-5 z-10">
<Link
href={`/project/${ref}/sql/new?content=${encodeURIComponent(
formattedDefinition ?? ''
)}`}
>
Open in SQL Editor
</Link>
</Button>
<CodeEditor isReadOnly language="pgsql" value={formattedDefinition} />
</div>
<Footer />
</>
)
}