import { useQueryClient } from '@tanstack/react-query' import { Plus, Search } from 'lucide-react' import { useEffect, useRef, useState } from 'react' import { useParams } from 'common' import Table from 'components/to-be-cleaned/Table' import { AlertError } from 'components/ui/AlertError' import { DocsButton } from 'components/ui/DocsButton' import { useReplicationDestinationsQuery } from 'data/replication/destinations-query' import { replicationKeys } from 'data/replication/keys' import { fetchReplicationPipelineVersion } from 'data/replication/pipeline-version-query' import { useReplicationPipelinesQuery } from 'data/replication/pipelines-query' import { useReplicationSourcesQuery } from 'data/replication/sources-query' import { DOCS_URL } from 'lib/constants' import { Button, cn, Input_Shadcn_ } from 'ui' import { GenericSkeletonLoader } from 'ui-patterns' import { DestinationPanel } from './DestinationPanel' import { DestinationRow } from './DestinationRow' import { EnableReplicationModal } from './EnableReplicationModal' import { PIPELINE_ERROR_MESSAGES } from './Pipeline.utils' export const Destinations = () => { const [showNewDestinationPanel, setShowNewDestinationPanel] = useState(false) const [filterString, setFilterString] = useState('') const { ref: projectRef } = useParams() const { data: sourcesData, error: sourcesError, isLoading: isSourcesLoading, isError: isSourcesError, isSuccess: isSourcesSuccess, } = useReplicationSourcesQuery({ projectRef, }) const sourceId = sourcesData?.sources.find((s) => s.name === projectRef)?.id const replicationNotEnabled = isSourcesSuccess && !sourceId const { data: destinationsData, error: destinationsError, isLoading: isDestinationsLoading, isError: isDestinationsError, isSuccess: isDestinationsSuccess, } = useReplicationDestinationsQuery({ projectRef, }) const { data: pipelinesData, error: pipelinesError, isLoading: isPipelinesLoading, isError: isPipelinesError, isSuccess: isPipelinesSuccess, } = useReplicationPipelinesQuery({ projectRef, }) const hasDestinations = isDestinationsSuccess && destinationsData.destinations.length > 0 const filteredDestinations = filterString.length === 0 ? destinationsData?.destinations ?? [] : (destinationsData?.destinations ?? []).filter((destination) => destination.name.toLowerCase().includes(filterString.toLowerCase()) ) // Prefetch pipeline version info for all destinations on first load only const queryClient = useQueryClient() const prefetchedRef = useRef(false) useEffect(() => { if ( projectRef && !prefetchedRef.current && pipelinesData?.pipelines && pipelinesData.pipelines.length > 0 && isPipelinesSuccess ) { prefetchedRef.current = true pipelinesData.pipelines.forEach((p) => { if (!p?.id) return queryClient.prefetchQuery({ queryKey: replicationKeys.pipelinesVersion(projectRef, p.id), queryFn: ({ signal }) => fetchReplicationPipelineVersion({ projectRef, pipelineId: p.id }, signal), staleTime: Infinity, }) }) } }, [projectRef, pipelinesData?.pipelines, isPipelinesSuccess, queryClient]) return ( <>
setFilterString(e.target.value)} />
{!!sourceId && ( )}
{(isSourcesLoading || isDestinationsLoading) && } {(isSourcesError || isDestinationsError) && ( )} {replicationNotEnabled ? (

Run analysis on your data via integrations with Replication

Enable replication on your project to send data to your first destination

{/* [Joshen] Placeholder for when we have documentation */}
) : hasDestinations ? ( Name, Type, Status, Publication, , ]} body={filteredDestinations.map((destination) => { const pipeline = pipelinesData?.pipelines.find( (p) => p.destination_id === destination.id ) return ( ) })} /> ) : ( !isSourcesLoading && !isDestinationsLoading && !isSourcesError && !isDestinationsError && (

Send data to your first destination

Use destinations to improve performance or run analysis on your data via integrations like BigQuery

) )} {!isSourcesLoading && !isDestinationsLoading && filteredDestinations.length === 0 && hasDestinations && (

No destinations match "{filterString}"

)} setShowNewDestinationPanel(false)} /> ) }