mirror of
https://github.com/supabase/supabase.git
synced 2026-09-08 19:08:44 +08:00
Previously, uncaught permission errors were bubbling up and preventing the marketplace UI from rendering at all. This is a problem because users might have access to the integrations tab, but not permissions to view all the connected resources on a particular integration. In that scenario, we want to degrade gracefully and show them only those resources they have access to.
71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
import { useParams } from 'common'
|
|
import { useMemo } from 'react'
|
|
import { toast } from 'sonner'
|
|
import { Button } from 'ui'
|
|
|
|
import { isOAuthInstalled, type ProjectOAuthIntegrationData } from '../../../Landing/Landing.utils'
|
|
import type { IntegrationDefinition } from '@/components/interfaces/Integrations/Landing/Integrations.constants'
|
|
import { useInstallOAuthIntegrationMutation } from '@/data/marketplace/install-oauth-integration-mutation'
|
|
|
|
interface InstallOAuthIntegrationButtonProps {
|
|
integration: IntegrationDefinition
|
|
data: ProjectOAuthIntegrationData
|
|
isLoading: boolean
|
|
}
|
|
|
|
export function InstallOAuthIntegrationButton({
|
|
integration,
|
|
data,
|
|
isLoading,
|
|
}: InstallOAuthIntegrationButtonProps) {
|
|
const { ref: projectRef } = useParams()
|
|
|
|
const { mutate: installOAuthIntegration, isPending: isInstalling } =
|
|
useInstallOAuthIntegrationMutation({
|
|
onSuccess: (data) => {
|
|
if ('redirectUrl' in data) {
|
|
if (!data.redirectUrl) {
|
|
toast.error('Failed to redirect because redirect URL is invalid')
|
|
return
|
|
}
|
|
window.open(data.redirectUrl, '_blank', 'noreferrer')
|
|
} else {
|
|
toast.error('Failed to start integration installation')
|
|
}
|
|
},
|
|
})
|
|
|
|
const isIntegrationInstalled = useMemo(() => {
|
|
if (!integration) return false
|
|
|
|
return isOAuthInstalled({ integration, projectData: data })
|
|
}, [data, integration])
|
|
|
|
const handleInstallClick = async () => {
|
|
if (!integration || !projectRef) return
|
|
if (!integration.id) return toast.error('Listing ID is required')
|
|
|
|
installOAuthIntegration({ projectRef, listingSlug: integration.id })
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{isIntegrationInstalled ? (
|
|
<Button disabled variant="outline" className="shrink-0">
|
|
Installed
|
|
</Button>
|
|
) : (
|
|
<Button
|
|
variant="primary"
|
|
className="shrink-0"
|
|
loading={isInstalling || isLoading}
|
|
disabled={isLoading}
|
|
onClick={handleInstallClick}
|
|
>
|
|
Install integration
|
|
</Button>
|
|
)}
|
|
</>
|
|
)
|
|
}
|