Repository: supabase/supabase
File: apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/NamespaceWithTables/TableRowComponent.tsx (TypeScript)

Code before:
    projectRef,
    bucketId,
  })
  const { data } = useReplicationPipelineStatusQuery({ projectRef, pipelineId: pipeline?.id })
  const pipelineStatus = data?.status.name

  const { data: tables } = useReplicationTablesQuery({ projectRef, sourceId })
  const { data: wrapperInstance, meta: wrapperMeta } = useAnalyticsBucketWrapperInstance({
    bucketId: bucketId,
  })

  const { mutateAsync: updateFDW } = useFDWUpdateMutation()
  const { mutateAsync: deleteNamespaceTable } = useIcebergNamespaceTableDeleteMutation()
  const { mutateAsync: updatePublication } = useUpdatePublicationMutation()
  const { mutateAsync: startPipeline } = useStartPipelineMutation()

  const inferredPostgresTable = inferPostgresTableFromNamespaceTable({
    publication,
    tableName: table.name,
  })
  const isTableUnderReplicationPublication = !!inferredPostgresTable
  const hasReplication = !!pipeline && !!publication
  const isPipelineRunning = pipelineStatus === 'started'
  const isReplicating = isTableUnderReplicationPublication && isPipelineRunning

  const getStatusLabel = () => {
    if (isLoading) return 'Checking'
    if (hasReplication && isTableUnderReplicationPublication) {
      if (isPipelineRunning) {
        return 'Running'
      } else {
        return pipelineStatus ?? 'unknown'
      }
    } else {
      return 'Stopped'
    }
  }

  const onConfirmStopReplication = async () => {
    if (!projectRef) return console.error('Project ref is required')
    if (!bucketId) return console.error('Bucket ID is required')
    if (!sourceId) return toast.error('Source ID is required')
    if (!publication) return toast.error('Unable to find existing publication')
    if (!pipeline) return toast.error('Unable to find existing pipeline')

    try {
      setIsUpdatingReplication(true)
      // [Joshen ALPHA] Assumption here is that all the namespace tables have _changelog as suffix
      // May need to update if that assumption falls short (e.g for those dealing with iceberg APIs directly)
      const updatedTables = publication.tables.filter(
        (x) => table.name !== snakeCase(`${x.schema}.${x.name}_changelog`)

Review comment (suggestion):
Nit: convert this to a useMemo instead of a function call... `{getStatusLabel()}` makes me feel weird lol

Code after:
    projectRef,
    bucketId,
  })
  const { data } = useReplicationPipelineStatusQuery({ projectRef, pipelineId: pipeline?.id })
  const pipelineStatus = data?.status.name

  const { data: tables } = useReplicationTablesQuery({ projectRef, sourceId })
  const { data: wrapperInstance, meta: wrapperMeta } = useAnalyticsBucketWrapperInstance({
    bucketId: bucketId,
  })

  const { mutateAsync: updateFDW } = useFDWUpdateMutation()
  const { mutateAsync: deleteNamespaceTable } = useIcebergNamespaceTableDeleteMutation()
  const { mutateAsync: updatePublication } = useUpdatePublicationMutation()
  const { mutateAsync: startPipeline } = useStartPipelineMutation()

  const inferredPostgresTable = inferPostgresTableFromNamespaceTable({
    publication,
    tableName: table.name,
  })
  const isTableUnderReplicationPublication = !!inferredPostgresTable
  const hasReplication = !!pipeline && !!publication
  const isPipelineRunning = pipelineStatus === 'started'
  const isReplicating = isTableUnderReplicationPublication && isPipelineRunning

  // [Joshen] Considers both the replication pipeline status + if the table is in the replication publication
  const replicationStatusLabel = useMemo(() => {
    if (isLoading) return 'Checking'

    if (hasReplication) {
      if (!isPipelineRunning) {
        return '-'
      } else if (isTableUnderReplicationPublication) {
        return 'Running'
      } else {
        return 'Disabled'
      }
    }
  }, [hasReplication, isLoading, isPipelineRunning, isTableUnderReplicationPublication])

  const onConfirmStopReplication = async () => {
    if (!projectRef) return console.error('Project ref is required')
    if (!bucketId) return console.error('Bucket ID is required')
    if (!sourceId) return toast.error('Source ID is required')
    if (!publication) return toast.error('Unable to find existing publication')
    if (!pipeline) return toast.error('Unable to find existing pipeline')

    try {
      setIsUpdatingReplication(true)
      // [Joshen ALPHA] Assumption here is that all the namespace tables have _changelog as suffix
      // May need to update if that assumption falls short (e.g for those dealing with iceberg APIs directly)