Files
supabase/apps/studio/components/interfaces/Settings/Database/SSLConfiguration.tsx
T
Alaister YoungandAlaister Young ca2b50a0a7 chore(ui-patterns): collapse the admonition shim into ui-patterns/Admonition (#48377)
Follow-up to #48344: collapses the two resolution paths for the
Admonition module into one.

`src/admonition.tsx` was a back-compat shim re-exporting
`src/Admonition/`. Two ways to resolve one module is exactly what
produced the macOS self-import bug fixed in #48344, and the local
typecheck errors that #48374 worked around. This removes the shim and
standardizes on the PascalCase subpath, matching every other export in
the package.

**Changed:**

- Codemodded all 246 `ui-patterns/admonition` imports to
`ui-patterns/Admonition` (240 `.tsx`, 5 `.mdx`, 1 `.ts` across studio,
docs, www, design-system, and lite-studio)
- Pointed the 5 internal `'../admonition'` imports back at the
`'../Admonition'` directory

**Removed:**

- `packages/ui-patterns/src/admonition.tsx`, and its `./admonition`
entry in the exports map (regenerated with `pnpm gen:exports`)

## To test

- `grep -r "ui-patterns/admonition" --include='*.ts*'` → no hits
- `pnpm test:case-hazards` → passes
- `pnpm typecheck` → all 15 tasks green
- `pnpm --filter studio run lint:ratchet` → passes
- `pnpm --filter ui-patterns vitest run src/Admonition` → 11 tests pass

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Standardized Admonition component imports across the application and
documentation.
* Improved compatibility with case-sensitive environments by using the
canonical component path.
  * Removed the legacy Admonition import entry point.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-07-29 00:48:56 +08:00

237 lines
8.9 KiB
TypeScript

import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useParams } from 'common'
import { template } from 'lodash'
import { Download, Loader2 } from 'lucide-react'
import { useEffect, useMemo, useState } from 'react'
import { toast } from 'sonner'
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
Button,
Card,
CardContent,
Switch,
Tooltip,
TooltipContent,
TooltipTrigger,
} from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import { FormLayout } from 'ui-patterns/form/Layout/FormLayout'
import {
PageSection,
PageSectionContent,
PageSectionMeta,
PageSectionSummary,
PageSectionTitle,
} from 'ui-patterns/PageSection'
import { SupportLink } from '@/components/interfaces/Support/SupportLink'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { DocsButton } from '@/components/ui/DocsButton'
import { InlineLinkClassName } from '@/components/ui/InlineLink'
import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'
import { useSSLEnforcementQuery } from '@/data/ssl-enforcement/ssl-enforcement-query'
import { useSSLEnforcementUpdateMutation } from '@/data/ssl-enforcement/ssl-enforcement-update-mutation'
import { useCustomContent } from '@/hooks/custom-content/useCustomContent'
import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { DOCS_URL } from '@/lib/constants'
export const SSLConfiguration = () => {
const { ref } = useParams()
const { data: project } = useSelectedProjectQuery()
const [isEnforced, setIsEnforced] = useState(false)
const { data: settings } = useProjectSettingsV2Query({ projectRef: ref })
const {
data: sslEnforcementConfiguration,
isPending: isLoading,
isSuccess,
} = useSSLEnforcementQuery({
projectRef: ref,
})
const { mutate: updateSSLEnforcement, isPending: isSubmitting } = useSSLEnforcementUpdateMutation(
{
onSuccess: () => {
toast.success('Successfully updated SSL configuration')
},
onError: (error) => {
setIsEnforced(initialIsEnforced)
toast.error(`Failed to update SSL enforcement: ${error.message}`)
},
}
)
const { can: canUpdateSSLEnforcement } = useAsyncCheckPermissions(
PermissionAction.UPDATE,
'projects',
{
resource: {
project_id: project?.id,
},
}
)
const initialIsEnforced = isSuccess
? sslEnforcementConfiguration.appliedSuccessfully &&
sslEnforcementConfiguration.currentConfig.database
: false
const hasAccessToSSLEnforcement = !(
sslEnforcementConfiguration !== undefined &&
'isNotAllowed' in sslEnforcementConfiguration &&
sslEnforcementConfiguration.isNotAllowed
)
const env = process.env.NEXT_PUBLIC_ENVIRONMENT === 'prod' ? 'prod' : 'staging'
const hasSSLCertificate =
settings?.inserted_at !== undefined && new Date(settings.inserted_at) >= new Date('2021-04-30')
const { sslCertificateUrl: sslCertificateUrlTemplate } = useCustomContent(['ssl:certificate_url'])
const sslCertificateUrl = useMemo(
() => template(sslCertificateUrlTemplate ?? '')({ env }),
[sslCertificateUrlTemplate, env]
)
useEffect(() => {
if (!isLoading && sslEnforcementConfiguration) {
setIsEnforced(initialIsEnforced)
}
}, [isLoading])
const toggleSSLEnforcement = async () => {
if (!ref) return console.error('Project ref is required')
setIsEnforced(!isEnforced)
updateSSLEnforcement({ projectRef: ref, requestedConfig: { database: !isEnforced } })
}
return (
<PageSection id="ssl-configuration">
<PageSectionMeta>
<PageSectionSummary>
<PageSectionTitle>SSL configuration</PageSectionTitle>
</PageSectionSummary>
<DocsButton href={`${DOCS_URL}/guides/platform/ssl-enforcement`} />
</PageSectionMeta>
<PageSectionContent>
<Card>
<CardContent className="space-y-4">
<FormLayout
layout="flex-row-reverse"
label="Enforce SSL on incoming connections"
description="Reject non-SSL connections to your database"
>
<AlertDialog>
<AlertDialogTrigger asChild>
<div className="flex items-center justify-end mt-2.5 space-x-2">
{(isLoading || isSubmitting) && (
<Loader2 className="animate-spin" strokeWidth={1.5} size={16} />
)}
{isSuccess && (
<Tooltip>
<TooltipTrigger asChild>
{/* [Joshen] Added div as tooltip is messing with data state property of toggle */}
<div>
<Switch
size="large"
checked={isEnforced}
disabled={
isLoading ||
isSubmitting ||
!canUpdateSSLEnforcement ||
!hasAccessToSSLEnforcement
}
/>
</div>
</TooltipTrigger>
{(!canUpdateSSLEnforcement || !hasAccessToSSLEnforcement) && (
<TooltipContent side="bottom" className="w-64 text-center">
{!canUpdateSSLEnforcement
? 'You need additional permissions to update SSL enforcement for your project'
: !hasAccessToSSLEnforcement
? 'Your project does not have access to SSL enforcement'
: undefined}
</TooltipContent>
)}
</Tooltip>
)}
</div>
</AlertDialogTrigger>
<AlertDialogContent size="medium">
<AlertDialogHeader>
<AlertDialogTitle>
Updating SSL enforcement involves a brief downtime
</AlertDialogTitle>
<AlertDialogDescription>
A database restart is required for SSL enforcement changes to take place, and
this involves a few minutes of downtime. Confirm to proceed now?
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={isSubmitting}>Cancel</AlertDialogCancel>
<AlertDialogAction
variant="warning"
disabled={isSubmitting}
onClick={toggleSSLEnforcement}
>
{!isEnforced ? 'Enable SSL' : 'Disable SSL'}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</FormLayout>
{isSuccess && !sslEnforcementConfiguration?.appliedSuccessfully && (
<Admonition
type="warning"
layout="horizontal"
title="SSL enforcement was not updated successfully"
description={
<>
Please try updating again, or contact{' '}
<SupportLink className={InlineLinkClassName}>support</SupportLink> if this error
persists
</>
}
/>
)}
</CardContent>
<CardContent>
<FormLayout
layout="flex-row-reverse"
label="SSL Certificate"
description="Use this certificate when connecting to your database to prevent snooping and man-in-the-middle attacks."
>
<div className="flex items-end justify-end">
{!hasSSLCertificate ? (
<ButtonTooltip
disabled
variant="default"
icon={<Download />}
tooltip={{
content: {
side: 'bottom',
text: 'Projects before 15:08 (GMT+08), 29th April 2021 do not have SSL certificates installed',
},
}}
>
Download certificate
</ButtonTooltip>
) : (
<Button variant="default" icon={<Download />}>
<a href={sslCertificateUrl}>Download certificate</a>
</Button>
)}
</div>
</FormLayout>
</CardContent>
</Card>
</PageSectionContent>
</PageSection>
)
}