Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

1.4 KB
Raw Blame
import { useMemo } from 'react'

import { getVectorBucketFDWServerName } from './VectorBuckets.utils'
import {
  WRAPPER_HANDLERS,
  WRAPPERS,
} from '@/components/interfaces/Integrations/Wrappers/Wrappers.constants'
import { wrapperMetaComparator } from '@/components/interfaces/Integrations/Wrappers/Wrappers.utils'
import { useFDWsQuery } from '@/data/fdw/fdws-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'

export const useS3VectorsWrapperInstance = ({ bucketId }: { bucketId?: string }) => {
  const { data: project, isPending: isLoadingProject } = useSelectedProjectQuery()

  const { data, isPending: isLoadingFDWs } = useFDWsQuery(
    {
      projectRef: project?.ref,
      connectionString: project?.connectionString,
    },
    {
      enabled: !!bucketId,
    }
  )

  const s3VectorsWrapper = useMemo(() => {
    return data
      ?.filter((wrapper) =>
        wrapperMetaComparator(
          { handlerName: WRAPPER_HANDLERS.S3_VECTORS, server: { options: [] } },
          wrapper
        )
      )
      .find((w) => w.server_name === getVectorBucketFDWServerName(bucketId ?? ''))
  }, [data, bucketId])

  const s3VectorsWrapperMeta = WRAPPERS.find((w) => w.handlerName === WRAPPER_HANDLERS.S3_VECTORS)

  return {
    data: s3VectorsWrapper,
    meta: s3VectorsWrapperMeta!,
    isLoading: isLoadingProject || isLoadingFDWs,
  }
}