mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## Context Just some housekeeping to move off old layout components - this PR targets Auth and Settings pages, specifically - Auth -> Emails - Auth -> Providers - Settings -> API Keys - Settings -> JWT Keys - Settings -> Log Drains - Settings -> Read Replica/id The only visual change is just that navigation tabs take the full width of the viewport now Just need to make sure that these pages still render fine
278 lines
10 KiB
TypeScript
278 lines
10 KiB
TypeScript
import { createFileRoute } from '@tanstack/react-router'
|
|
import { IS_PLATFORM, useParams } from 'common'
|
|
import { ChevronDownIcon } from 'lucide-react'
|
|
import { cloneElement, useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import {
|
|
Alert,
|
|
Button,
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuTrigger,
|
|
} from 'ui'
|
|
import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
|
|
import { PageContainer } from 'ui-patterns/PageContainer'
|
|
import {
|
|
PageHeader,
|
|
PageHeaderAside,
|
|
PageHeaderDescription,
|
|
PageHeaderMeta,
|
|
PageHeaderSummary,
|
|
PageHeaderTitle,
|
|
} from 'ui-patterns/PageHeader'
|
|
import { PageSection } from 'ui-patterns/PageSection'
|
|
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
|
|
|
|
import { LogDrainDestinationSheetForm } from '@/components/interfaces/LogDrains/LogDrainDestinationSheetForm'
|
|
import { LogDrains } from '@/components/interfaces/LogDrains/LogDrains'
|
|
import { LogDrainType } from '@/components/interfaces/LogDrains/LogDrains.constants'
|
|
import { useEnabledLogDrainTypes } from '@/components/interfaces/LogDrains/useEnabledLogDrainTypes'
|
|
import { DocsButton } from '@/components/ui/DocsButton'
|
|
import { Shortcut } from '@/components/ui/Shortcut'
|
|
import {
|
|
LogDrainCreateVariables,
|
|
useCreateLogDrainMutation,
|
|
} from '@/data/log-drains/create-log-drain-mutation'
|
|
import { LogDrainData, useLogDrainsQuery } from '@/data/log-drains/log-drains-query'
|
|
import { useUpdateLogDrainMutation } from '@/data/log-drains/update-log-drain-mutation'
|
|
import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements'
|
|
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
|
|
import { DOCS_URL } from '@/lib/constants'
|
|
import { useTrack } from '@/lib/telemetry/track'
|
|
import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
|
|
|
|
export const Route = createFileRoute('/project/$ref/settings/log-drains')({
|
|
component: LogDrainsSettings,
|
|
staticData: { settingsLayoutTitle: 'Log Drains' },
|
|
})
|
|
|
|
function LogDrainsSettings() {
|
|
const { can: canManageLogDrains, isLoading: isLoadingPermissions } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.PROJECT.ANALYTICS_CONFIG_WRITE
|
|
)
|
|
|
|
const track = useTrack()
|
|
const [open, setOpen] = useState(false)
|
|
const { ref } = useParams() as { ref: string }
|
|
const [selectedLogDrain, setSelectedLogDrain] = useState<Partial<LogDrainData> | null>(null)
|
|
const [isCreateConfirmModalOpen, setIsCreateConfirmModalOpen] = useState(false)
|
|
const [pendingLogDrainValues, setPendingLogDrainValues] =
|
|
useState<LogDrainCreateVariables | null>(null)
|
|
const [mode, setMode] = useState<'create' | 'update'>('create')
|
|
|
|
const { hasAccess: hasAccessToLogDrains, isLoading: isLoadingEntitlement } =
|
|
useCheckEntitlements('log_drains')
|
|
|
|
const enabledDrainTypes = useEnabledLogDrainTypes()
|
|
|
|
const { data: logDrains } = useLogDrainsQuery(
|
|
{ ref },
|
|
{ enabled: !isLoadingEntitlement && hasAccessToLogDrains }
|
|
)
|
|
|
|
const { mutate: createLogDrain, isPending: createLoading } = useCreateLogDrainMutation({
|
|
onSuccess: () => {
|
|
toast.success('Log drain destination created')
|
|
setIsCreateConfirmModalOpen(false)
|
|
setOpen(false)
|
|
},
|
|
onError: () => {
|
|
toast.error('Failed to create log drain')
|
|
setIsCreateConfirmModalOpen(false)
|
|
setOpen(false)
|
|
},
|
|
})
|
|
|
|
const { mutate: updateLogDrain, isPending: updateLoading } = useUpdateLogDrainMutation({
|
|
onSuccess: () => {
|
|
toast.success('Log drain updated')
|
|
setOpen(false)
|
|
},
|
|
onError: () => {
|
|
setOpen(false)
|
|
toast.error('Failed to update log drain')
|
|
},
|
|
})
|
|
|
|
const isLoading = createLoading || updateLoading
|
|
|
|
function handleUpdateClick(drain: LogDrainData) {
|
|
setSelectedLogDrain(drain)
|
|
setMode('update')
|
|
setOpen(true)
|
|
}
|
|
|
|
function handleNewClick(src: LogDrainType) {
|
|
setSelectedLogDrain({ type: src })
|
|
setMode('create')
|
|
setOpen(true)
|
|
}
|
|
|
|
function handleAddDestinationClick() {
|
|
setSelectedLogDrain(null)
|
|
setMode('create')
|
|
setOpen(true)
|
|
}
|
|
|
|
const content = (
|
|
<PageSection id="log-drains" className="gap-6">
|
|
<PageContainer className="flex flex-col gap-10 pb-16">
|
|
<LogDrainDestinationSheetForm
|
|
mode={mode}
|
|
open={open}
|
|
onOpenChange={(v) => {
|
|
if (!v) {
|
|
setSelectedLogDrain(null)
|
|
}
|
|
setOpen(v)
|
|
}}
|
|
defaultValues={{
|
|
...selectedLogDrain,
|
|
type: selectedLogDrain?.type ? selectedLogDrain.type : 'webhook',
|
|
}}
|
|
isLoading={isLoading}
|
|
existingDrainNames={(logDrains ?? []).map((drain) => drain.name)}
|
|
onSaveClick={(type) => {
|
|
track('log_drain_save_button_clicked', { destination: type })
|
|
}}
|
|
onSubmit={({ name, description, type, ...values }) => {
|
|
const logDrainValues = {
|
|
name,
|
|
description: description || '',
|
|
type,
|
|
config: values as any, // TODO: fix generated API types from backend
|
|
id: selectedLogDrain?.id,
|
|
projectRef: ref,
|
|
token: selectedLogDrain?.token,
|
|
}
|
|
|
|
if (mode === 'create') {
|
|
setPendingLogDrainValues(logDrainValues)
|
|
setIsCreateConfirmModalOpen(true)
|
|
} else {
|
|
if (!logDrainValues.id || !selectedLogDrain?.token) {
|
|
toast.error('Unable to update log drain: missing ID or token')
|
|
return
|
|
}
|
|
updateLogDrain(logDrainValues)
|
|
}
|
|
}}
|
|
/>
|
|
|
|
{isLoadingPermissions ? (
|
|
<GenericSkeletonLoader />
|
|
) : !canManageLogDrains ? (
|
|
<Alert variant="default">You do not have permission to manage log drains</Alert>
|
|
) : (
|
|
<LogDrains onUpdateDrainClick={handleUpdateClick} onNewDrainClick={handleNewClick} />
|
|
)}
|
|
</PageContainer>
|
|
|
|
<ConfirmationModal
|
|
confirmLabel="Add destination"
|
|
variant="default"
|
|
title="Confirm Log Drain Creation"
|
|
visible={isCreateConfirmModalOpen}
|
|
onConfirm={() => {
|
|
if (pendingLogDrainValues) {
|
|
createLogDrain(pendingLogDrainValues)
|
|
setPendingLogDrainValues(null)
|
|
}
|
|
setIsCreateConfirmModalOpen(false)
|
|
}}
|
|
onCancel={() => {
|
|
setIsCreateConfirmModalOpen(false)
|
|
setPendingLogDrainValues(null)
|
|
}}
|
|
>
|
|
<div className="text-foreground-light text-sm space-y-2">
|
|
<p>
|
|
You are about to create a new log drain destination:{' '}
|
|
<span className="text-foreground">{pendingLogDrainValues?.name}</span>
|
|
</p>
|
|
{IS_PLATFORM && (
|
|
<p>
|
|
This will incur an additional <span className="text-foreground">$60 per month</span>{' '}
|
|
charge to your subscription.
|
|
</p>
|
|
)}
|
|
<p>Are you sure you want to proceed?</p>
|
|
</div>
|
|
</ConfirmationModal>
|
|
</PageSection>
|
|
)
|
|
|
|
// [kemal]: Ordinarily the page header would be bundled with the settings layout, however in this case we need access to some bits for the "Add destination" button to render as part of the page header.
|
|
if (!isLoadingEntitlement && hasAccessToLogDrains) {
|
|
return (
|
|
<>
|
|
<PageHeader>
|
|
<PageHeaderMeta>
|
|
<PageHeaderSummary>
|
|
<PageHeaderTitle>Log Drains</PageHeaderTitle>
|
|
<PageHeaderDescription>
|
|
Send your project logs to third party destinations
|
|
</PageHeaderDescription>
|
|
</PageHeaderSummary>
|
|
<PageHeaderAside>
|
|
<DocsButton href={`${DOCS_URL}/guides/platform/log-drains`} />
|
|
{!(logDrains?.length === 0) && (
|
|
<div className="flex items-center">
|
|
<Shortcut
|
|
id={SHORTCUT_IDS.LOG_DRAINS_ADD_DESTINATION}
|
|
onTrigger={handleAddDestinationClick}
|
|
options={{ enabled: hasAccessToLogDrains && canManageLogDrains }}
|
|
side="bottom"
|
|
tooltipOpen={open ? false : undefined}
|
|
>
|
|
<Button
|
|
disabled={!hasAccessToLogDrains || !canManageLogDrains}
|
|
onClick={handleAddDestinationClick}
|
|
variant="primary"
|
|
className="rounded-r-none px-3 hover:z-10 focus-visible:z-10 focus-visible:rounded-r-sm"
|
|
>
|
|
Add destination
|
|
</Button>
|
|
</Shortcut>
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button
|
|
variant="primary"
|
|
aria-label="Choose destination type"
|
|
className="shrink-0 rounded-l-none px-1 py-1.25 -ml-px focus-visible:z-10 focus-visible:rounded-l-sm"
|
|
icon={<ChevronDownIcon />}
|
|
/>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end" side="bottom">
|
|
{enabledDrainTypes.map((drainType) => (
|
|
<DropdownMenuItem
|
|
key={drainType.value}
|
|
onClick={() => handleNewClick(drainType.value)}
|
|
>
|
|
<div className="flex items-center gap-3">
|
|
{cloneElement(drainType.icon, { height: 16, width: 16 })}
|
|
<div className="space-y-1">
|
|
<p className="block text-foreground">{drainType.name}</p>
|
|
{IS_PLATFORM && (
|
|
<p className="text-xs text-foreground-lighter">Additional $60</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</DropdownMenuItem>
|
|
))}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</div>
|
|
)}
|
|
</PageHeaderAside>
|
|
</PageHeaderMeta>
|
|
</PageHeader>
|
|
{content}
|
|
</>
|
|
)
|
|
}
|
|
|
|
return content
|
|
}
|