<commit_before>import React, { Component } from 'react'

import Icon from './icon'
import propTypes from './prop-types'
import { createGetObject } from './selectors'
import { isSrWritable } from './xo'
import {
  connectStore,
  formatSize
} from './utils'

// ===================================================================

const OBJECT_TYPE_TO_ICON = {
  'VM-template': 'vm',
  host: 'host',
  network: 'network'
}

// Host, Network, VM-template.
export const PoolObjectItem = propTypes({
  object: propTypes.object.isRequired
})(connectStore(() => {
  const getPool = createGetObject(
    (_, props) => props.object.$pool
  )

  return (state, props) => ({
    pool: getPool(state, props)
  })
})(({ object, pool }) => {
  const icon = OBJECT_TYPE_TO_ICON[object.type]
  const { id } = object

  return (
    <span>
      <Icon icon={icon} /> {`${object.name_label || id} `}
      {pool && `(${pool.name_label || pool.id})`}
    </span>
  )
}))

// SR.
export const SrItem = propTypes({
  sr: propTypes.object.isRequired
})(connectStore(() => {
  const getContainer = createGetObject(
    (_, props) => props.sr.$container
  )

  return (state, props) => ({
    container: getContainer(state, props)
  })
})(({ sr, container }) => {
  let label = `${sr.name_label || sr.id}`

  if (isSrWritable(sr)) {
    label += ` (${formatSize(sr.size)})`
  }

  return (
    <span>
      <Icon icon='sr' /> {label}
      {container && ` (${container.name_label || container.id})`}
    </span>
  )
}))

// VM.
export const VmItem = propTypes({
  vm: propTypes.object.isRequired
})(connectStore(() => {
  const getContainer = createGetObject(
    (_, props) => props.vm.$container
  )

  return (state, props) => ({
    container: getContainer(state, props)
  })
})(({ vm, container }) => (
  <span>
    <Icon icon={`vm-${vm.power_state.toLowerCase()}`} /> {vm.name_label || vm.id}
    {container && ` (${container.name_label || container.id})`}
  </span>
)))

// ===================================================================

const xoItemToRender = {
  // Subscription objects.
  group: group => (<commit_msg>feat(sr select): display space left on SR. Fixes #1358<commit_after>import React, { Component } from 'react'

import Icon from './icon'
import propTypes from './prop-types'
import { createGetObject } from './selectors'
import { isSrWritable } from './xo'
import {
  connectStore,
  formatSize
} from './utils'

// ===================================================================

const OBJECT_TYPE_TO_ICON = {
  'VM-template': 'vm',
  host: 'host',
  network: 'network'
}

// Host, Network, VM-template.
export const PoolObjectItem = propTypes({
  object: propTypes.object.isRequired
})(connectStore(() => {
  const getPool = createGetObject(
    (_, props) => props.object.$pool
  )

  return (state, props) => ({
    pool: getPool(state, props)
  })
})(({ object, pool }) => {
  const icon = OBJECT_TYPE_TO_ICON[object.type]
  const { id } = object

  return (
    <span>
      <Icon icon={icon} /> {`${object.name_label || id} `}
      {pool && `(${pool.name_label || pool.id})`}
    </span>
  )
}))

// SR.
export const SrItem = propTypes({
  sr: propTypes.object.isRequired
})(connectStore(() => {
  const getContainer = createGetObject(
    (_, props) => props.sr.$container
  )

  return (state, props) => ({
    container: getContainer(state, props)
  })
})(({ sr, container }) => {
  let label = `${sr.name_label || sr.id}`

  if (isSrWritable(sr)) {
    label += ` (${formatSize(sr.size - sr.physical_usage)} free)`
  }

  return (
    <span>
      <Icon icon='sr' /> {label}
    </span>
  )
}))

// VM.
export const VmItem = propTypes({
  vm: propTypes.object.isRequired
})(connectStore(() => {
  const getContainer = createGetObject(
    (_, props) => props.vm.$container
  )

  return (state, props) => ({
    container: getContainer(state, props)
  })
})(({ vm, container }) => (
  <span>
    <Icon icon={`vm-${vm.power_state.toLowerCase()}`} /> {vm.name_label || vm.id}
    {container && ` (${container.name_label || container.id})`}
  </span>
)))

// ===================================================================

const xoItemToRender = {
  // Subscription objects.
  group: group => (
{'fraction_non_alphanumeric': 0.24265402843601896, 'fraction_numerical': 0.0009478672985781991, 'mean_word_length': 3.573131094257855, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '638832', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1378, 'n_words': 301}