Repository: supabase/supabase
File: apps/studio/components/interfaces/Realtime/RealtimeSettings.tsx (TypeScript)

Code before:
  const isRealtimeDisabled = data?.suspend ?? REALTIME_DEFAULT_CONFIG.suspend
  // Check if RLS policies exist for realtime.messages table
  const realtimeMessagesPolicies = policies?.filter(
    (policy) => policy.schema === 'realtime' && policy.table === 'messages'
  )
  const hasRealtimeMessagesPolicies =
    realtimeMessagesPolicies && realtimeMessagesPolicies.length > 0

  const { mutate: updateRealtimeConfig, isPending: isUpdatingConfig } =
    useRealtimeConfigurationUpdateMutation({
      onSuccess: () => {
        form.reset(form.getValues())
        toast.success('Successfully updated realtime settings')
        setIsConfirmNextModalOpen(false)
      },
    })

  const {
    getEntitlementNumericValue: getEntitledMaxPayloadSize,
    isEntitlementUnlimited: isMaxPayloadSizeUnlimited,
  } = useCheckEntitlements('realtime.max_payload_size_in_kb')
  const entitledMaxPayloadSize = isMaxPayloadSizeUnlimited()
    ? Number.MAX_SAFE_INTEGER
    : getEntitledMaxPayloadSize() ?? 3000

  const {
    getEntitlementNumericValue: getEntitledMaxConcurrentUsers,
    isEntitlementUnlimited: isMaxConcurrentUsersUnlimited,
  } = useCheckEntitlements('realtime.max_concurrent_users')
  const entitledMaxConcurrentUsers = isMaxConcurrentUsersUnlimited()
    ? Number.MAX_SAFE_INTEGER
    : getEntitledMaxConcurrentUsers() ?? 50000

  const {
    getEntitlementNumericValue: getEntitledMaxEventsPerSecond,
    isEntitlementUnlimited: isMaxEventsPerSecondUnlimited,
  } = useCheckEntitlements('realtime.max_events_per_second')
  const entitledMaxEventsPerSecond = isMaxEventsPerSecondUnlimited()
    ? Number.MAX_SAFE_INTEGER
    : getEntitledMaxEventsPerSecond() ?? 10000

  const {
    getEntitlementNumericValue: getEntitledMaxPresenceEventsPerSecond,
    isEntitlementUnlimited: isMaxPresenceEventsPerSecondUnlimited,
  } = useCheckEntitlements('realtime.max_presence_events_per_second')
  const entitledMaxPresenceEventsPerSecond = isMaxPresenceEventsPerSecondUnlimited()
    ? Number.MAX_SAFE_INTEGER
    : getEntitledMaxPresenceEventsPerSecond() ?? 10000

  const FormSchema = z.object({
    connection_pool: z.coerce

Review comment (refactor):
may want to use a helper function like this since it seems repetitive

```
function useEntitlementMax(key: string, fallback: number) {
    const { getEntitlementNumericValue, isEntitlementUnlimited } = useCheckEntitlements(key)
    return isEntitlementUnlimited() ? Number.MAX_SAFE_INTEGER : getEntitlementNumericValue() ?? fallback
  }
```

Code after:
  const isRealtimeDisabled = data?.suspend ?? REALTIME_DEFAULT_CONFIG.suspend
  // Check if RLS policies exist for realtime.messages table
  const realtimeMessagesPolicies = policies?.filter(
    (policy) => policy.schema === 'realtime' && policy.table === 'messages'
  )
  const hasRealtimeMessagesPolicies =
    realtimeMessagesPolicies && realtimeMessagesPolicies.length > 0

  const { mutate: updateRealtimeConfig, isPending: isUpdatingConfig } =
    useRealtimeConfigurationUpdateMutation({
      onSuccess: () => {
        form.reset(form.getValues())
        toast.success('Successfully updated realtime settings')
        setIsConfirmNextModalOpen(false)
      },
    })

  const { getEntitlementMax: getEntitledMaxPayloadSize } = useCheckEntitlements(
    'realtime.max_payload_size_in_kb'
  )
  const entitledMaxPayloadSize = getEntitledMaxPayloadSize() ?? 3000

  const { getEntitlementMax: getEntitledMaxConcurrentUsers } = useCheckEntitlements(
    'realtime.max_concurrent_users'
  )
  const entitledMaxConcurrentUsers = getEntitledMaxConcurrentUsers() ?? 50000

  const { getEntitlementMax: getEntitledMaxEventsPerSecond } = useCheckEntitlements(
    'realtime.max_events_per_second'
  )
  const entitledMaxEventsPerSecond = getEntitledMaxEventsPerSecond() ?? 10000

  const { getEntitlementMax: getEntitledMaxPresenceEventsPerSecond } = useCheckEntitlements(
    'realtime.max_presence_events_per_second'
  )
  const entitledMaxPresenceEventsPerSecond = getEntitledMaxPresenceEventsPerSecond() ?? 10000

  const FormSchema = z.object({
    connection_pool: z.coerce
      .number()
      .min(1)
      .max(maxConn?.maxConnections ?? 100),
    max_concurrent_users: z.coerce.number().min(1).max(entitledMaxConcurrentUsers),
    max_events_per_second: z.coerce.number().min(1).max(entitledMaxEventsPerSecond),
    max_presence_events_per_second: z.coerce
      .number()
      .min(1)
      .max(entitledMaxPresenceEventsPerSecond),
    max_payload_size_in_kb: z.coerce.number().min(1).max(entitledMaxPayloadSize),
    suspend: z.boolean(),
    // [Joshen] These fields are temporarily hidden from the UI