Files
supabase/apps/studio/components/interfaces/Functions/TerminalInstructions.tsx
T
Joshen Lim 90d3b56db0 Joshen/fe 2621 show custom domain on dashboard and connect modal (#43233)
## Context

Main fix is to adjust the new home page + connect dialog (and connect
sheet) to render the project's custom domain if available

<img width="471" height="255" alt="image"
src="https://github.com/user-attachments/assets/3a208b2e-bdeb-43f5-a2e7-3495881dbaaa"
/>
<img width="1065" height="233" alt="image"
src="https://github.com/user-attachments/assets/2a7b8f81-8c0b-4803-bf0a-fc16a2f1e0e1"
/>

## Changes involved

- Created a `useProjectApiUrl` hook that will return the API URL
depending if custom domains is available, otherwise default to default
project API URL
- Refactored all the other places that were manually deriving the
project's endpoint
  - Storage Explorer -> copy URL
  - Edge Functions
  - Integrations -> Data API + API Docs
  - Auth Providers -> Callback URL
- Also updated the copy CTA for the addons page
  - Instead of just "Change xxx", make it a bit more actionable
  - For add ons with binary states (Custom domains, IPv4)
    - If not enabled yet, "Enable xxx", otherwise "Toggle xxx"
  - For PITR
- If not enabled yet, "Enable PITR", otherwise "Change recovery
duration"
  - Also added "Edit custom domain" CTA if enabled
<img width="1144" height="518" alt="image"
src="https://github.com/user-attachments/assets/4f152ea5-0cc7-412c-95e8-ad5bb37c19c3"
/>


## To test
- [ ] Verify that for a project with custom domain set up, all the
affected UI mentioned in the above section look correct
2026-03-03 11:37:08 +08:00

159 lines
6.1 KiB
TypeScript

import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useParams } from 'common'
import CommandRender from 'components/interfaces/Functions/CommandRender'
import { DocsButton } from 'components/ui/DocsButton'
import { useAccessTokensQuery } from 'data/access-tokens/access-tokens-query'
import { getKeys, useAPIKeysQuery } from 'data/api-keys/api-keys-query'
import { useAsyncCheckPermissions } from 'hooks/misc/useCheckPermissions'
import { DOCS_URL } from 'lib/constants'
import { ExternalLink, Maximize2, Minimize2, Terminal } from 'lucide-react'
import { useRouter } from 'next/router'
import { ComponentPropsWithoutRef, ElementRef, forwardRef, useState } from 'react'
import {
Button,
Collapsible_Shadcn_,
CollapsibleContent_Shadcn_,
CollapsibleTrigger_Shadcn_,
} from 'ui'
import type { Commands } from './Functions.types'
import { useProjectApiUrl } from '@/data/config/project-endpoint-query'
interface TerminalInstructionsProps extends ComponentPropsWithoutRef<typeof Collapsible_Shadcn_> {
closable?: boolean
removeBorder?: boolean
}
export const TerminalInstructions = forwardRef<
ElementRef<typeof Collapsible_Shadcn_>,
TerminalInstructionsProps
>(({ closable = false, removeBorder = false, ...props }, ref) => {
const router = useRouter()
const { ref: projectRef } = useParams()
const [showInstructions, setShowInstructions] = useState(!closable)
const { data: tokens } = useAccessTokensQuery()
const { can: canReadAPIKeys } = useAsyncCheckPermissions(PermissionAction.SECRETS_READ, '*')
const { data: apiKeys } = useAPIKeysQuery({ projectRef }, { enabled: canReadAPIKeys })
const { data: endpoint } = useProjectApiUrl({ projectRef })
const functionsEndpoint = `${endpoint}/functions/v1`
const { anonKey, publishableKey } = getKeys(apiKeys)
const apiKey = publishableKey?.api_key ?? anonKey?.api_key ?? '[YOUR ANON KEY]'
// get the .co or .net TLD from the restUrl
const restUrl = `https://${endpoint}`
const restUrlTld = !!endpoint ? new URL(restUrl).hostname.split('.').pop() : 'co'
const commands: Commands[] = [
{
command: 'supabase functions new hello-world',
description: ' creates a function stub at ./functions/hello-world/index.ts',
jsx: () => {
return (
<>
<span className="text-brand-600">supabase</span> functions new hello-world
</>
)
},
comment: 'Create a function',
},
{
command: `supabase functions deploy hello-world --project-ref ${projectRef}`,
description: 'Deploys function at ./functions/hello-world/index.ts',
jsx: () => {
return (
<>
<span className="text-brand-600">supabase</span> functions deploy hello-world
--project-ref {projectRef}
</>
)
},
comment: 'Deploy your function',
},
{
command: `curl -L -X POST 'https://${projectRef}.supabase.${restUrlTld}/functions/v1/hello-world' -H 'Authorization: Bearer ${apiKey}'${anonKey?.type === 'publishable' ? ` -H 'apikey: ${apiKey}'` : ''} --data '{"name":"Functions"}'`,
description: 'Invokes the hello-world function',
jsx: () => {
return (
<>
<span className="text-brand-600">curl</span> -L -X POST '{functionsEndpoint}
/hello-world' -H 'Authorization: Bearer [YOUR ANON KEY]'
{anonKey?.type === 'publishable' ? " -H 'apikey: [YOUR ANON KEY]' " : ''}
{`--data '{"name":"Functions"}'`}
</>
)
},
comment: 'Invoke your function',
},
]
return (
<Collapsible_Shadcn_
ref={ref}
open={showInstructions}
className="w-full"
onOpenChange={() => setShowInstructions(!showInstructions)}
{...props}
>
<CollapsibleTrigger_Shadcn_ className="flex w-full justify-between" disabled={!closable}>
<div className="flex items-center gap-x-3">
<div className="flex items-center justify-center w-8 h-8 p-2 border rounded bg-alternative">
<Terminal strokeWidth={2} />
</div>
<h4>Create your first Edge Function via the CLI</h4>
</div>
{closable && (
<div className="cursor-pointer" onClick={() => setShowInstructions(!showInstructions)}>
{showInstructions ? (
<Minimize2 size={16} strokeWidth={1.5} />
) : (
<Maximize2 size={16} strokeWidth={1.5} />
)}
</div>
)}
</CollapsibleTrigger_Shadcn_>
<CollapsibleContent_Shadcn_ className="w-full transition-all data-[state=closed]:animate-collapsible-up data-[state=open]:animate-collapsible-down">
<CommandRender commands={commands} className="my-4" />
{tokens && tokens.length === 0 ? (
<div className="py-4 space-y-3 border-t">
<div>
<p className="text-sm text-foreground">You may need to create an access token</p>
<p className="text-sm text-foreground-light">
You can create a secure access token in your account section
</p>
</div>
<Button type="default" onClick={() => router.push('/account/tokens')}>
Access tokens
</Button>
</div>
) : (
<div className="py-4 space-y-3 border-t">
<div>
<h3 className="text-base text-foreground">Need help?</h3>
<p className="text-sm text-foreground-light">
Read the documentation, or browse some sample code.
</p>
</div>
<div className="flex gap-2">
<DocsButton href={`${DOCS_URL}/guides/functions`} />
<Button asChild type="default" icon={<ExternalLink />}>
<a
target="_blank"
rel="noreferrer"
href="https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions"
>
Examples
</a>
</Button>
</div>
</div>
)}
</CollapsibleContent_Shadcn_>
</Collapsible_Shadcn_>
)
})
TerminalInstructions.displayName = 'TerminalInstructions'