<commit_before>import * as React from 'react'
import { Dispatcher } from '../../lib/dispatcher'
import Repository from '../../models/repository'
import User from '../../models/user'
import API, { IAPIUser, getDotComAPIEndpoint } from '../../lib/api'

interface IPublishRepositoryProps {
  readonly dispatcher: Dispatcher
  readonly repository: Repository
  readonly users: ReadonlyArray<User>
}

interface IPublishRepositoryState {
  readonly name: string
  readonly description: string
  readonly private: boolean
  readonly groupedUsers: Map<User, ReadonlyArray<IAPIUser>>
  readonly selectedUser: IAPIUser
}

export default class PublishRepository extends React.Component<IPublishRepositoryProps, IPublishRepositoryState> {
  public constructor(props: IPublishRepositoryProps) {
    super(props)

    this.state = {
      name: props.repository.name,
      description: '',
      private: true,
      groupedUsers: new Map<User, ReadonlyArray<IAPIUser>>(),
      selectedUser: userToAPIUser(this.props.users[0]),
    }
  }

  public async componentWillMount() {
    const orgsByUser = new Map<User, ReadonlyArray<IAPIUser>>()
    for (const user of Array.from(this.props.users)) {
      const api = new API(user)
      const orgs = await api.fetchOrgs()
      orgsByUser.set(user, orgs)
    }

    this.setState({
      name: this.state.name,
      description: this.state.description,
      private: this.state.private,
      groupedUsers: orgsByUser,
      selectedUser: this.state.selectedUser,
    })
  }

  private onNameChange(event: React.FormEvent<HTMLInputElement>) {
    this.setState({
      name: event.target.value,
      description: this.state.description,
      private: this.state.private,
      groupedUsers: this.state.groupedUsers,
      selectedUser: this.state.selectedUser,
    })
  }

  private onDescriptionChange(event: React.FormEvent<HTMLInputElement>) {
    this.setState({
      name: this.state.name,
      description: event.target.value,
      private: this.state.private,
      groupedUsers: this.state.groupedUsers,
      selectedUser: this.state.selectedUser,
    })
  }

  private onPrivateChange(event: React.FormEvent<HTMLInputElement>) {
    this.setState({
      name: this.state.name,
      description: this.state.description,
      private: event.target.checked,
      groupedUsers: this.state.groupedUsers,
      selectedUser: this.state.selectedUser,
    })
  }

  private findOwningUserForSelectedUser(): User | null {
    const selectedUser = this.state.selectedUser
    for (const [ user, orgs ] of this.state.groupedUsers) {
      const apiUser = userToAPIUser(user)
      if (apiUser.id === selectedUser.id && apiUser.url === selectedUser.url) {
        return user
      }

      let owningAccount: User | null = null
      orgs.forEach(org => {
        if (org.id === selectedUser.id && org.url === selectedUser.url) {
          owningAccount = user
        }
      })

      if (owningAccount) {
        return owningAccount
      }
    }

    return null
  }

  private publishRepository(event: React.FormEvent<HTMLFormElement>) {
    event.preventDefault()

    const owningAccount = this.findOwningUserForSelectedUser()
    this.props.dispatcher.publishRepository(this.props.repository, this.state.name, this.state.description, this.state.private, owningAccount!, this.state.selectedUser)
    this.props.dispatcher.closePopup()
  }

