useS3VectorsWrapper.tsx 1.6 KB

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