import { useState, useEffect } from 'react'
import { getPageSize } from '../lib/pageSize'

/**
 * 响应式分页大小 hook
 * 当用户在系统设置中修改分页大小时，所有使用此 hook 的页面会自动更新
 */
export function usePageSize() {
  const [pageSize, setPageSizeState] = useState(getPageSize())

  useEffect(() => {
    const handler = () => setPageSizeState(getPageSize())
    window.addEventListener('page-size-changed', handler)
    return () => window.removeEventListener('page-size-changed', handler)
  }, [])

  return pageSize
}
