Skip to content

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

refactor: DisplayConfigSettings and DisplayApiSettings

phamhieu1d47fc5e769f
diff --git a/studio/components/interfaces/Home/NewProjectPanel/NewProjectPanel.tsx b/studio/components/interfaces/Home/NewProjectPanel/NewProjectPanel.tsx
index fba67f8d2b9..2bae2c671a6 100644
--- a/studio/components/interfaces/Home/NewProjectPanel/NewProjectPanel.tsx
+++ b/studio/components/interfaces/Home/NewProjectPanel/NewProjectPanel.tsx
@@ -1,11 +1,6 @@
+import { DisplayApiSettings, DisplayConfigSettings } from 'components/ui/ProjectSettings'
import { FC } from 'react'
-import { Typography } from '@supabase/ui'
-
import GetStartedPanel from './GetStartedPanel'
-import {
- DisplayApiSettings,
- DisplayConfigSettings,
-} from 'components/to-be-cleaned/DisplayProjectSettings'
interface Props {}
diff --git a/studio/components/layouts/ProjectLayout/BuildingState.tsx b/studio/components/layouts/ProjectLayout/BuildingState.tsx
index e23caf7c2af..19fe8106372 100644
--- a/studio/components/layouts/ProjectLayout/BuildingState.tsx
+++ b/studio/components/layouts/ProjectLayout/BuildingState.tsx
@@ -2,10 +2,6 @@ import Link from 'next/link'
import { FC, useEffect, useRef } from 'react'
import { observer } from 'mobx-react-lite'
import { Badge, IconArrowRight, IconLoader, Button } from '@supabase/ui'
-import {
- DisplayApiSettings,
- DisplayConfigSettings,
-} from 'components/to-be-cleaned/DisplayProjectSettings'
import ExampleProject from 'components/interfaces/Home/ExampleProject'
import ClientLibrary from 'components/interfaces/Home/ClientLibrary'
import { CLIENT_LIBRARIES, EXAMPLE_PROJECTS } from 'components/interfaces/Home/Home.constants'
@@ -14,6 +10,7 @@ import { API_URL, PROJECT_STATUS } from 'lib/constants'
import { useStore } from 'hooks'
import { get } from 'lib/common/fetch'
import { Project } from 'types'
+import { DisplayApiSettings, DisplayConfigSettings } from 'components/ui/ProjectSettings'
type ProjectBuildingState = { project: Project }
const ProjectBuildingState: FC<ProjectBuildingState> = ({ project }) => {
diff --git a/studio/components/to-be-cleaned/DisplayProjectSettings.tsx b/studio/components/to-be-cleaned/DisplayProjectSettings.tsx
deleted file mode 100644
index a120dc92352..00000000000
--- a/studio/components/to-be-cleaned/DisplayProjectSettings.tsx
+++ /dev/null
@@ -1,230 +0,0 @@
-import useSWR from 'swr'
-import { FC } from 'react'
-import { useRouter } from 'next/router'
-import { get } from 'lib/common/fetch'
-import { Badge, IconAlertCircle, Input, Loading, Typography } from '@supabase/ui'
-import { JwtSecretUpdateStatus, ProjectEvents } from '@supabase/shared-types/out/events'
-
-import { useJwtSecretUpdateStatus } from 'hooks'
-import { API_URL } from 'lib/constants'
-import Panel from './Panel'
-
-const { Text, Title } = Typography
-const API_SERVICE_ID = 1
-
-export const DisplayApiSettings = () => {
- const router = useRouter()
- const { ref } = router.query
-
- const { data, error }: any = useSWR(`${API_URL}/props/project/${ref}/settings`, get)
- const {
- isError: isJwtSecretUpdateStatusError,
- isLoading: isJwtSecretUpdateStatusLoading,
- jwtSecretUpdateStatus,
- }: any = useJwtSecretUpdateStatus(ref)
-
- const isNotUpdatingJwtSecret =
- jwtSecretUpdateStatus === undefined || jwtSecretUpdateStatus === JwtSecretUpdateStatus.Updated
-
- if (!data || isJwtSecretUpdateStatusLoading)
- return (
- <ApiContentWrapper>
- <Panel.Content className="py-8">
- {error || isJwtSecretUpdateStatusError ? (
- <div className="flex items-center space-x-2">
- <Typography.Text type="secondary">
- <IconAlertCircle strokeWidth={2} />
- </Typography.Text>
- <Typography.Text type="secondary">Failed to fetch API keys</Typography.Text>
- </div>
- ) : (
- <div className="py-4">
- {/* @ts-ignore */}
- <Loading active={true} />
- </div>
- )}
- </Panel.Content>
- </ApiContentWrapper>
- )
-
- // Get the API service
- const apiService = (data?.services ?? []).find((x: any) => x.app.id == API_SERVICE_ID)
- const apiKeys = apiService?.service_api_keys ?? []
-
- return (
- <ApiContentWrapper>
- {!data || isJwtSecretUpdateStatusLoading ? (
- // @ts-ignore
- <Loading active={true} />
- ) : (
- apiKeys.map((x: any, i: number) => (
- <Panel.Content
- key={x.api_key}
- className={
- i >= 1 &&
- 'border-t border-panel-border-interior-light dark:border-panel-border-interior-dark'
- }
- >
- <Input
- layout="horizontal"
- // @ts-ignore
- label={
- <>
- {x.tags?.split(',').map((x: any, i: number) => (
- <code key={`${x}${i}`} className="text-xs text-code">
- {x}
- </code>
- ))}
- {x.tags === 'service_role' && (
- <>
- <code className="text-xs bg-red-900 text-white">{'secret'}</code>
- </>
- )}
- {x.tags === 'anon' && <code className="text-xs text-code">{'public'}</code>}
- </>
- }
- readOnly
- copy={isNotUpdatingJwtSecret}
- className="input-mono"
- disabled
- reveal={x.tags !== 'anon' && isNotUpdatingJwtSecret}
- value={
- jwtSecretUpdateStatus === JwtSecretUpdateStatus.Failed
- ? 'JWT secret update failed, new API key may have issues'
- : jwtSecretUpdateStatus === JwtSecretUpdateStatus.Updating
- ? 'Updating JWT secret...'
- : x.api_key
- }
- onChange={() => {}}
- descriptionText={
- x.tags === 'service_role'
- ? 'This key has the ability to bypass Row Level Security. Never share it publicly.'
- : 'This key is safe to use in a browser if you have enabled Row Level Security for your tables and configured policies.'
- }
- />
- </Panel.Content>
- ))
- )}
- </ApiContentWrapper>
- )
-}
-
-export const DisplayConfigSettings = () => {
- const router = useRouter()
- const { ref } = router.query
-
- const { data, error }: any = useSWR(`${API_URL}/props/project/${ref}/settings`, get)
- const {
- isError: isJwtSecretUpdateStatusError,
- isLoading: isJwtSecretUpdateStatusLoading,
- jwtSecretUpdateStatus,
- }: any = useJwtSecretUpdateStatus(ref)
-
- const isNotUpdatingJwtSecret =
- jwtSecretUpdateStatus === undefined || jwtSecretUpdateStatus === JwtSecretUpdateStatus.Updated
-
- if (!data || isJwtSecretUpdateStatusLoading)
- return (
- <ConfigContentWrapper>
- <Panel.Content className="py-8">
- {error || isJwtSecretUpdateStatusError ? (
- <div className="flex items-center space-x-2">
- <Typography.Text type="secondary">
- <IconAlertCircle strokeWidth={2} />
- </Typography.Text>
- <Typography.Text type="secondary">
- Failed to fetch project configuration
- </Typography.Text>
- </div>
- ) : (
- <div className="py-4">
- {/* @ts-ignore */}
- <Loading active={true} />
- </div>
- )}
- </Panel.Content>
- </ConfigContentWrapper>
- )
-
- // Get the API service
- const jwtSecret = data?.project.jwt_secret ?? ''
- const apiService = (data?.services ?? []).find((x: any) => x.app.id == API_SERVICE_ID)
- const apiConfig = apiService.app_config
-
- return (
- <section>
- <Panel
- title={
- <Typography.Title level={5} className="mb-0">
- Project Configuration
- </Typography.Title>
- }
- >
- <Panel.Content>
- <Input
- label="URL"
- readOnly
- copy
- disabled
- className="input-mono"
- value={`https://${apiConfig.endpoint}`}
- descriptionText="A RESTful endpoint for querying and managing your database."
- layout="horizontal"
- />
- </Panel.Content>
- <Panel.Content className="border-t border-panel-border-interior-light dark:border-panel-border-interior-dark">
- <Input
- label="JWT Secret"
- readOnly
- copy={isNotUpdatingJwtSecret}
- reveal={isNotUpdatingJwtSecret}
- disabled
- value={
- jwtSecretUpdateStatus === JwtSecretUpdateStatus.Failed
- ? 'JWT secret update failed'
- : jwtSecretUpdateStatus === JwtSecretUpdateStatus.Updating
- ? 'Updating JWT secret...'
- : jwtSecret
- }
- className="input-mono"
- descriptionText="Used to decode your JWTs. You can also use this to mint your own JWTs."
- layout="horizontal"
- />
- </Panel.Content>
- </Panel>
- </section>
- )
-}
-
-const ApiContentWrapper: FC<any> = ({ children }) => {
- return (
- <Panel
- title={
- <div className="space-y-3">
- <h5 className="text-base">Project API keys</h5>
- <p className="text-sm text-scale-1000">
- Your API is secured behind an API gateway which requires an API Key for every request.
- <br />
- You can use the keys below to use Supabase client libraries.
- </p>
- </div>
- }
- >
- {children}
- </Panel>
- )
-}
-
-const ConfigContentWrapper: FC<any> = ({ children }) => {
- return (
- <Panel
- title={
- <div className="space-y-3">
- <h5 className="text-base">Project Configuration</h5>
- </div>
- }
- >
- {children}
- </Panel>
- )
-}
diff --git a/studio/components/to-be-cleaned/Panel.tsx b/studio/components/to-be-cleaned/Panel.tsx
index 59ffd62aa81..9ee51559ef7 100644
--- a/studio/components/to-be-cleaned/Panel.tsx
+++ b/studio/components/to-be-cleaned/Panel.tsx
@@ -22,7 +22,7 @@ function Panel(props: any) {
className={`
border border-panel-border-light dark:border-panel-border-dark
shadow-sm overflow-hidden
- rounded rounded-md ${props.noMargin ? '' : 'mb-8'} ${props.className}`}
+ rounded-md ${props.noMargin ? '' : 'mb-8'} ${props.className}`}
>
{props.title && (
<div className={headerClasses.join(' ')}>
diff --git a/studio/components/ui/ProjectSettings/DisplayApiSettings.tsx b/studio/components/ui/ProjectSettings/DisplayApiSettings.tsx
new file mode 100644
index 00000000000..1ff735751ed
--- /dev/null
+++ b/studio/components/ui/ProjectSettings/DisplayApiSettings.tsx
@@ -0,0 +1,108 @@
+import { FC } from 'react'
+import { useRouter } from 'next/router'
+import { Input } from '@supabase/ui'
+import { JwtSecretUpdateStatus } from '@supabase/shared-types/out/events'
+import { useJwtSecretUpdateStatus, useProjectSettings } from 'hooks'
+import { SettingsLoadingState } from './SettingsLoadingState'
+import { DEFAULT_API_SERVICE_ID } from './index.constants'
+import Panel from 'components/to-be-cleaned/Panel'
+
+const DisplayApiSettings = () => {
+ const router = useRouter()
+ const { ref } = router.query
+ const {
+ services,
+ isLoading: isProjectSettingsLoading,
+ isError: isProjectSettingsError,
+ } = useProjectSettings(ref as string | undefined)
+ const {
+ isError: isJwtSecretUpdateStatusError,
+ isLoading: isJwtSecretUpdateStatusLoading,
+ jwtSecretUpdateStatus,
+ }: any = useJwtSecretUpdateStatus(ref)
+
+ const isNotUpdatingJwtSecret =
+ jwtSecretUpdateStatus === undefined || jwtSecretUpdateStatus === JwtSecretUpdateStatus.Updated
+ // Get the API service
+ const apiService = (services ?? []).find((x: any) => x.app.id == DEFAULT_API_SERVICE_ID)
+ const apiKeys = apiService?.service_api_keys ?? []
+
+ return (
+ <ApiContentWrapper>
+ {!isProjectSettingsLoading || isJwtSecretUpdateStatusLoading ? (
+ <SettingsLoadingState
+ isError={isProjectSettingsError || isJwtSecretUpdateStatusError}
+ errorMessage="Failed to fetch API keys"
+ />
+ ) : (
+ apiKeys.map((x: any, i: number) => (
+ <Panel.Content
+ key={x.api_key}
+ className={
+ i >= 1 &&
+ 'border-t border-panel-border-interior-light dark:border-panel-border-interior-dark'
+ }
+ >
+ <Input
+ layout="horizontal"
+ // @ts-ignore
+ label={
+ <>
+ {x.tags?.split(',').map((x: any, i: number) => (
+ <code key={`${x}${i}`} className="text-xs text-code">
+ {x}
+ </code>
+ ))}
+ {x.tags === 'service_role' && (
+ <>
+ <code className="text-xs bg-red-900 text-white">{'secret'}</code>
+ </>
+ )}
+ {x.tags === 'anon' && <code className="text-xs text-code">{'public'}</code>}
+ </>
+ }
+ readOnly
+ copy={isNotUpdatingJwtSecret}
+ className="input-mono"
+ disabled
+ reveal={x.tags !== 'anon' && isNotUpdatingJwtSecret}
+ value={
+ jwtSecretUpdateStatus === JwtSecretUpdateStatus.Failed
+ ? 'JWT secret update failed, new API key may have issues'
+ : jwtSecretUpdateStatus === JwtSecretUpdateStatus.Updating
+ ? 'Updating JWT secret...'
+ : x.api_key
+ }
+ onChange={() => {}}
+ descriptionText={
+ x.tags === 'service_role'
+ ? 'This key has the ability to bypass Row Level Security. Never share it publicly.'
+ : 'This key is safe to use in a browser if you have enabled Row Level Security for your tables and configured policies.'
+ }
+ />
+ </Panel.Content>
+ ))
+ )}
+ </ApiContentWrapper>
+ )
+}
+export default DisplayApiSettings
+
+const ApiContentWrapper: FC = ({ children }) => {
+ return (
+ <Panel
+ title={
+ <div className="space-y-3">
+ <h5 className="text-base">Project API keys</h5>
+ <p className="text-sm text-scale-1000">
+ Your API is secured behind an API gateway which requires an API Key for every request.
+ <br />
+ You can use the keys below to use Supabase client libraries.
+ </p>
+ </div>
+ }
+ >
+ {children}
+ </Panel>
+ )
+}
diff --git a/studio/components/ui/ProjectSettings/DisplayConfigSettings.tsx b/studio/components/ui/ProjectSettings/DisplayConfigSettings.tsx
new file mode 100644
index 00000000000..8ef447a0b79
--- /dev/null
+++ b/studio/components/ui/ProjectSettings/DisplayConfigSettings.tsx
@@ -0,0 +1,92 @@
+import { FC } from 'react'
+import { useRouter } from 'next/router'
+import { Input } from '@supabase/ui'
+import { JwtSecretUpdateStatus } from '@supabase/shared-types/out/events'
+import { useJwtSecretUpdateStatus, useProjectSettings } from 'hooks'
+import { SettingsLoadingState } from './SettingsLoadingState'
+import { DEFAULT_API_SERVICE_ID } from './index.constants'
+import Panel from 'components/to-be-cleaned/Panel'
+
+const DisplayConfigSettings = () => {
+ const router = useRouter()
+ const { ref } = router.query
+ const {
+ project,
+ services,
+ isLoading: isProjectSettingsLoading,
+ isError: isProjectSettingsError,
+ } = useProjectSettings(ref as string | undefined)
+ const {
+ isError: isJwtSecretUpdateStatusError,
+ isLoading: isJwtSecretUpdateStatusLoading,
+ jwtSecretUpdateStatus,
+ }: any = useJwtSecretUpdateStatus(ref)
+
+ const isNotUpdatingJwtSecret =
+ jwtSecretUpdateStatus === undefined || jwtSecretUpdateStatus === JwtSecretUpdateStatus.Updated
+ // Get the API service
+ const jwtSecret = project?.jwt_secret ?? ''
+ const apiService = (services ?? []).find((x: any) => x.app.id == DEFAULT_API_SERVICE_ID)
+ const apiConfig = apiService?.app_config
+
+ return (
+ <ConfigContentWrapper>
+ {isProjectSettingsLoading || isJwtSecretUpdateStatusLoading ? (
+ <SettingsLoadingState
+ isError={isProjectSettingsError || isJwtSecretUpdateStatusError}
+ errorMessage="Failed to fetch project configuration"
+ />
+ ) : (
+ <>
+ <Panel.Content>
+ <Input
+ label="URL"
+ readOnly
+ copy
+ disabled
+ className="input-mono"
+ value={`https://${apiConfig.endpoint}`}
+ descriptionText="A RESTful endpoint for querying and managing your database."
+ layout="horizontal"
+ />
+ </Panel.Content>
+ <Panel.Content className="border-t border-panel-border-interior-light dark:border-panel-border-interior-dark">
+ <Input
+ label="JWT Secret"
+ readOnly
+ copy={isNotUpdatingJwtSecret}
+ reveal={isNotUpdatingJwtSecret}
+ disabled
+ value={
+ jwtSecretUpdateStatus === JwtSecretUpdateStatus.Failed
+ ? 'JWT secret update failed'
+ : jwtSecretUpdateStatus === JwtSecretUpdateStatus.Updating
+ ? 'Updating JWT secret...'
+ : jwtSecret
+ }
+ className="input-mono"
+ descriptionText="Used to decode your JWTs. You can also use this to mint your own JWTs."
+ layout="horizontal"
+ />
+ </Panel.Content>
+ </>
+ )}
+ </ConfigContentWrapper>
+ )
+}
+
+export default DisplayConfigSettings
+
+const ConfigContentWrapper: FC = ({ children }) => {
+ return (
+ <Panel
+ title={
+ <div className="space-y-3">
+ <h5 className="text-base">Project Configuration</h5>
+ </div>
+ }
+ >
+ {children}
+ </Panel>
+ )
+}
diff --git a/studio/components/ui/ProjectSettings/SettingsLoadingState.tsx b/studio/components/ui/ProjectSettings/SettingsLoadingState.tsx
new file mode 100644
index 00000000000..1c8dd232659
--- /dev/null
+++ b/studio/components/ui/ProjectSettings/SettingsLoadingState.tsx
@@ -0,0 +1,24 @@
+import { FC } from 'react'
+import { IconAlertCircle, Loading, Typography } from '@supabase/ui'
+import { SettingsLoadingStateProps } from './SettingsLoadingState.types'
+import Panel from 'components/to-be-cleaned/Panel'
+
+export const SettingsLoadingState: FC<SettingsLoadingStateProps> = ({ isError, errorMessage }) => {
+ return (
+ <Panel.Content className="py-8">
+ {isError ? (
+ <div className="flex items-center space-x-2">
+ <Typography.Text type="secondary">
+ <IconAlertCircle strokeWidth={2} />
+ </Typography.Text>
+ <Typography.Text type="secondary">{errorMessage}</Typography.Text>
+ </div>
+ ) : (
+ <div className="py-4">
+ {/* @ts-ignore */}
+ <Loading active={true} />
+ </div>
+ )}
+ </Panel.Content>
+ )
+}
diff --git a/studio/components/ui/ProjectSettings/SettingsLoadingState.types.tsx b/studio/components/ui/ProjectSettings/SettingsLoadingState.types.tsx
new file mode 100644
index 00000000000..b3c9248594d
--- /dev/null
+++ b/studio/components/ui/ProjectSettings/SettingsLoadingState.types.tsx
@@ -0,0 +1,4 @@
+export interface SettingsLoadingStateProps {
+ isError: boolean
+ errorMessage: string
+}
diff --git a/studio/components/ui/ProjectSettings/index.constants.tsx b/studio/components/ui/ProjectSettings/index.constants.tsx
new file mode 100644
index 00000000000..fdd902e2733
--- /dev/null
+++ b/studio/components/ui/ProjectSettings/index.constants.tsx
@@ -0,0 +1 @@
+export const DEFAULT_API_SERVICE_ID = 1
diff --git a/studio/components/ui/ProjectSettings/index.tsx b/studio/components/ui/ProjectSettings/index.tsx
new file mode 100644
index 00000000000..37e2dcc5b8b
--- /dev/null
+++ b/studio/components/ui/ProjectSettings/index.tsx
@@ -0,0 +1,4 @@
+import DisplayApiSettings from './DisplayApiSettings'
+import DisplayConfigSettings from './DisplayConfigSettings'
+
+export { DisplayApiSettings, DisplayConfigSettings }
diff --git a/studio/pages/project/[ref]/settings/api.tsx b/studio/pages/project/[ref]/settings/api.tsx
index dfe96adce81..4e83547e1ef 100644
--- a/studio/pages/project/[ref]/settings/api.tsx
+++ b/studio/pages/project/[ref]/settings/api.tsx
@@ -45,7 +45,7 @@ import ConfirmModal from 'components/ui/Dialogs/ConfirmDialog'
import Panel from 'components/to-be-cleaned/Panel'
import MultiSelectUI from 'components/to-be-cleaned/MultiSelect'
import SchemaFormPanel from 'components/to-be-cleaned/forms/SchemaFormPanel'
-import { DisplayApiSettings } from 'components/to-be-cleaned/DisplayProjectSettings'
+import { DisplayApiSettings } from 'components/ui/ProjectSettings'
const JWT_SECRET_UPDATE_ERROR_MESSAGES = {
[JwtSecretUpdateError.APIServicesConfigurationUpdateFailed]: