Skip to content

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

Merge branch 'master' into feat/add-autodocs-to-table-editor

Alaister Young8ae3cef01fd7
diff --cc studio/components/grid/types/grid.ts
index 9650ea0b890,a036249a97d..bae5c96556b
--- a/studio/components/grid/types/grid.ts
+++ b/studio/components/grid/types/grid.ts
@@@ -65,19 -63,8 +63,17 @@@ export interface SupabaseGridProps
* show edit row button if available
*/
onEditRow?: (row: SupaRow) => void
- /**
- * error handler
- */
onError?: (error: any) => void
+ /**
+ * Toggle api preview panel open
+ */
+ apiPreviewPanelOpen?: boolean
+ setApiPreviewPanelOpen?: () => void
+ /**
+ * Refresh the docs after a change is made to the table
+ */
+ refreshDocs: () => void
+ onExpandJSONEditor: (column: string, row: SupaRow) => void
}
export interface SupabaseGridRef {
diff --cc studio/components/interfaces/TableGridEditor/TableGridEditor.tsx
index e0cbb60d6f9,442723952cc..71c6f5b35bc
--- a/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx
@@@ -11,14 -11,7 +11,14 @@@ import GridHeaderActions from './GridHe
import NotFoundState from './NotFoundState'
import SidePanelEditor from './SidePanelEditor'
import { Dictionary, parseSupaTable, SupabaseGrid, SupabaseGridRef } from 'components/grid'
+import { IconBookOpen, SidePanel } from 'ui'
+import ActionBar from './SidePanelEditor/ActionBar'
+import { GeneralContent, ResourceContent } from '../Docs'
- import { useProjectSettingsQuery } from 'data/config/project-settings-query'
++import { useProjectApiQuery } from 'data/config/project-api-query'
++import { useProjectJsonSchemaQuery } from 'data/docs/project-json-schema-query'
+import { snakeToCamel } from 'lib/helpers'
- import useSWR, { mutate } from 'swr'
- import { get } from 'lib/common/fetch'
+import LangSelector from 'components/to-be-cleaned/Docs/LangSelector'
+ import { JsonEditValue } from './SidePanelEditor/RowEditor/RowEditor.types'
interface Props {
/** Theme for the editor */
@@@ -64,77 -61,17 +68,82 @@@ const TableGridEditor: FC<Props> = (
}) => {
const { meta, ui, vault } = useStore()
const router = useRouter()
+ const { query } = router
+ const { page } = query
+
const gridRef = useRef<SupabaseGridRef>(null)
const projectRef = ui.selectedProject?.ref
+ const tables = meta.tables.list()
+
- const { data: settings } = useProjectSettingsQuery({ projectRef: projectRef })
++ const { data: settings } = useProjectApiQuery({ projectRef: projectRef })
+
+ const autoApiService = {
+ ...settings?.autoApiService,
+ endpoint: `${settings?.autoApiService.protocol ?? 'https'}://${
+ settings?.autoApiService.endpoint ?? '-'
+ }`,
+ }
+ const DEFAULT_KEY = { name: 'hide', key: 'SUPABASE_KEY' }
+ const isVaultEnabled = useFlag('vaultExtension')
const [encryptedColumns, setEncryptedColumns] = useState([])
+ const [apiPreviewPanelOpen, setApiPreviewPanelOpen] = useState(false)
+
+ const [selectedLang, setSelectedLang] = useState<any>('js')
+ const [showApiKey, setShowApiKey] = useState<any>(DEFAULT_KEY)
- const isVaultEnabled = useFlag('vaultExtension')
-
const getEncryptedColumns = async (table: any) => {
const columns = await vault.listEncryptedColumns(table.schema, table.name)
setEncryptedColumns(columns)
}
+ function getResourcesFromJsonSchema(value: any) {
+ const { paths } = value || {}
+ const functionPath = 'rpc/'
+ let resources: any = {}
+
+ Object.entries(paths || []).forEach(([name, val]) => {
+ let trimmed = name.slice(1)
+ let id = trimmed.replace(functionPath, '')
+ let displayName = id.replace(/_/g, ' ')
+ let camelCase = snakeToCamel(id)
+ let enriched = { id, displayName, camelCase }
+ if (!trimmed.length) return
+ else resources[id] = enriched
+ })
+
+ return resources
+ }
+
- const API_KEY = settings?.autoApiService.service_api_keys.find((key: any) => key.tags === 'anon')
- ? settings.autoApiService.defaultApiKey
++ const apiService = settings?.autoApiService
++ const anonKey = apiService?.service_api_keys.find((x) => x.name === 'anon key')
++ ? apiService.defaultApiKey
+ : undefined
++ const swaggerUrl = settings?.autoApiService?.restUrl
+
- const swaggerUrl = settings?.autoApiService.restUrl
-
- const headers: any = { apikey: API_KEY }
-
- if (API_KEY) headers['Authorization'] = `Bearer ${API_KEY}`
-
- const { data: jsonSchema, error: jsonSchemaError } = useSWR(
- () => swaggerUrl,
- (url: string) => get(url, { headers, credentials: 'omit' }).then((res) => res)
++ const canReadServiceKey = checkPermissions(
++ PermissionAction.READ,
++ 'service_api_keys.service_role_key'
+ )
+
++ const {
++ data: jsonSchema,
++ error: jsonSchemaError,
++ refetch,
++ } = useProjectJsonSchemaQuery({
++ projectRef,
++ swaggerUrl,
++ apiKey: anonKey,
++ })
++
+ if (jsonSchemaError) console.log('jsonSchemaError', jsonSchemaError)
+
+ const resources = getResourcesFromJsonSchema(jsonSchema)
+
+ const refreshDocs = async () => {
- mutate(swaggerUrl)
++ await refetch()
+ }
+
useEffect(() => {
if (selectedTable !== undefined && selectedTable.id !== undefined && isVaultEnabled) {
getEncryptedColumns(selectedTable)
@@@ -271,80 -202,6 +282,80 @@@
closePanel={onClosePanel}
/>
)}
+
+ <SidePanel
+ key="WrapperTableEditor"
+ size="xxlarge"
+ visible={apiPreviewPanelOpen}
+ onCancel={() => setApiPreviewPanelOpen(false)}
+ header={
+ <span className="flex items-center gap-2">
+ <IconBookOpen size="tiny" />
+ API
+ </span>
+ }
+ customFooter={
+ <ActionBar
+ backButtonLabel="Close"
+ hideApply={true}
+ formId="wrapper-table-editor-form"
+ closePanel={() => setApiPreviewPanelOpen(false)}
+ />
+ }
+ >
+ <div className="Docs Docs--table-editor">
+ <SidePanel.Content>
+ {jsonSchemaError ? (
- <div className="mx-auto p-6 text-center sm:w-full md:w-3/4">
++ <div className="p-6 mx-auto text-center sm:w-full md:w-3/4">
+ <div className="text-scale-1000">
+ <p>Error connecting to API</p>
+ <p>{`${jsonSchemaError}`}</p>
+ </div>
+ </div>
+ ) : (
+ <>
+ {jsonSchema ? (
+ <>
- <div className="sticky top-0 bg-scale-100 dark:bg-scale-300 z-10">
++ <div className="sticky top-0 z-10 bg-scale-100 dark:bg-scale-300">
+ <LangSelector
+ selectedLang={selectedLang}
+ setSelectedLang={setSelectedLang}
+ showApiKey={showApiKey}
+ setShowApiKey={setShowApiKey}
- apiKey={API_KEY}
++ apiKey={anonKey}
+ autoApiService={autoApiService}
+ />
+ </div>
+ <GeneralContent
+ autoApiService={autoApiService}
+ selectedLang={selectedLang}
+ showApiKey={true}
+ page={page}
+ />
+
+ {jsonSchema?.definitions && (
+ <ResourceContent
+ autoApiService={autoApiService}
+ selectedLang={selectedLang}
+ resourceId={tables.find((table) => table.id === Number(query.id))?.name}
+ resources={resources}
+ definitions={jsonSchema.definitions}
+ paths={jsonSchema.paths}
+ showApiKey={true}
+ refreshDocs={refreshDocs}
+ />
+ )}
+ </>
+ ) : (
- <div className="mx-auto p-6 text-center sm:w-full md:w-3/4">
++ <div className="p-6 mx-auto text-center sm:w-full md:w-3/4">
+ <h3 className="text-lg">Building docs ...</h3>
+ </div>
+ )}
+ </>
+ )}
+ </SidePanel.Content>
+ </div>
+ </SidePanel>
</>
)
}
diff --cc studio/components/to-be-cleaned/Docs/Pages/Introduction.tsx
index 5b58b9d7348,30e24301034..b3563417179
--- a/studio/components/to-be-cleaned/Docs/Pages/Introduction.tsx
+++ b/studio/components/to-be-cleaned/Docs/Pages/Introduction.tsx
@@@ -1,33 -1,7 +1,33 @@@
import Snippets from '../Snippets'
import CodeSnippet from '../CodeSnippet'
+import Image from 'next/image'
+import { useStore } from 'hooks'
- import { AutoApiService } from 'data/config/project-settings-query'
++import { AutoApiService } from 'data/config/project-api-query'
+
+const libs = [
+ {
+ name: 'Javascript',
+ url: 'https://supabase.com/docs/reference/javascript/introduction',
+ icon: 'javascript',
+ },
+ { name: 'Flutter', url: 'https://supabase.com/docs/reference/dart/introduction', icon: 'dart' },
+ {
+ name: 'Python',
+ url: 'https://supabase.com/docs/reference/python/introduction',
+ icon: 'python',
+ },
+ { name: 'C#', url: 'https://supabase.com/docs/reference/csharp/introduction', icon: 'csharp' },
+]
+
+interface Props {
+ autoApiService: AutoApiService
+ selectedLang: string
+}
+
+export default function Introduction({ autoApiService, selectedLang }: Props) {
+ const { ui } = useStore()
+ const { isDarkTheme } = ui
-export default function Introduction({ autoApiService, selectedLang }) {
return (
<>
<h2 className="doc-heading">Introduction</h2>
diff --cc studio/pages/project/[ref]/api/index.tsx
index 670a7b93323,82bed5db772..ff1ef6824f0
--- a/studio/pages/project/[ref]/api/index.tsx
+++ b/studio/pages/project/[ref]/api/index.tsx
@@@ -1,5 -1,5 +1,4 @@@
- import useSWR, { mutate } from 'swr'
import { useRouter } from 'next/router'
-import { Button, Dropdown, IconKey } from 'ui'
import { FC, createContext, useContext, useEffect, useState } from 'react'
import { observer, useLocalObservable } from 'mobx-react-lite'
@@@ -10,8 -10,7 +9,8 @@@ import { useProjectApiQuery } from 'dat
import { DocsLayout } from 'components/layouts'
import { GeneralContent, ResourceContent, RpcContent } from 'components/interfaces/Docs'
import { PermissionAction } from '@supabase/shared-types/out/constants'
- import { useProjectSettingsQuery } from 'data/config/project-settings-query'
+ import { useProjectJsonSchemaQuery } from 'data/docs/project-json-schema-query'
+import LangSelector from 'components/to-be-cleaned/Docs/LangSelector'
const PageContext = createContext(null)
@@@ -101,7 -104,7 +104,7 @@@ const DocView: FC<any> = observer(({})
if (error || jsonSchemaError)
return (
-- <div className="mx-auto p-6 text-center sm:w-full md:w-3/4">
++ <div className="p-6 mx-auto text-center sm:w-full md:w-3/4">
<p className="text-scale-1000">
<p>Error connecting to API</p>
<p>{`${error || jsonSchemaError}`}</p>
@@@ -110,7 -113,7 +113,7 @@@
)
if (!data || !jsonSchema || !PageState.jsonSchema)
return (
-- <div className="mx-auto p-6 text-center sm:w-full md:w-3/4">
++ <div className="p-6 mx-auto text-center sm:w-full md:w-3/4">
<h3 className="text-xl">Building docs ...</h3>
</div>
)
@@@ -128,18 -129,10 +129,18 @@@
const PAGE_KEY: any = resource || rpc || page || 'index'
return (
- <div className="Docs Docs--api-page h-full w-full overflow-y-auto" key={PAGE_KEY}>
- <div className="Docs h-full w-full overflow-y-auto" key={PAGE_KEY}>
++ <div className="w-full h-full overflow-y-auto Docs Docs--api-page" key={PAGE_KEY}>
<div className="Docs--inner-wrapper">
-- <div className="sticky top-0 z-40 flex w-full flex-row-reverse ">
- <div className="bg-scale-100 dark:bg-scale-300" style={{ width: '50%' }}>
++ <div className="sticky top-0 z-40 flex flex-row-reverse w-full ">
+ <LangSelector
+ selectedLang={selectedLang}
+ setSelectedLang={setSelectedLang}
+ showApiKey={showApiKey}
+ setShowApiKey={setShowApiKey}
- apiKey={API_KEY}
++ apiKey={anonKey}
+ autoApiService={autoApiService}
+ />
+ {/* <div className="bg-scale-100 dark:bg-scale-300" style={{ width: '50%' }}>
<div className="z-0 flex ">
<button
type="button"
@@@ -172,7 -165,7 +173,7 @@@
<Dropdown
align="end"
side="bottom"
-- className="cursor-pointer border-none bg-transparent p-0 pl-2 pr-8 text-sm text-scale-900"
++ className="p-0 pl-2 pr-8 text-sm bg-transparent border-none cursor-pointer text-scale-900"
overlay={
<>
<Dropdown.Item key="hide" onClick={() => setShowApiKey(DEFAULT_KEY)}>