Files
supabase/apps/studio/components/interfaces/Integrations/Integration/useInstallRequiredExtensions.ts
T
Joshen Limandcoderabbitai[bot] 2400e9537a Joshenlim/fe 4679 inconsistency with integrations that arent installed create (#52141)
## Context

Addresses some issues with the "One click integration" feature preview
for the following 3 integrations

### Database Webhooks
- The "Create a new hook" CTA can be clicked despite the integration not
being installed yet
- As such if you were to create a new webhook, you'll run into errors
- Am opting to follow the UX in wrappers where the dashboard will
install the integration for you if you're trying to create a new webhook
while the integration isn't installed yet
- So this allows users to go straight to creating webhooks without the
additional step of clicking "Install integration"
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/7fb70937-94c2-4095-9eef-4fb71347d132"
/>
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/7efcee65-34c0-438f-8fb5-3f0d855a0f34"
/>

### Cron and Queues
- The "Jobs" / "Queues" tab can be opened despite the integration not
being installed yet
- This results in a number of unnecessary API requests to fetch the data
for the corresponding grids, but they'd all error out since the database
extension isn't installed
- Opting to gate all the network calls via `enabled` if the integration
is not installed yet, and also render an empty state instead as such
(This aligns with the Stripe Sync Engine)
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/e8203162-a249-4da0-9ef9-f47df194b20f"
/>

## To test
- [ ] For database webhooks, verify that you can create a webhook
without having to click "Install integration" prior
- [ ] For cron and queues, verify that there's no network requests to
fetch their data if the integration is not installed yet while you're on
the jobs / queues pages

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2026-10-23 16:16:33 +08:00

64 lines
2.7 KiB
TypeScript

import { useMemo } from 'react'
import { useIsMarketplaceEnabled } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext'
import { getExtensionDefaultSchema } from '@/components/interfaces/Integrations/Integration/IntegrationOverviewTabV2/IntegrationOverviewTabV2.utils'
import { useIntegrationDetail } from '@/components/interfaces/Integrations/Landing/useIntegrationDetail'
import { getRequiredExtensionsToInstall } from '@/components/interfaces/Integrations/Wrappers/Wrappers.utils'
import { useDatabaseExtensionEnableMutation } from '@/data/database-extensions/database-extension-enable-mutation'
import { useDatabaseExtensionsQuery } from '@/data/database-extensions/database-extensions-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
/**
* Tracks which of the current integration's required extensions are not yet installed, and
* exposes a function to install them. Only active when the marketplace flag is on.
*/
export const useInstallRequiredExtensions = () => {
const isMarketplaceEnabled = useIsMarketplaceEnabled()
const { integration } = useIntegrationDetail()
const { data: project } = useSelectedProjectQuery()
const { data: extensions } = useDatabaseExtensionsQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
})
// null while the query is in flight — distinct from [] which means "all installed"
const requiredExtensionsToInstall = useMemo(
() => getRequiredExtensionsToInstall(extensions, integration?.requiredExtensions ?? []),
[extensions, integration?.requiredExtensions]
)
const needsExtensions = isMarketplaceEnabled && (requiredExtensionsToInstall?.length ?? 0) > 0
const isExtensionDataLoading = isMarketplaceEnabled && requiredExtensionsToInstall === null
const { mutateAsync: enableExtension } = useDatabaseExtensionEnableMutation({ onError: () => {} })
const installRequiredExtensions = async () => {
if (!project) return
const { ref: projectRef, connectionString } = project
const results = await Promise.allSettled(
(requiredExtensionsToInstall ?? []).map((ext) => {
const schema = getExtensionDefaultSchema(ext) ?? 'extensions'
return enableExtension({
projectRef,
connectionString,
schema,
name: ext.name,
version: ext.default_version,
cascade: true,
createSchema: false,
})
})
)
const failure = results.find((r) => r.status === 'rejected')
if (failure) throw new Error((failure as PromiseRejectedResult).reason.message)
}
return {
requiredExtensionsToInstall,
needsExtensions,
isExtensionDataLoading,
installRequiredExtensions,
}
}