<commit_before>import { useContext } from 'react';
import { Modal, Button } from 'react-bootstrap';
import { Form } from 'lib/elektra-form';
import { Base64 } from 'js-base64';

const initialValues = { role: '', name: '', url: '', username: '', password: '', token: '' };

const roleInfoTexts = {
  'primary': "You can push images into this account. Accounts in other regions can replicate from this account.",
  'replica': "This account replicates images from a primary account in a different region with the same name. You cannot push images into this account directly. Images are replicated on first use, when a client first tries to pull them.",
  'external_replica': "This account replicates images from a registry outside of Keppel (e.g. Docker Hub). You cannot push images into this account directly. Images are replicated on first use, when a client first tries to pull them. Only pulls by authenticated users can trigger replication.",
};

const decodeSubleaseToken = (token) => {
  try {
    const t = JSON.parse(Base64.decode(token));
    if (t.account && t.primary && t.secret) {
      return t;
    } else {
      return {}; //looks incomplete
    }
  } catch {
    return {}; //looks broken
  }
};

const isValidSubleaseToken = (token) => {
  return (token.account && token.primary && token.secret) ? true : false;
};

const BackingStorageInfo = ({ accountName }) => (
  <Form.ElementHorizontal label='Backing storage' name='backing_storage'>
    <p className='form-control-static'>
      Swift container <strong>keppel-{accountName}</strong><br/>
      <span className='text-muted'>The container will be created if it does not exist yet. Please ensure that you have sufficient object storage quota.</span>
    </p>
  </Form.ElementHorizontal>
);

const FormBody = ({ values }) => {
  const accountName  = values.name || '';
  const roleInfoText = roleInfoTexts[values.role || ''];

  const decodedToken = decodeSubleaseToken(values.token);
  const {
    account: accountNameFromToken,
    primary: primaryHostNameFromToken,
  } = decodedToken;
  const isValidToken = isValidSubleaseToken(decodedToken);

  return (
    <Modal.Body>
      <Form.Errors/>

      <Form.ElementHorizontal label='Role' name='role' required>
        <Form.Input elementType='select' name='role'>
          {values.role ? null : <option value=''>-- Please select --</option>}
          <option value='primary'>Primary account</option>
          <option value='replica'>Replica account</option>
          <option value='external_replica'>External replica account</option>
        </Form.Input>
        {roleInfoText && <p className='form-control-static'>{roleInfoText}</p>}
      </Form.ElementHorizontal>

      {(values.role == 'primary' || values.role == 'external_replica') && (
        <React.Fragment>

          <Form.ElementHorizontal label='Name' name='name' required>
            <Form.Input elementType='input' type='text' name='name' />
          </Form.ElementHorizontal>

          {accountName ? (
            <React.Fragment>

              <BackingStorageInfo accountName={accountName} />

              {values.role == 'external_replica' && (
                <React.Fragment>

                  <Form.ElementHorizontal label='Upstream source' name='url' required>
                    <Form.Input elementType='input' type='text' name='url' />
                    <p className='form-control-static'>{'Enter the domain name of a registry (for Docker Hub, use "registry-1.docker.io"). If you only want to replicate images below a certain path, append the path after the domain name (e.g. "gcr.io/google_containers").'}</p>
                  </Form.ElementHorizontal><commit_msg>use the more canonical domain name for Docker Hub<commit_after>import { useContext } from 'react';
import { Modal, Button } from 'react-bootstrap';
import { Form } from 'lib/elektra-form';
import { Base64 } from 'js-base64';

const initialValues = { role: '', name: '', url: '', username: '', password: '', token: '' };

const roleInfoTexts = {
  'primary': "You can push images into this account. Accounts in other regions can replicate from this account.",
  'replica': "This account replicates images from a primary account in a different region with the same name. You cannot push images into this account directly. Images are replicated on first use, when a client first tries to pull them.",
  'external_replica': "This account replicates images from a registry outside of Keppel (e.g. Docker Hub). You cannot push images into this account directly. Images are replicated on first use, when a client first tries to pull them. Only pulls by authenticated users can trigger replication.",
};

const decodeSubleaseToken = (token) => {
  try {
    const t = JSON.parse(Base64.decode(token));
    if (t.account && t.primary && t.secret) {
      return t;
    } else {
      return {}; //looks incomplete
    }
  } catch {
    return {}; //looks broken
  }
};

const isValidSubleaseToken = (token) => {
  return (token.account && token.primary && token.secret) ? true : false;
};

const BackingStorageInfo = ({ accountName }) => (
  <Form.ElementHorizontal label='Backing storage' name='backing_storage'>
    <p className='form-control-static'>
      Swift container <strong>keppel-{accountName}</strong><br/>
      <span className='text-muted'>The container will be created if it does not exist yet. Please ensure that you have sufficient object storage quota.</span>
    </p>
  </Form.ElementHorizontal>
);

const FormBody = ({ values }) => {
  const accountName  = values.name || '';
  const roleInfoText = roleInfoTexts[values.role || ''];

  const decodedToken = decodeSubleaseToken(values.token);
  const {
    account: accountNameFromToken,
    primary: primaryHostNameFromToken,
  } = decodedToken;
  const isValidToken = isValidSubleaseToken(decodedToken);

  return (
    <Modal.Body>
      <Form.Errors/>

      <Form.ElementHorizontal label='Role' name='role' required>
        <Form.Input elementType='select' name='role'>
          {values.role ? null : <option value=''>-- Please select --</option>}
          <option value='primary'>Primary account</option>
          <option value='replica'>Replica account</option>
          <option value='external_replica'>External replica account</option>
        </Form.Input>
        {roleInfoText && <p className='form-control-static'>{roleInfoText}</p>}
      </Form.ElementHorizontal>

      {(values.role == 'primary' || values.role == 'external_replica') && (
        <React.Fragment>

          <Form.ElementHorizontal label='Name' name='name' required>
            <Form.Input elementType='input' type='text' name='name' />
          </Form.ElementHorizontal>

          {accountName ? (
            <React.Fragment>

              <BackingStorageInfo accountName={accountName} />

              {values.role == 'external_replica' && (
                <React.Fragment>

                  <Form.ElementHorizontal label='Upstream source' name='url' required>
                    <Form.Input elementType='input' type='text' name='url' />
                    <p className='form-control-static'>{'Enter the domain name of a registry (for Docker Hub, use "index.docker.io"). If you only want to replicate images below a certain path, append the path after the domain name (e.g. "gcr.io/google_containers").'}</p>
                  </Form.ElementHorizontal>
{'fraction_non_alphanumeric': 0.12778904665314403, 'fraction_numerical': 0.001757944557133198, 'mean_word_length': 3.245694603903559, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 85, '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': '2496257', 'n_tokens_mistral': 2045, 'n_tokens_neox': 1983, 'n_words': 629}