Skip to content

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

Add ability to retrieve project API keys from cmdk

Joshen Lim7ba0e57962a5
diff --git a/packages/ui/src/components/Command/APIKeys.tsx b/packages/ui/src/components/Command/APIKeys.tsx
new file mode 100644
index 00000000000..a12ef0b6788
--- /dev/null
+++ b/packages/ui/src/components/Command/APIKeys.tsx
@@ -0,0 +1,64 @@
+import { CommandGroup } from 'cmdk-supabase'
+import { useCommandMenu } from './CommandMenuProvider'
+import SearchOnlyItem from './SearchOnlyItem'
+import { Badge } from '../Badge'
+import { CommandItem } from './Command.utils'
+import { IconAlertCircle } from './../Icon/icons/IconAlertCircle'
+
+const APIKeys = ({ isSubItem = false }) => {
+ const { setIsOpen, project } = useCommandMenu()
+ const { apiKeys } = project
+
+ const copyToClipboard = (str: string, callback = () => {}) => {
+ const focused = window.document.hasFocus()
+ if (focused) {
+ window.navigator?.clipboard?.writeText(str).then(callback)
+ } else {
+ console.warn('Unable to copy to clipboard')
+ }
+ }
+
+ return (
+ <CommandGroup>
+ {apiKeys?.anon !== undefined && (
+ <SearchOnlyItem
+ isSubItem={isSubItem}
+ onSelect={() => {
+ copyToClipboard(apiKeys?.anon ?? '')
+ setIsOpen(false)
+ }}
+ className="space-x-2"
+ >
+ <p>Copy anonymous key</p>
+ <Badge color="gray">Public</Badge>
+ </SearchOnlyItem>
+ )}
+ {apiKeys?.service !== undefined && (
+ <SearchOnlyItem
+ isSubItem={isSubItem}
+ onSelect={() => {
+ copyToClipboard(apiKeys?.service ?? '')
+ setIsOpen(false)
+ }}
+ className="space-x-2"
+ >
+ <p>Copy service key</p>
+ <Badge color="red">Secret</Badge>
+ </SearchOnlyItem>
+ )}
+ {apiKeys?.anon === undefined && apiKeys?.service === undefined && (
+ <CommandItem type="link" className="items-start">
+ <IconAlertCircle strokeWidth={1.5} className="text-scale-1100" />
+ <div>
+ <p>No API keys available</p>
+ <p className="text-scale-1000">
+ You may not have the necessary permissions to view the project's API keys
+ </p>
+ </div>
+ </CommandItem>
+ )}
+ </CommandGroup>
+ )
+}
+
+export default APIKeys
diff --git a/packages/ui/src/components/Command/Command.constants.ts b/packages/ui/src/components/Command/Command.constants.ts
index b73917ac59f..5ff9d5de760 100644
--- a/packages/ui/src/components/Command/Command.constants.ts
+++ b/packages/ui/src/components/Command/Command.constants.ts
@@ -5,6 +5,7 @@ export const COMMAND_ROUTES = {
THEME: 'Theme',
AI_ASK_ANYTHING: 'Ask anything',
AI_RLS_POLICY: 'Help me create a RLS policy',
+ API_KEYS: 'Project API keys',
}
type QueryCategory = {
diff --git a/packages/ui/src/components/Command/CommandMenu.tsx b/packages/ui/src/components/Command/CommandMenu.tsx
index 4c1a0e99d30..cf561b8155b 100644
--- a/packages/ui/src/components/Command/CommandMenu.tsx
+++ b/packages/ui/src/components/Command/CommandMenu.tsx
@@ -1,4 +1,3 @@
-import { useCommandState } from 'cmdk-supabase'
import { useRouter } from 'next/router'
import * as React from 'react'
import { ElementRef, useRef } from 'react'
@@ -12,6 +11,7 @@ import { IconLifeBuoy } from './../Icon/icons/IconLifeBuoy'
import { IconMonitor } from './../Icon/icons/IconMonitor'
import { IconPhone } from './../Icon/icons/IconPhone'
import { IconUser } from './../Icon/icons/IconUser'
+import { IconKey } from './../Icon/icons/IconKey'
import AiCommand from './AiCommand'
import sharedItems from './utils/shared-nav-items.json'
@@ -23,15 +23,16 @@ import {
CommandItem,
CommandLabel,
CommandList,
- CommandShortcut,
} from './Command.utils'
+import { COMMAND_ROUTES } from './Command.constants'
import { useCommandMenu } from './CommandMenuProvider'
+
import DocsSearch from './DocsSearch'
-import CommandMenuShortcuts from './CommandMenuShortcuts'
-import SearchOnlyItem from './SearchOnlyItem'
-import SearchableStudioItems from './SearchableStudioItems'
-import { COMMAND_ROUTES } from './Command.constants'
import GenerateSQL from './GenerateSQL'
+import ThemeOptions from './ThemeOptions'
+import APIKeys from './APIKeys'
+import SearchableStudioItems from './SearchableStudioItems'
+import CommandMenuShortcuts from './CommandMenuShortcuts'
export const CHAT_ROUTES = [
COMMAND_ROUTES.AI, // this one is temporary
@@ -62,32 +63,6 @@ const CommandMenu = ({ projectRef }: CommandMenuProps) => {
const commandInputRef = useRef<ElementRef<typeof CommandInput>>(null)
const { isOpen, setIsOpen, actions, search, setSearch, pages, setPages, currentPage, site } =
useCommandMenu()
-
- const ThemeOptions = ({ isSubItem = false }) => {
- return (
- <CommandGroup>
- <SearchOnlyItem
- isSubItem={isSubItem}
- onSelect={() => {
- actions.toggleTheme(true)
- setIsOpen(false)
- }}
- >
- Change Theme to dark
- </SearchOnlyItem>
- <SearchOnlyItem
- isSubItem={isSubItem}
- onSelect={() => {
- actions.toggleTheme(false)
- setIsOpen(false)
- }}
- >
- Change Theme to light
- </SearchOnlyItem>
- </CommandGroup>
- )
- }
-
const showCommandInput = !currentPage || !CHAT_ROUTES.includes(currentPage)
return (
@@ -194,7 +169,7 @@ const CommandMenu = ({ projectRef }: CommandMenuProps) => {
</CommandGroup>
)}
- {site === 'studio' && [
+ {site === 'studio' && (
<CommandGroup heading="Experimental">
<CommandItem
forceMount
@@ -204,7 +179,23 @@ const CommandMenu = ({ projectRef }: CommandMenuProps) => {
<AiIcon className="text-scale-1100" />
<CommandLabel>Generate SQL with Supabase AI</CommandLabel>
</CommandItem>
- </CommandGroup>,
+ </CommandGroup>
+ )}
+
+ {site === 'studio' && projectRef !== undefined && (
+ <CommandGroup heading="Project tools">
+ <CommandItem
+ forceMount
+ type="command"
+ onSelect={() => setPages([...pages, COMMAND_ROUTES.API_KEYS])}
+ >
+ <IconKey className="text-scale-1100" />
+ <CommandLabel>Get API keys</CommandLabel>
+ </CommandItem>
+ </CommandGroup>
+ )}
+
+ {site === 'studio' && (
<CommandGroup heading="Navigate">
{sharedItems.tools.map((item) => {
const itemUrl = (
@@ -220,8 +211,8 @@ const CommandMenu = ({ projectRef }: CommandMenuProps) => {
</CommandItem>
)
})}
- </CommandGroup>,
- ]}
+ </CommandGroup>
+ )}
{/* <DashboardTableEditor /> */}
@@ -246,12 +237,14 @@ const CommandMenu = ({ projectRef }: CommandMenuProps) => {
))}
</CommandGroup>
)}
+
<CommandGroup heading="Settings">
<CommandItem type="link" onSelect={() => setPages([...pages, 'Theme'])}>
<IconMonitor className="mr-2" />
Change theme
</CommandItem>
</CommandGroup>
+
<ThemeOptions isSubItem />
{site === 'studio' && search && <SearchableStudioItems />}
</>
@@ -260,6 +253,7 @@ const CommandMenu = ({ projectRef }: CommandMenuProps) => {
{currentPage === COMMAND_ROUTES.DOCS_SEARCH && <DocsSearch />}
{currentPage === COMMAND_ROUTES.GENERATE_SQL && <GenerateSQL />}
{currentPage === COMMAND_ROUTES.THEME && <ThemeOptions />}
+ {currentPage === COMMAND_ROUTES.API_KEYS && <APIKeys />}
</CommandList>
</CommandDialog>
</>
diff --git a/packages/ui/src/components/Command/CommandMenuProvider.tsx b/packages/ui/src/components/Command/CommandMenuProvider.tsx
index 45f8f8c0f84..77c121db76c 100644
--- a/packages/ui/src/components/Command/CommandMenuProvider.tsx
+++ b/packages/ui/src/components/Command/CommandMenuProvider.tsx
@@ -17,6 +17,9 @@ export interface CommandMenuContextValue {
currentPage?: string
site: 'studio' | 'docs'
+ // Project metadata for easy retrieval
+ project: { apiKeys: { anon?: string; service?: string } }
+
// to do: remove this prop
// this is a temporary hack as ReactMarkdown fails our jest tests if we import the package within this UI package
MarkdownHandler: (props: ReactMarkdownOptions) => JSX.Element // to do: remove this. although it breaks our jest tests
@@ -42,6 +45,7 @@ export interface CommandMenuActions {
export interface CommandMenuProviderProps {
site: 'studio' | 'docs'
projectRef?: string
+ apiKeys: { anon?: string; service?: string }
// to do: remove this prop
// this is a temporary hack as ReactMarkdown fails our jest tests if we import the package within this UI package
MarkdownHandler: (props: ReactMarkdownOptions) => JSX.Element
@@ -52,6 +56,7 @@ const CommandMenuProvider = ({
children,
site,
projectRef,
+ apiKeys,
MarkdownHandler,
onSaveGeneratedSQL,
}: PropsWithChildren<CommandMenuProviderProps>) => {
@@ -62,9 +67,7 @@ const CommandMenuProvider = ({
const { toggleTheme } = useTheme()
const currentPage = pages[pages.length - 1]
- const actions: CommandMenuActions = {
- toggleTheme,
- }
+ const actions: CommandMenuActions = { toggleTheme }
useKeyboardEvents({ setIsOpen, currentPage, setSearch, setPages })
@@ -82,6 +85,7 @@ const CommandMenuProvider = ({
setPages,
currentPage,
site,
+ project: { apiKeys },
MarkdownHandler,
onSaveGeneratedSQL,
}}
diff --git a/packages/ui/src/components/Command/SearchOnlyItem.tsx b/packages/ui/src/components/Command/SearchOnlyItem.tsx
index 33f43ff78cc..82cd3d16314 100644
--- a/packages/ui/src/components/Command/SearchOnlyItem.tsx
+++ b/packages/ui/src/components/Command/SearchOnlyItem.tsx
@@ -1,5 +1,4 @@
import { useCommandState } from 'cmdk-supabase'
-import React from 'react'
import { CommandItem } from './Command.utils'
export default function SearchOnlyItem({ children, isSubItem, ...props }: any) {
diff --git a/packages/ui/src/components/Command/ThemeOptions.tsx b/packages/ui/src/components/Command/ThemeOptions.tsx
new file mode 100644
index 00000000000..1e20213bd70
--- /dev/null
+++ b/packages/ui/src/components/Command/ThemeOptions.tsx
@@ -0,0 +1,31 @@
+import { CommandGroup } from 'cmdk-supabase'
+import { useCommandMenu } from './CommandMenuProvider'
+import SearchOnlyItem from './SearchOnlyItem'
+
+const ThemeOptions = ({ isSubItem = false }) => {
+ const { setIsOpen, actions } = useCommandMenu()
+ return (
+ <CommandGroup>
+ <SearchOnlyItem
+ isSubItem={isSubItem}
+ onSelect={() => {
+ actions.toggleTheme(true)
+ setIsOpen(false)
+ }}
+ >
+ Change Theme to dark
+ </SearchOnlyItem>
+ <SearchOnlyItem
+ isSubItem={isSubItem}
+ onSelect={() => {
+ actions.toggleTheme(false)
+ setIsOpen(false)
+ }}
+ >
+ Change Theme to light
+ </SearchOnlyItem>
+ </CommandGroup>
+ )
+}
+
+export default ThemeOptions
diff --git a/studio/components/interfaces/App/CommandMenuWrapper.tsx b/studio/components/interfaces/App/CommandMenuWrapper.tsx
index 1946259e16b..e4038a72b49 100644
--- a/studio/components/interfaces/App/CommandMenuWrapper.tsx
+++ b/studio/components/interfaces/App/CommandMenuWrapper.tsx
@@ -8,17 +8,24 @@ import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useProfileQuery } from 'data/profile/profile-query'
import { uuidv4 } from 'lib/helpers'
import { createSqlSnippetSkeleton } from 'components/to-be-cleaned/SqlEditor/SqlEditor.utils'
+import { useProjectApiQuery } from 'data/config/project-api-query'
const CommandMenuWrapper = ({ children }: PropsWithChildren<{}>) => {
const { ref } = useParams()
const { content } = useStore()
const { data: profile } = useProfileQuery()
+ const { data: settings } = useProjectApiQuery({ projectRef: ref })
// const canCreateSQLSnippet = checkPermissions(PermissionAction.CREATE, 'user_content', {
// resource: { type: 'sql', owner_id: profile?.id },
// subject: { id: profile?.id },
// })
+ const apiKeys = {
+ anon: settings?.autoApiService?.defaultApiKey ?? undefined,
+ service: settings?.autoApiService?.serviceApiKey ?? undefined,
+ }
+
const onSaveGeneratedSQL = async (answer: string, resolve: any) => {
// remove backticks from returned answer
answer = answer.replace(/`/g, '')
@@ -46,6 +53,7 @@ ${answer}
<CommandMenuProvider
site="studio"
projectRef={ref}
+ apiKeys={apiKeys}
MarkdownHandler={(props) => <ReactMarkdown remarkPlugins={[remarkGfm]} {...props} />}
onSaveGeneratedSQL={onSaveGeneratedSQL}
>