useS3VectorsWrapper.tsx42 lines · main
1import { INTEGRATIONS } from '@/components/interfaces/Integrations/Landing/Integrations.constants'
2import {
3 DatabaseExtension,
4 useDatabaseExtensionsQuery,
5} from '@/data/database-extensions/database-extensions-query'
6import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
7
8export const useS3VectorsWrapperExtension = (): {
9 extension: DatabaseExtension | undefined
10 state: 'not-found' | 'loading' | 'installed' | 'needs-upgrade' | 'not-installed'
11} => {
12 const { data: project } = useSelectedProjectQuery()
13 const { data: extensionsData, isPending: isExtensionsLoading } = useDatabaseExtensionsQuery({
14 projectRef: project?.ref,
15 connectionString: project?.connectionString,
16 })
17
18 const integration = INTEGRATIONS.find((i) => i.id === 's3_vectors_wrapper')
19
20 if (!integration || integration.type !== 'wrapper') {
21 // This should never happen
22 return { extension: undefined, state: 'not-found' as const }
23 }
24
25 const wrapperMeta = integration.meta
26
27 const wrappersExtension = extensionsData?.find((ext) => ext.name === 'wrappers')
28 const isWrappersExtensionInstalled = !!wrappersExtension?.installed_version
29 const hasRequiredVersion =
30 (wrappersExtension?.installed_version ?? '') >= (wrapperMeta?.minimumExtensionVersion ?? '')
31
32 const state: 'not-found' | 'loading' | 'installed' | 'needs-upgrade' | 'not-installed' =
33 isExtensionsLoading
34 ? 'loading'
35 : isWrappersExtensionInstalled
36 ? hasRequiredVersion
37 ? 'installed'
38 : 'needs-upgrade'
39 : 'not-installed'
40
41 return { extension: wrappersExtension, state }
42}