Files
supabase/apps/studio/components/interfaces/Integrations/IntegrationConnection.tsx
Joshen Lim 4637140e72 Fix downgrade network error causing UI crash (#26919)
* Fix downgrade network error causing UI crash

* Ensure that all try blocks have at least a catch block and not an empty finally block

* Update apps/studio/components/interfaces/Organization/BillingSettings/Subscription/PlanUpdateSidePanel.tsx

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>

* Update

---------

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>
2024-06-03 11:08:39 +08:00

166 lines
5.8 KiB
TypeScript

import Link from 'next/link'
import { useRouter } from 'next/router'
import { forwardRef, useCallback, useState } from 'react'
import toast from 'react-hot-toast'
import {
Button,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
IconChevronDown,
IconLoader,
IconRefreshCw,
IconTrash,
} from 'ui'
import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
import {
IntegrationConnection,
IntegrationConnectionProps,
} from 'components/interfaces/Integrations/IntegrationPanels'
import { useIntegrationsVercelConnectionSyncEnvsMutation } from 'data/integrations/integrations-vercel-connection-sync-envs-mutation'
import type { IntegrationProjectConnection } from 'data/integrations/integrations.types'
import { useProjectsQuery } from 'data/projects/projects-query'
interface IntegrationConnectionItemProps extends IntegrationConnectionProps {
disabled?: boolean
onDeleteConnection: (connection: IntegrationProjectConnection) => void | Promise<void>
}
const IntegrationConnectionItem = forwardRef<HTMLLIElement, IntegrationConnectionItemProps>(
({ disabled, onDeleteConnection, ...props }, ref) => {
const router = useRouter()
const { type, connection } = props
const { data: projects } = useProjectsQuery()
const project = projects?.find((project) => project.ref === connection.supabase_project_ref)
const isBranchingEnabled = project?.is_branch_enabled === true
const [isOpen, setIsOpen] = useState(false)
const [dropdownVisible, setDropdownVisible] = useState(false)
const onConfirm = useCallback(async () => {
try {
await onDeleteConnection(connection)
} catch (error) {
// [Joshen] No need for error handler
} finally {
setIsOpen(false)
}
}, [connection, onDeleteConnection])
const onCancel = useCallback(() => {
setIsOpen(false)
}, [])
const { mutate: syncEnvs, isLoading: isSyncEnvLoading } =
useIntegrationsVercelConnectionSyncEnvsMutation({
onSuccess: () => {
toast.success('Successfully synced environment variables')
setDropdownVisible(false)
},
})
const onReSyncEnvVars = useCallback(async () => {
syncEnvs({ connectionId: connection.id })
}, [connection, syncEnvs])
const projectIntegrationUrl = `/project/[ref]/settings/integrations`
return (
<>
<IntegrationConnection
showNode={false}
actions={
disabled ? (
<Button asChild disabled iconRight={<IconChevronDown />} type="default">
<span>Manage</span>
</Button>
) : (
<DropdownMenu
open={dropdownVisible}
onOpenChange={() => setDropdownVisible(!dropdownVisible)}
modal={false}
>
<DropdownMenuTrigger asChild>
<Button iconRight={<IconChevronDown />} type="default">
<span>Manage</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent side="bottom" align="end">
{router.pathname !== projectIntegrationUrl && (
<DropdownMenuItem asChild>
<Link
href={projectIntegrationUrl.replace(
'[ref]',
connection.supabase_project_ref
)}
>
Configure connection
</Link>
</DropdownMenuItem>
)}
{type === 'Vercel' && (
<DropdownMenuItem
className="space-x-2"
onSelect={(event) => {
event.preventDefault()
onReSyncEnvVars()
}}
disabled={isSyncEnvLoading}
>
{isSyncEnvLoading ? (
<IconLoader className="animate-spin" size={14} />
) : (
<IconRefreshCw size={14} />
)}
<p>Resync environment variables</p>
</DropdownMenuItem>
)}
{(type === 'Vercel' || router.pathname !== projectIntegrationUrl) && (
<DropdownMenuSeparator />
)}
<DropdownMenuItem className="space-x-2" onSelect={() => setIsOpen(true)}>
<IconTrash size={14} />
<p>Delete connection</p>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
}
{...props}
/>
<ConfirmationModal
variant="destructive"
size={type === 'GitHub' && isBranchingEnabled ? 'medium' : 'small'}
visible={isOpen}
title={`Confirm to delete ${type} connection`}
confirmLabel="Delete connection"
onCancel={onCancel}
onConfirm={onConfirm}
alert={
type === 'GitHub' && isBranchingEnabled
? {
title: 'Branching will be disabled for this project',
description: ` Deleting this GitHub connection will remove all preview branches on this project,
and also disable branching for ${project.name}`,
}
: undefined
}
>
<p className="text-sm text-foreground-light">
This action cannot be undone. Are you sure you want to delete this {type} connection?
</p>
</ConfirmationModal>
</>
)
}
)
IntegrationConnectionItem.displayName = 'IntegrationConnectionItem'
export { IntegrationConnectionItem }