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

Code before:
  severity: SeverityTableRow[],
  setResult: React.Dispatch<React.SetStateAction<AlgorithmResult | undefined>>,
  setEmpiricalCases: React.Dispatch<React.SetStateAction<EmpiricalData | undefined>>,
) {
  const paramsFlat = {
    ...params.population,
    ...params.epidemiological,
    ...params.simulation,
    ...params.containment,
  }

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

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

  const ageDistribution = (countryAgeDistributionData as CountryAgeDistribution)[params.population.country]
  const caseCounts: EmpiricalData = countryCaseCountData[params.population.cases] || []
  const containment: TimeSeries = intervalsToTimeSeries(params.containment.mitigationIntervals)

  serializeScenarioToURL(scenarioState)
  intervalsToTimeSeries(params.containment.mitigationIntervals)
  const newResult = await run(paramsFlat, severity, ageDistribution, containment)

  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 CountryAgeDistribution => {
  return Object.prototype.hasOwnProperty.call(countryAgeDistributionData, country)
}

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

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

Review comment (suggestion):
This is the line I mean! I don't think we can remove `params` since it contains the non-stale data being used.

Code after:
  params: AllParams,
  severity: SeverityTableRow[],
  setResult: React.Dispatch<React.SetStateAction<AlgorithmResult | undefined>>,
  setEmpiricalCases: React.Dispatch<React.SetStateAction<EmpiricalData | undefined>>,
) {
  const paramsFlat = {
    ...params.population,
    ...params.epidemiological,
    ...params.simulation,
    ...params.containment,
  }

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

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

  const ageDistribution = (countryAgeDistributionData as CountryAgeDistribution)[params.population.country]
  const caseCounts: EmpiricalData = countryCaseCountData[params.population.cases] || []
  const containment: TimeSeries = intervalsToTimeSeries(params.containment.mitigationIntervals)

  intervalsToTimeSeries(params.containment.mitigationIntervals)
  const newResult = await run(paramsFlat, severity, ageDistribution, containment)

  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 CountryAgeDistribution => {
  return Object.prototype.hasOwnProperty.call(countryAgeDistributionData, country)
}

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

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