  private onAccountChange(event: React.FormEvent<HTMLSelectElement>) {
    const value = event.target.value
    const selectedUser = JSON.parse(value)

    this.setState({
      name: this.state.name,
      description: this.state.description,
      private: this.state.private,
      groupedUsers: this.state.groupedUsers,
      selectedUser,
    })
  }<commit_msg>Don't pass an org if we chose a user<commit_after>import * as React from 'react'
import { Dispatcher } from '../../lib/dispatcher'
import Repository from '../../models/repository'
import User from '../../models/user'
import API, { IAPIUser, getDotComAPIEndpoint } from '../../lib/api'

interface IPublishRepositoryProps {
  readonly dispatcher: Dispatcher
  readonly repository: Repository
  readonly users: ReadonlyArray<User>
}

interface IPublishRepositoryState {
  readonly name: string
  readonly description: string
  readonly private: boolean
  readonly groupedUsers: Map<User, ReadonlyArray<IAPIUser>>
  readonly selectedUser: IAPIUser
}

export default class PublishRepository extends React.Component<IPublishRepositoryProps, IPublishRepositoryState> {
  public constructor(props: IPublishRepositoryProps) {
    super(props)

    this.state = {
      name: props.repository.name,
      description: '',
      private: true,
      groupedUsers: new Map<User, ReadonlyArray<IAPIUser>>(),
      selectedUser: userToAPIUser(this.props.users[0]),
    }
  }

  public async componentWillMount() {
    const orgsByUser = new Map<User, ReadonlyArray<IAPIUser>>()
    for (const user of Array.from(this.props.users)) {
      const api = new API(user)
      const orgs = await api.fetchOrgs()
      orgsByUser.set(user, orgs)
    }

    this.setState({
      name: this.state.name,
      description: this.state.description,
      private: this.state.private,
      groupedUsers: orgsByUser,
      selectedUser: this.state.selectedUser,
    })
  }

  private onNameChange(event: React.FormEvent<HTMLInputElement>) {
    this.setState({
      name: event.target.value,
      description: this.state.description,
      private: this.state.private,
      groupedUsers: this.state.groupedUsers,
      selectedUser: this.state.selectedUser,
    })
  }

  private onDescriptionChange(event: React.FormEvent<HTMLInputElement>) {
    this.setState({
      name: this.state.name,
      description: event.target.value,
      private: this.state.private,
      groupedUsers: this.state.groupedUsers,
      selectedUser: this.state.selectedUser,
    })
  }

  private onPrivateChange(event: React.FormEvent<HTMLInputElement>) {
    this.setState({
      name: this.state.name,
      description: this.state.description,
      private: event.target.checked,
      groupedUsers: this.state.groupedUsers,
      selectedUser: this.state.selectedUser,
    })
  }

  private findOwningUserForSelectedUser(): User | null {
    const selectedUser = this.state.selectedUser
    for (const [ user, orgs ] of this.state.groupedUsers) {
      const apiUser = userToAPIUser(user)
      if (apiUser.id === selectedUser.id && apiUser.url === selectedUser.url) {
        return user
      }

      let owningAccount: User | null = null
      orgs.forEach(org => {
        if (org.id === selectedUser.id && org.url === selectedUser.url) {
          owningAccount = user
        }
      })

      if (owningAccount) {
        return owningAccount
      }
    }

    return null
  }

  private get selectedOrg(): IAPIUser | null {
    if (this.state.selectedUser.type === 'user') { return null }

    return this.state.selectedUser
  }

  private publishRepository(event: React.FormEvent<HTMLFormElement>) {
    event.preventDefault()

    const owningAccount = this.findOwningUserForSelectedUser()!
    this.props.dispatcher.publishRepository(this.props.repository, this.state.name, this.state.description, this.state.private, owningAccount, this.selectedOrg)
    this.props.dispatcher.closePopup()
  }

  private onAccountChange(event: React.FormEvent<HTMLSelectElement>) {
    const value = event.target.value
    const selectedUser = JSON.parse(value)

    this.setState({
      name: this.state.name,
      description: this.state.description,
      private: this.state.private,
      groupedUsers: this.state.groupedUsers,
      selectedUser,
    })
  }
{'fraction_non_alphanumeric': 0.10927540002601795, 'fraction_numerical': 0.0002601795238714713, 'mean_word_length': 3.2640044370493624, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, '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': '4877845', 'n_tokens_mistral': 2250, 'n_tokens_neox': 2224, 'n_words': 472}