Repository: neherlab/covid19_scenarios
File: src/components/Main/Main.tsx (TypeScript)

Code before:
  if (!isCountry(params.population.country)) {
    console.error(`The given country is invalid: ${params.population.country}`)
    return
  }

  if (!isRegion(params.population.cases)) {
    console.error(`The given confirmed cases region is invalid: ${params.population.cases}`)
    return
  }

  const ageDistribution = countryAgeDistributionData[params.population.country]
  const caseCounts: EmpiricalData = countryCaseCounts[params.population.cases] || []
  const containmentData = params.containment.reduction

  serializeScenarioToURL(scenarioState, params)

  const newResult = await run(paramsFlat, severity, ageDistribution, containmentData)
  setResult(newResult)
  caseCounts.sort((a, b) => (a.time > b.time ? 1 : -1))
  setEmpiricalCases(caseCounts)
}

const severityDefaults: SeverityTableRow[] = updateSeverityTable(severityData)

const isCountry = (country: string): country is keyof typeof countryAgeDistribution => {
  return countryAgeDistributionData.hasOwnProperty(country)
}

const isRegion = (region: string): region is keyof typeof countryCaseCounts => {
  return countryCaseCounts.hasOwnProperty(region)
}

function Main() {
  const [result, setResult] = useState<AlgorithmResult | undefined>()
  const [autorunSimulation, setAutorunSimulation] = useState(false)
  const [scenarioState, scenarioDispatch] = useReducer(
    scenarioReducer,
    defaultScenarioState,
    deserializeScenarioFromURL,
  )

  // TODO: Can this complex state be handled by formik too?
  const [severity, setSeverity] = useState<SeverityTableRow[]>(severityDefaults)

  const [empiricalCases, setEmpiricalCases] = useState<EmpiricalData | undefined>()

  const toggleAutorun = () => setAutorunSimulation(!autorunSimulation)

  const allParams: AllParams = {
    population: scenarioState.population.data,
    epidemiological: scenarioState.epidemiological.data,

Review comment (suggestion):
`countryAgeDistribution` type doesn't exist. `countryAgeDistributionData` is the correct name.

Code after:
  if (!isCountry(params.population.country)) {
    console.error(`The given country is invalid: ${params.population.country}`)
    return
  }

  if (!isRegion(params.population.cases)) {
    console.error(`The given confirmed cases region is invalid: ${params.population.cases}`)
    return
  }

  const ageDistribution = countryAgeDistributionData[params.population.country]
  const caseCounts: EmpiricalData = countryCaseCountData[params.population.cases] || []
  const containmentData = params.containment.reduction

  serializeScenarioToURL(scenarioState, params)

  const newResult = await run(paramsFlat, severity, ageDistribution, containmentData)
  setResult(newResult)
  caseCounts.sort((a, b) => (a.time > b.time ? 1 : -1))
  setEmpiricalCases(caseCounts)
}

const severityDefaults: SeverityTableRow[] = updateSeverityTable(severityData)

const isCountry = (country: string): country is keyof typeof countryAgeDistributionData => {
  return countryAgeDistributionData.hasOwnProperty(country)
}

const isRegion = (region: string): region is keyof typeof countryCaseCountData => {
  return countryCaseCountData.hasOwnProperty(region)
}

function Main() {
  const [result, setResult] = useState<AlgorithmResult | undefined>()
  const [autorunSimulation, setAutorunSimulation] = useState(false)
  const [scenarioState, scenarioDispatch] = useReducer(
    scenarioReducer,
    defaultScenarioState,
    deserializeScenarioFromURL,
  )

  // TODO: Can this complex state be handled by formik too?
  const [severity, setSeverity] = useState<SeverityTableRow[]>(severityDefaults)

  const [empiricalCases, setEmpiricalCases] = useState<EmpiricalData | undefined>()

  const toggleAutorun = () => setAutorunSimulation(!autorunSimulation)

  const allParams: AllParams = {
    population: scenarioState.population.data,
    epidemiological: scenarioState.epidemiological.data,