<commit_before>import React, { useMemo, useState } from 'react';
import { useHistory, useRouteMatch } from 'react-router-dom';

import EditRunModal from './EditRunModal';
import buildEventCategoryUrl from './buildEventCategoryUrl';
import {
  ConventionFieldsFragment,
  EventFieldsFragment,
  RunFieldsFragment,
} from './queries.generated';

export type EditRunProps = {
  convention: ConventionFieldsFragment;
  events: EventFieldsFragment[];
};

function EditRun({ convention, events }: EditRunProps) {
  const match = useRouteMatch<{ eventId: string; runId: string }>();
  const history = useHistory();
  const event = useMemo(() => {
    if (!match) {
      return undefined;
    }

    return events.find((e) => e.id.toString() === match.params.eventId);
  }, [match, events]);

  const initialRun: RunFieldsFragment | undefined = useMemo(() => {
    if (!match || !event) {
      return undefined;
    }

    if (match.path.endsWith('/new')) {
      return {
        __typename: 'Run',
        id: 0,
        my_signups: [],
        my_signup_requests: [],
        starts_at: '',
        title_suffix: undefined,
        schedule_note: undefined,
        rooms: [],
        room_names: [],
        confirmed_signup_count: 0,
        not_counted_signup_count: 0,
        signup_count_by_state_and_bucket_key_and_counted: '{}',
      };
    }

    return event.runs.find((r) => r.id.toString() === match.params.runId);
  }, [match, event]);

  const cancelEditing = () => {
    if (!event) {
      return;
    }
    const eventCategoryUrl = buildEventCategoryUrl(
      convention.event_categories.find((c) => c.id === event.event_category.id),
    );
    if (eventCategoryUrl) {
      history.replace(eventCategoryUrl);
    }
  };

  const [run, setRun] = useState(initialRun);
  const [prevMatch, setPrevMatch] = useState(match);

  if (prevMatch !== match) {
    // navigation happened, reset the run state
    setRun(initialRun);
    setPrevMatch(match);
  }

  if (!event || !run) {
    return <></>;
  }

  return (
    <EditRunModal
      editingRunChanged={setRun}
      event={event}
      onCancel={cancelEditing}
      onDelete={cancelEditing}
      onSaveStart={() => {}}
      onSaveSucceeded={cancelEditing}
      onSaveFailed={() => {}}
      run={run}
    />
  );
}

export default EditRun;<commit_msg>Be less trigger-happy with the state reset<commit_after>import React, { useEffect, useMemo, useState } from 'react';
import { useHistory, useRouteMatch } from 'react-router-dom';

import EditRunModal from './EditRunModal';
import buildEventCategoryUrl from './buildEventCategoryUrl';
import {
  ConventionFieldsFragment,
  EventFieldsFragment,
  RunFieldsFragment,
} from './queries.generated';

export type EditRunProps = {
  convention: ConventionFieldsFragment;
  events: EventFieldsFragment[];
};

function EditRun({ convention, events }: EditRunProps) {
  const match = useRouteMatch<{ eventId: string; runId: string }>();
  const history = useHistory();
  const event = useMemo(() => {
    if (!match) {
      return undefined;
    }

    return events.find((e) => e.id.toString() === match.params.eventId);
  }, [match, events]);

  const initialRun: RunFieldsFragment | undefined = useMemo(() => {
    if (!event) {
      return undefined;
    }

    if (match.path.endsWith('/new')) {
      return {
        __typename: 'Run',
        id: 0,
        my_signups: [],
        my_signup_requests: [],
        starts_at: '',
        title_suffix: undefined,
        schedule_note: undefined,
        rooms: [],
        room_names: [],
        confirmed_signup_count: 0,
        not_counted_signup_count: 0,
        signup_count_by_state_and_bucket_key_and_counted: '{}',
      };
    }

    return event.runs.find((r) => r.id.toString() === match.params.runId);
  }, [match.path, match.params.runId, event]);

  const cancelEditing = () => {
    if (!event) {
      return;
    }
    const eventCategoryUrl = buildEventCategoryUrl(
      convention.event_categories.find((c) => c.id === event.event_category.id),
    );
    if (eventCategoryUrl) {
      history.replace(eventCategoryUrl);
    }
  };

  const [run, setRun] = useState(initialRun);

  useEffect(() => {
    // navigation happened, reset the run state
    setRun(initialRun);
  }, [match.path, initialRun]);

  if (!event || !run) {
    return <></>;
  }

  return (
    <EditRunModal
      editingRunChanged={setRun}
      event={event}
      onCancel={cancelEditing}
      onDelete={cancelEditing}
      onSaveStart={() => {}}
      onSaveSucceeded={cancelEditing}
      onSaveFailed={() => {}}
      run={run}
    />
  );
}

export default EditRun;
{'fraction_non_alphanumeric': 0.1438415159345392, 'fraction_numerical': 0.0012919896640826874, 'mean_word_length': 3.0532286212914483, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, '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': '113351', 'n_tokens_mistral': 1446, 'n_tokens_neox': 1436, 'n_words': 295}