Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Midway implementing show view definition

Joshen Lim6b52b7437b22
diff --git a/studio/components/grid/SupabaseGrid.tsx b/studio/components/grid/SupabaseGrid.tsx
index cf57c55dace..c579f2f5a2d 100644
--- a/studio/components/grid/SupabaseGrid.tsx
+++ b/studio/components/grid/SupabaseGrid.tsx
@@ -58,7 +58,7 @@ export const SupabaseGrid = forwardRef<SupabaseGridRef, SupabaseGridProps>((prop
})
const SupabaseGridLayout = forwardRef<SupabaseGridRef, SupabaseGridProps>((props, ref) => {
- const { editable, storageRef, gridProps, headerActions } = props
+ const { editable, storageRef, gridProps, headerActions, showCustomChildren, children } = props
const dispatch = useDispatch()
const state = useTrackedState()
@@ -198,15 +198,22 @@ const SupabaseGridLayout = forwardRef<SupabaseGridRef, SupabaseGridProps>((props
onAddColumn={editable ? props.onAddColumn : undefined}
headerActions={headerActions}
/>
- <Grid
- ref={gridRef}
- {...gridProps}
- rows={data?.rows ?? []}
- updateRow={props.updateTableRow}
- onEditForeignKeyColumnValue={props.onEditForeignKeyColumnValue}
- />
- <Footer isLoading={isLoading || isRefetching} />
- <Shortcuts gridRef={gridRef} />
+ {showCustomChildren && children !== undefined ? (
+ <>{children}</>
+ ) : (
+ <>
+ <Grid
+ ref={gridRef}
+ {...gridProps}
+ rows={data?.rows ?? []}
+ updateRow={props.updateTableRow}
+ onEditForeignKeyColumnValue={props.onEditForeignKeyColumnValue}
+ />
+ <Footer isLoading={isLoading || isRefetching} />
+ <Shortcuts gridRef={gridRef} />
+ </>
+ )}
+
{mounted &&
createPortal(<RowContextMenu table={table} rows={data?.rows ?? []} />, document.body)}
</div>
diff --git a/studio/components/grid/types/grid.ts b/studio/components/grid/types/grid.ts
index 28981c8f556..918229e565e 100644
--- a/studio/components/grid/types/grid.ts
+++ b/studio/components/grid/types/grid.ts
@@ -1,5 +1,5 @@
import { ForeignRowSelectorProps } from 'components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector'
-import React from 'react'
+import React, { ReactNode } from 'react'
import { Dictionary } from './base'
import { SupaRow, SupaTable } from './table'
@@ -81,6 +81,16 @@ export interface SupabaseGridProps {
row: any
column: any
}) => void
+
+ /**
+ * Show custom component passed as children instead of the grid editor
+ */
+ showCustomChildren?: boolean
+
+ /**
+ * Custom component passed as children
+ */
+ children?: ReactNode
}
export interface SupabaseGridRef {
diff --git a/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx b/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx
index 5cd00a26d62..f595551f68c 100644
--- a/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx
@@ -36,6 +36,8 @@ import {
} from 'data/database/foreign-key-constraints-query'
import { FOREIGN_KEY_DELETION_ACTION } from 'data/database/database-query-constants'
import { ForeignRowSelectorProps } from './SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector'
+import TwoOptionToggle from 'components/ui/TwoOptionToggle'
+import ViewDefinition from './ViewDefinition'
export interface TableGridEditorProps {
/** Theme for the editor */
@@ -120,6 +122,9 @@ const TableGridEditor = ({
const [selectedLang, setSelectedLang] = useState<any>('js')
const [showApiKey, setShowApiKey] = useState<any>(DEFAULT_KEY)
+ // When rendering a view in the grid
+ const [selectedView, setSelectedView] = useState<'data' | 'definition'>('data')
+
const isReadOnly =
!checkPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'tables') &&
!checkPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'columns')
@@ -372,19 +377,30 @@ const TableGridEditor = ({
theme={theme}
gridProps={{ height: '100%' }}
storageRef={projectRef}
+ showCustomChildren={isViewSelected && selectedView === 'definition'}
editable={!isReadOnly && canUpdateTables && canEditViaTableEditor}
schema={selectedTable.schema}
table={gridTable}
refreshDocs={refreshDocs}
headerActions={
- canEditViaTableEditor && (
+ isViewSelected ? (
+ <div>
+ <TwoOptionToggle
+ width={80}
+ options={['definition', 'data']}
+ activeOption={selectedView}
+ borderOverride="border-gray-500"
+ onClickOption={setSelectedView}
+ />
+ </div>
+ ) : canEditViaTableEditor ? (
<GridHeaderActions
table={selectedTable as PostgresTable}
apiPreviewPanelOpen={apiPreviewPanelOpen}
setApiPreviewPanelOpen={setApiPreviewPanelOpen}
refreshDocs={refreshDocs}
/>
- )
+ ) : null
}
onAddColumn={onAddColumn}
onEditColumn={onSelectEditColumn}
@@ -396,7 +412,9 @@ const TableGridEditor = ({
onSqlQuery={onSqlQuery}
onExpandJSONEditor={onExpandJSONEditor}
onEditForeignKeyColumnValue={onEditForeignKeyColumnValue}
- />
+ >
+ <ViewDefinition />
+ </SupabaseGrid>
{!isUndefined(selectedSchema) && (
<SidePanelEditor
selectedSchema={selectedSchema}
diff --git a/studio/components/interfaces/TableGridEditor/ViewDefinition.tsx b/studio/components/interfaces/TableGridEditor/ViewDefinition.tsx
new file mode 100644
index 00000000000..c3bec258a71
--- /dev/null
+++ b/studio/components/interfaces/TableGridEditor/ViewDefinition.tsx
@@ -0,0 +1,53 @@
+import { useRef } from 'react'
+import Editor from '@monaco-editor/react'
+import { timeout } from 'lib/helpers'
+import { useStore } from 'hooks'
+
+const ViewDefinition = () => {
+ const editorRef = useRef(null)
+ const monacoRef = useRef(null)
+
+ const { ui } = useStore()
+ const { isDarkTheme } = ui
+
+ 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()
+ }
+
+ return (
+ <div className="flex-grow overflow-y-auto border-t border-scale-400">
+ <Editor
+ className="monaco-editor"
+ theme={isDarkTheme ? 'vs-dark' : 'vs'}
+ onMount={handleEditorOnMount}
+ defaultLanguage="pgsql"
+ defaultValue={'Hello'}
+ path={''}
+ options={{
+ domReadOnly: true,
+ tabSize: 2,
+ fontSize: 13,
+ minimap: { enabled: false },
+ wordWrap: 'on',
+ fixedOverflowWidgets: true,
+ }}
+ />
+ </div>
+ )
+}
+
+export default ViewDefinition
diff --git a/studio/data/database/view-definition-query.ts b/studio/data/database/view-definition-query.ts
new file mode 100644
index 00000000000..e69de29bb2d