import { useMemo } from 'react' import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader' import { summarizeTableCopyEstimate, type ReplicationTableIdentity, type TableCopyEstimate, } from './TableSyncCopy.utils' import { useReplicationCostEstimateQuery } from '@/data/replication/cost-estimate-query' import { formatBytes, formatCurrency } from '@/lib/helpers' interface RestartCostEstimateProps { open: boolean projectRef?: string sourceId?: number publicationName?: string tables: Pick[] } export const calculateRestartCostEstimate = ( estimates: TableCopyEstimate[], tables: Pick[] ) => summarizeTableCopyEstimate(estimates, tables) export const RestartCostEstimate = ({ open, projectRef, sourceId, publicationName, tables, }: RestartCostEstimateProps) => { const { data: estimate, isFetching } = useReplicationCostEstimateQuery( { projectRef, sourceId, publicationName }, { enabled: open && tables.length > 0 } ) const restartEstimate = useMemo( () => estimate === undefined ? undefined : calculateRestartCostEstimate(estimate.table_copy.tables, tables), [estimate, tables] ) return (
{tables.length === 0 ? (

No additional initial sync charge

This reset will skip initial sync based on the pipeline’s settings.

{formatCurrency(0)}
) : isFetching ? ( ) : restartEstimate?.isComplete ? (

Estimated additional initial sync cost

Based on an estimated {formatBytes(restartEstimate.estimatedBytes)} of initial sync data across {tables.length} {tables.length === 1 ? 'table' : 'tables'}

{formatCurrency(restartEstimate.estimatedCost)} {restartEstimate.hasRowFilteredTables ? '*' : null}
) : (

A cost estimate is unavailable. You can still reset the{' '} {tables.length === 1 ? 'table' : 'tables'}.

)} {restartEstimate?.isComplete && restartEstimate.hasRowFilteredTables && (

*Row filters can reduce the data processed compared with this estimate.

)} {restartEstimate?.isComplete && (

Quick planning estimate; the final charge is based on successfully processed initial sync data, which is billed again.

)}
) }