import PermissionDeniedPage from './PermissionDeniedPage'
import { PermissionsStore } from '../shared-state/PermissionsStore'

const accessIfNoKey = process.env.NODE_ENV === 'development' ? true : false

export default function PageWithPermission({
  permissionKey,
  children,
}: {
  permissionKey: string
  children?: any
}) {
  const permissions = PermissionsStore.useState((s) => s).permissions
  const hasAccess = permissionKey ? permissions[permissionKey] : accessIfNoKey

  if (hasAccess) return children
  return <PermissionDeniedPage />
}
