Browse Source

ref(select): Remove useState in teamSelector (#28738)

With recent changes to the <Projects> utility in #28610 we no longer have to maintain internal state here for keeping track of teams which were added to the current project.

In order to still achieve the same optimistic update that we had before with local state we now assume that if a team/option is selected then it is already part of the project's teams.
David Wang 3 years ago
parent
commit
f3ffb93bab
1 changed files with 7 additions and 22 deletions
  1. 7 22
      static/app/components/forms/teamSelector.tsx

+ 7 - 22
static/app/components/forms/teamSelector.tsx

@@ -1,4 +1,4 @@
-import {useEffect, useRef, useState} from 'react';
+import {useRef} from 'react';
 import {StylesConfig} from 'react-select';
 import {StylesConfig} from 'react-select';
 import styled from '@emotion/styled';
 import styled from '@emotion/styled';
 import debounce from 'lodash/debounce';
 import debounce from 'lodash/debounce';
@@ -113,8 +113,6 @@ function TeamSelector(props: Props) {
   const api = useApi();
   const api = useApi();
   const {teams, fetching, onSearch} = useTeams();
   const {teams, fetching, onSearch} = useTeams();
 
 
-  const [options, setOptions] = useState<TeamOption[]>([]);
-
   // TODO(ts) This type could be improved when react-select types are better.
   // TODO(ts) This type could be improved when react-select types are better.
   const selectRef = useRef<any>(null);
   const selectRef = useRef<any>(null);
 
 
@@ -160,18 +158,6 @@ function TeamSelector(props: Props) {
 
 
     try {
     try {
       await addTeamToProject(api, organization.slug, project.slug, team);
       await addTeamToProject(api, organization.slug, project.slug, team);
-
-      // Remove add to project button without changing order
-      const newOptions = options.map(option => {
-        if (option.actor?.id === team.id) {
-          option.disabled = false;
-          option.label = <IdBadge team={team} />;
-        }
-
-        return option;
-      });
-
-      setOptions(newOptions);
     } catch (err) {
     } catch (err) {
       // Unable to add team to project, revert select menu value
       // Unable to add team to project, revert select menu value
       onChange?.(oldValue);
       onChange?.(oldValue);
@@ -181,6 +167,10 @@ function TeamSelector(props: Props) {
   }
   }
 
 
   function createTeamOutsideProjectOption(team: Team): TeamOption {
   function createTeamOutsideProjectOption(team: Team): TeamOption {
+    // If the option/team is currently selected, optimistically assume it is now a part of the project
+    if (value === (useId ? team.id : team.slug)) {
+      return createTeamOption(team);
+    }
     const canAddTeam = organization.access.includes('project:write');
     const canAddTeam = organization.access.includes('project:write');
 
 
     return {
     return {
@@ -217,7 +207,7 @@ function TeamSelector(props: Props) {
     };
     };
   }
   }
 
 
-  function getInitialOptions() {
+  function getOptions() {
     const filteredTeams = teamFilter ? teams.filter(teamFilter) : teams;
     const filteredTeams = teamFilter ? teams.filter(teamFilter) : teams;
 
 
     if (project) {
     if (project) {
@@ -242,15 +232,10 @@ function TeamSelector(props: Props) {
     ];
     ];
   }
   }
 
 
-  useEffect(
-    () => void setOptions(getInitialOptions()),
-    [teams, teamFilter, project, includeUnassigned]
-  );
-
   return (
   return (
     <SelectControl
     <SelectControl
       ref={selectRef}
       ref={selectRef}
-      options={options}
+      options={getOptions()}
       onInputChange={debounce(val => void onSearch(val), DEFAULT_DEBOUNCE_DURATION)}
       onInputChange={debounce(val => void onSearch(val), DEFAULT_DEBOUNCE_DURATION)}
       isOptionDisabled={option => !!option.disabled}
       isOptionDisabled={option => !!option.disabled}
       styles={{
       styles={{