import { useMemo } from 'react'
import {
CardContent,
Table,
TableBody,
TableCell,
TableFooter,
TableHead,
TableHeader,
TableRow,
} from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
import {
getTableCopyTargets,
summarizeTableCopyEstimate,
type ReplicationTableIdentity,
} from '../TableSyncCopy.utils'
import { SANDWICHED_ADMONITION_CLASS } from './PipelineValidationAdmonition'
import { InlineLink } from '@/components/ui/InlineLink'
import type { ReplicationCostEstimateData } from '@/data/replication/cost-estimate-query'
import type { TableSyncCopyConfig } from '@/data/replication/types'
import { DOCS_URL } from '@/lib/constants'
import { formatBytes, formatCurrency } from '@/lib/helpers'
const MAX_VISIBLE_TABLES = 10
export const PipelineCostEstimate = ({
estimate,
isLoading = false,
publicationTables,
tableSyncCopy,
}: {
estimate?: ReplicationCostEstimateData
isLoading?: boolean
publicationTables: ReplicationTableIdentity[]
tableSyncCopy?: TableSyncCopyConfig
}) => {
const copyTargets = useMemo(
() => getTableCopyTargets(publicationTables, tableSyncCopy),
[publicationTables, tableSyncCopy]
)
const copyEstimate = useMemo(
() =>
estimate === undefined
? undefined
: summarizeTableCopyEstimate(estimate.table_copy.tables, copyTargets),
[copyTargets, estimate]
)
const tables = copyEstimate?.tables ?? []
const visibleTables = tables.slice(0, MAX_VISIBLE_TABLES)
const hiddenTableCount = tables.length - visibleTables.length
const copyTableCount = copyTargets.length
return (
<>
{isLoading && }
{!isLoading && estimate !== undefined && (
Initial sync
{copyTableCount === 0 ? (
No tables will run an initial sync
{formatCurrency(0)}
) : copyEstimate?.isComplete ? (
Table
Est. volume
Est. cost ({formatCurrency(estimate.table_copy.rate_per_gb)}/GB)
{visibleTables.map((table) => (
{table.schema}.{table.name}
{formatBytes(table.estimated_bytes)}
{formatCurrency(table.estimated_cost)}
))}
{hiddenTableCount > 0 && (
+{hiddenTableCount} more{' '}
{hiddenTableCount === 1 ? table : tables}
)}
Initial sync total
{formatBytes(copyEstimate.estimatedBytes)}
{formatCurrency(copyEstimate.estimatedCost)}
) : (
Estimate unavailable for one or more selected tables
)}
Final charges are based on data processed during initial sync.
{copyEstimate?.isComplete && copyEstimate.hasRowFilteredTables ? (
{' Row filters may reduce this amount.'}
) : (
''
)}
Ongoing
Pipeline runtime
${estimate.pipeline.hourly_cost}/hour
Replication data processed
{formatCurrency(estimate.streaming.rate_per_gb)}/GB
Replication usage depends on how often published data changes.{' '}
Learn more
)}
{!isLoading && estimate === undefined && (
)}
>
)
}