Files
supabase/apps/studio/components/interfaces/HomeNew/ProjectConnectionHoverCard.tsx
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

154 lines
5.6 KiB
TypeScript

import { PermissionAction } from '@supabase/shared-types/out/constants'
import { Label } from '@ui/components/shadcn/ui/label'
import { getConnectionStrings } from 'components/interfaces/Connect/DatabaseSettings.utils'
import { getKeys, useAPIKeysQuery } from 'data/api-keys/api-keys-query'
import { useReadReplicasQuery } from 'data/read-replicas/replicas-query'
import { useAsyncCheckPermissions } from 'hooks/misc/useCheckPermissions'
import { pluckObjectFields } from 'lib/helpers'
import { Plug } from 'lucide-react'
import { parseAsBoolean, useQueryState } from 'nuqs'
import { useMemo, useState, type ReactNode } from 'react'
import { Button, HoverCard, HoverCardContent, HoverCardTrigger } from 'ui'
import { ShimmeringLoader } from 'ui-patterns'
import { Input } from 'ui-patterns/DataInputs/Input'
import { useProjectApiUrl } from '@/data/config/project-endpoint-query'
const DB_FIELDS = ['db_host', 'db_name', 'db_port', 'db_user'] as const
const EMPTY_CONNECTION_INFO = {
db_user: '',
db_host: '',
db_port: '',
db_name: '',
}
const DetailRow = ({ label, children }: { label: string; children: ReactNode }) => {
return (
<div className="flex flex-col gap-2">
<Label>{label}</Label>
{children}
</div>
)
}
interface ProjectConnectionHoverCardProps {
projectRef?: string
}
export const ProjectConnectionHoverCard = ({ projectRef }: ProjectConnectionHoverCardProps) => {
const [open, setOpen] = useState(false)
const [, setShowConnect] = useQueryState('showConnect', parseAsBoolean.withDefault(false))
const { isLoading: isLoadingPermissions, can: canReadAPIKeys } = useAsyncCheckPermissions(
PermissionAction.READ,
'service_api_keys'
)
const { data: projectUrl, isPending: isLoadingApiUrl } = useProjectApiUrl({ projectRef })
const { data: apiKeys, isLoading: isLoadingKeys } = useAPIKeysQuery(
{ projectRef },
{ enabled: open && canReadAPIKeys }
)
const { publishableKey } = canReadAPIKeys ? getKeys(apiKeys) : { publishableKey: null }
const { data: databases, isLoading: isLoadingDatabases } = useReadReplicasQuery(
{ projectRef },
{ enabled: open && !!projectRef }
)
const primaryDatabase = databases?.find((db) => db.identifier === projectRef)
const directConnectionString = useMemo(() => {
if (
!primaryDatabase?.db_host ||
!primaryDatabase?.db_name ||
!primaryDatabase?.db_user ||
!primaryDatabase?.db_port
) {
return ''
}
const connectionInfo = pluckObjectFields(primaryDatabase, [...DB_FIELDS])
return getConnectionStrings({
connectionInfo: { ...EMPTY_CONNECTION_INFO, ...connectionInfo },
metadata: { projectRef },
}).direct.uri
}, [primaryDatabase, projectRef])
return (
<HoverCard openDelay={250} closeDelay={100} open={open} onOpenChange={setOpen}>
<HoverCardTrigger asChild>
<button onClick={() => setShowConnect(true)} className="flex items-center gap-4 mt-3 group">
<div className=" transition w-8 h-8 rounded-md bg-surface-75 group-hover:bg-muted border flex items-center justify-center">
<Plug strokeWidth={1.5} size={16} className="text-foreground-light rotate-90" />
</div>
{isLoadingApiUrl ? (
<ShimmeringLoader className="w-32" />
) : (
<span className="transition text-foreground-light group-hover:text-foreground underline decoration-dotted decoration-foreground-muted underline-offset-4 max-w-[320px] text-left">
{projectUrl ?? 'Project URL unavailable'}
</span>
)}
</button>
</HoverCardTrigger>
<HoverCardContent side="bottom" align="start" className="w-[420px] p-0">
<div className="p-4 border-b space-y-4">
<h3 className="heading-meta text-foreground-light">Data API</h3>
<DetailRow label="Project URL">
<Input
copy
readOnly
className="font-mono text-xs"
value={projectUrl ?? ''}
placeholder="Project URL unavailable"
/>
</DetailRow>
<DetailRow label="Publishable Key">
{isLoadingPermissions || isLoadingKeys ? (
<div className="text-xs text-foreground-lighter">Loading publishable key...</div>
) : canReadAPIKeys ? (
<Input
copy
readOnly
className="font-mono text-xs"
value={publishableKey?.api_key ?? ''}
placeholder="Publishable key unavailable"
/>
) : (
<div className="text-xs text-foreground-lighter">
You don't have permission to view API keys.
</div>
)}
</DetailRow>
</div>
<div className="p-4 space-y-4 border-b">
<h3 className="heading-meta text-foreground-light"> Database </h3>
<DetailRow label="Direct connection string">
{isLoadingDatabases ? (
<div className="text-xs text-foreground-lighter">Loading connection string...</div>
) : (
<Input
copy
readOnly
className="font-mono text-xs"
value={directConnectionString}
placeholder="Connection string unavailable"
/>
)}
</DetailRow>
</div>
<div className="p-4">
<Button
icon={<Plug className="rotate-90" />}
type="default"
size="medium"
className="w-full"
onClick={() => setShowConnect(true)}
>
Get connected
</Button>
</div>
</HoverCardContent>
</HoverCard>
)
}