Repository: facebook/react
File: packages/react-devtools-shared/src/devtools/views/Components/TreeContext.js (JavaScript)

Code before:
  const [state, dispatch] = useReducer(reducer, {
    // Tree
    numElements: store.numElements,
    ownerSubtreeLeafElementID: null,
    selectedElementID:
      defaultSelectedElementID == null ? null : defaultSelectedElementID,
    selectedElementIndex:
      defaultSelectedElementIndex == null ? null : defaultSelectedElementIndex,

    // Search
    searchIndex: null,
    searchResults: [],
    searchText: '',

    // Owners
    ownerID: defaultOwnerID == null ? null : defaultOwnerID,
    ownerFlatTree: null,

    // Inspection element panel
    inspectedElementID:
      defaultInspectedElementID == null ? null : defaultInspectedElementID,
  });

  const dispatchWrapper = useCallback(
    (action: Action) => {
      dispatch(action);
      startTransition(() => {
        dispatch({type: 'UPDATE_INSPECTED_ELEMENT_ID'});
      });
    },
    [dispatch],
  );

  // Listen for host element selections.
  useEffect(() => {
    const handleSelectElement = (id: number) =>
      dispatchWrapper({type: 'SELECT_ELEMENT_BY_ID', payload: id});
    bridge.addListener('selectElement', handleSelectElement);
    return () => bridge.removeListener('selectElement', handleSelectElement);
  }, [bridge, dispatchWrapper]);

  // If a newly-selected search result or inspection selection is inside of a collapsed subtree, auto expand it.
  // This needs to be a layout effect to avoid temporarily flashing an incorrect selection.
  const prevSelectedElementID = useRef<number | null>(null);
  useLayoutEffect(() => {
    if (state.selectedElementID !== prevSelectedElementID.current) {
      prevSelectedElementID.current = state.selectedElementID;

      if (state.selectedElementID !== null) {
        const element = store.getElementByID(state.selectedElementID);
        if (element !== null && element.parentID > 0) {

Review comment (suggestion):
It's a bit weird. why isn't it:
```
store.lastSelectedHostInstanceElementId ? store.lastSelectedHostInstanceElementId : defaultInspectedElementID == null ? null : defaultInspectedElementID
``` 

Code after:
    // Tree
    numElements: store.numElements,
    ownerSubtreeLeafElementID: null,
    selectedElementID:
      defaultSelectedElementID != null
        ? defaultSelectedElementID
        : store.lastSelectedHostInstanceElementId,
    selectedElementIndex:
      defaultSelectedElementIndex != null
        ? defaultSelectedElementIndex
        : store.lastSelectedHostInstanceElementId
          ? store.getIndexOfElementID(store.lastSelectedHostInstanceElementId)
          : null,

    // Search
    searchIndex: null,
    searchResults: [],
    searchText: '',

    // Owners
    ownerID: defaultOwnerID == null ? null : defaultOwnerID,
    ownerFlatTree: null,

    // Inspection element panel
    inspectedElementID:
      defaultInspectedElementID != null
        ? defaultInspectedElementID
        : store.lastSelectedHostInstanceElementId,
  });

  const dispatchWrapper = useCallback(
    (action: Action) => {
      dispatch(action);
      startTransition(() => {
        dispatch({type: 'UPDATE_INSPECTED_ELEMENT_ID'});
      });
    },
    [dispatch],
  );

  // Listen for host element selections.
  useEffect(() => {
    const handler = (id: Element['id']) =>
      dispatchWrapper({type: 'SELECT_ELEMENT_BY_ID', payload: id});

    store.addListener('hostInstanceSelected', handler);
    return () => store.removeListener('hostInstanceSelected', handler);
  }, [store, dispatchWrapper]);

  // If a newly-selected search result or inspection selection is inside of a collapsed subtree, auto expand it.
  // This needs to be a layout effect to avoid temporarily flashing an incorrect selection.