useDestinationInformation.ts72 lines · main
| 1 | import { useParams } from 'common' |
| 2 | |
| 3 | import { DestinationType } from './DestinationPanel/DestinationPanel.types' |
| 4 | import { getStatusName } from './Pipeline.utils' |
| 5 | import { getReplicationDestinationType } from './ReplicationDiagram/Nodes.utils' |
| 6 | import { useReplicationDestinationByIdQuery } from '@/data/replication/destination-by-id-query' |
| 7 | import { useReplicationPipelineStatusQuery } from '@/data/replication/pipeline-status-query' |
| 8 | import { useReplicationPipelinesQuery } from '@/data/replication/pipelines-query' |
| 9 | import { useReplicationSourcesQuery } from '@/data/replication/sources-query' |
| 10 | |
| 11 | export const useDestinationInformation = ({ id }: { id?: number | null }) => { |
| 12 | const { ref: projectRef } = useParams() |
| 13 | |
| 14 | const { data: sourcesData, isSuccess: isSourcesSuccess } = useReplicationSourcesQuery({ |
| 15 | projectRef, |
| 16 | }) |
| 17 | const sourceId = sourcesData?.sources.find((s) => s.name === projectRef)?.id |
| 18 | const replicationNotEnabled = isSourcesSuccess && !sourceId |
| 19 | |
| 20 | const { |
| 21 | data: destination, |
| 22 | error: destinationError, |
| 23 | isPending: isDestinationPending, |
| 24 | isError: isDestinationError, |
| 25 | isSuccess: isDestinationSuccess, |
| 26 | } = useReplicationDestinationByIdQuery({ |
| 27 | projectRef, |
| 28 | destinationId: id, |
| 29 | }) |
| 30 | const destinationType: DestinationType | undefined = getReplicationDestinationType( |
| 31 | destination?.config as Record<string, unknown> | undefined |
| 32 | ) |
| 33 | |
| 34 | const { |
| 35 | data: pipelinesData, |
| 36 | error: pipelineError, |
| 37 | isPending: isPipelinePending, |
| 38 | isError: isPipelineError, |
| 39 | isSuccess: isPipelineSuccess, |
| 40 | } = useReplicationPipelinesQuery({ projectRef }) |
| 41 | const pipeline = pipelinesData?.pipelines.find((p) => p.destination_id === id) |
| 42 | |
| 43 | const { data: pipelineStatus } = useReplicationPipelineStatusQuery({ |
| 44 | projectRef, |
| 45 | pipelineId: pipeline?.id, |
| 46 | }) |
| 47 | const statusName = getStatusName(pipelineStatus?.status) |
| 48 | |
| 49 | return { |
| 50 | sourceId, |
| 51 | destination, |
| 52 | pipeline, |
| 53 | pipelineStatus, |
| 54 | // Derivatives |
| 55 | statusName, |
| 56 | type: destinationType, |
| 57 | replicationNotEnabled, |
| 58 | // Data fetching status (Secondary information) |
| 59 | pipelineFetcher: { |
| 60 | error: pipelineError, |
| 61 | isPending: isPipelinePending, |
| 62 | isError: isPipelineError, |
| 63 | isSuccess: isPipelineSuccess, |
| 64 | }, |
| 65 | destinationFetcher: { |
| 66 | error: destinationError, |
| 67 | isPending: isDestinationPending, |
| 68 | isError: isDestinationError, |
| 69 | isSuccess: isDestinationSuccess, |
| 70 | }, |
| 71 | } |
| 72 | } |