import { createSelector } from 'reselect';
import { watcherTreeSelector } from './index';
import { normalizedWatchers } from './watchers';
import { Watcher } from '../types';

export const getCurrentlySelectedWatcherIdOrNull = createSelector(
  [watcherTreeSelector, normalizedWatchers],
  (watcherSettings, watchers) => {
    const { selectionId, selectionKind } = watcherSettings;

    if (selectionId && selectionKind !== 'folder') {
      const maybeSelectedWatcher: Watcher | undefined = watchers.get(
        selectionId,
        undefined
      );
      return maybeSelectedWatcher ? maybeSelectedWatcher.groupId : null;
    } else {
      return null;
    }
  }
);

import { createSelector } from 'reselect';
import { watcherTreeSelector } from './index';
import { normalizedWatchers } from './watchers';

export const getCurrentlySelectedWatcherIdOrNull = createSelector(
  [watcherTreeSelector, normalizedWatchers],
  (watcherSettings, watchers) => {
    const { selectionId, selectionKind } = watcherSettings;

    if (selectionId && selectionKind !== 'folder') {
      return watchers.get(selectionId).groupId;
    } else {
      return null;
    }
  }
);

--- 
+++ 
@@ -1,6 +1,7 @@
 import { createSelector } from 'reselect';
 import { watcherTreeSelector } from './index';
 import { normalizedWatchers } from './watchers';
+import { Watcher } from '../types';
 
 export const getCurrentlySelectedWatcherIdOrNull = createSelector(
   [watcherTreeSelector, normalizedWatchers],
@@ -8,7 +9,11 @@
     const { selectionId, selectionKind } = watcherSettings;
 
     if (selectionId && selectionKind !== 'folder') {
-      return watchers.get(selectionId).groupId;
+      const maybeSelectedWatcher: Watcher | undefined = watchers.get(
+        selectionId,
+        undefined
+      );
+      return maybeSelectedWatcher ? maybeSelectedWatcher.groupId : null;
     } else {
       return null;
     }
Fix crash when deleting a selected watcher.
