teamStore.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {createStore} from 'reflux';
  2. import {Team} from 'sentry/types';
  3. import {defined} from 'sentry/utils';
  4. import ProjectsStore from './projectsStore';
  5. import {CommonStoreDefinition} from './types';
  6. type State = {
  7. cursor: string | null;
  8. hasMore: boolean | null;
  9. loadedUserTeams: boolean;
  10. loading: boolean;
  11. teams: Team[];
  12. };
  13. interface TeamStoreDefinition extends CommonStoreDefinition<State> {
  14. getAll(): Team[];
  15. getById(id: string): Team | null;
  16. getBySlug(slug: string): Team | null;
  17. init(): void;
  18. initialized: boolean;
  19. loadInitialData(items: Team[], hasMore?: boolean | null, cursor?: string | null): void;
  20. loadUserTeams(userTeams: Team[]): void;
  21. onCreateSuccess(team: Team): void;
  22. onRemoveSuccess(slug: string): void;
  23. onUpdateSuccess(itemId: string, response: Team): void;
  24. reset(): void;
  25. state: State;
  26. }
  27. const teamStoreConfig: TeamStoreDefinition = {
  28. initialized: false,
  29. state: {
  30. teams: [],
  31. loadedUserTeams: false,
  32. loading: true,
  33. hasMore: null,
  34. cursor: null,
  35. },
  36. init() {
  37. this.reset();
  38. },
  39. reset() {
  40. this.state = {
  41. teams: [],
  42. loadedUserTeams: false,
  43. loading: true,
  44. hasMore: null,
  45. cursor: null,
  46. };
  47. },
  48. setTeams(teams, hasMore, cursor) {
  49. this.initialized = true;
  50. this.state = {
  51. teams,
  52. loadedUserTeams: defined(hasMore) ? !hasMore : this.state.loadedUserTeams,
  53. loading: false,
  54. hasMore: hasMore ?? this.state.hasMore,
  55. cursor: cursor ?? this.state.cursor,
  56. };
  57. this.trigger(new Set(teams.map(team => team.id)));
  58. },
  59. loadInitialData(items, hasMore, cursor) {
  60. const teams = this.updateTeams(items);
  61. this.setTeams(teams, hasMore, cursor);
  62. },
  63. loadUserTeams(userTeams: Team[]) {
  64. const teams = this.updateTeams(userTeams);
  65. this.state = {
  66. ...this.state,
  67. loadedUserTeams: true,
  68. teams,
  69. };
  70. this.trigger(new Set(teams.map(team => team.id)));
  71. },
  72. onUpdateSuccess(itemId, response) {
  73. if (!response) {
  74. return;
  75. }
  76. const item = this.getBySlug(itemId);
  77. if (!item) {
  78. this.state = {
  79. ...this.state,
  80. teams: [...this.state.teams, response],
  81. };
  82. this.trigger(new Set([itemId]));
  83. return;
  84. }
  85. // Slug was changed
  86. // Note: This is the proper way to handle slug changes but unfortunately not all of our
  87. // components use stores correctly. To be safe reload browser :((
  88. if (response.slug !== itemId) {
  89. // Replace the team
  90. const teams = [...this.state.teams.filter(({slug}) => slug !== itemId), response];
  91. this.state = {...this.state, teams};
  92. this.trigger(new Set([response.slug]));
  93. return;
  94. }
  95. const newTeams = [...this.state.teams];
  96. const index = newTeams.findIndex(team => team.slug === response.slug);
  97. newTeams[index] = response;
  98. this.state = {...this.state, teams: newTeams};
  99. this.trigger(new Set([itemId]));
  100. },
  101. onRemoveSuccess(slug: string) {
  102. const teams = this.state.teams.filter(team => team.slug !== slug);
  103. this.setTeams(teams);
  104. ProjectsStore.onDeleteTeam(slug);
  105. },
  106. onCreateSuccess(team: Team) {
  107. this.loadInitialData([team]);
  108. },
  109. getState() {
  110. return this.state;
  111. },
  112. getById(id: string) {
  113. const {teams} = this.state;
  114. return teams.find(item => item.id.toString() === id.toString()) || null;
  115. },
  116. getBySlug(slug: string) {
  117. const {teams} = this.state;
  118. return teams.find(item => item.slug === slug) || null;
  119. },
  120. getAll() {
  121. return this.state.teams;
  122. },
  123. updateTeams(teams: Team[]) {
  124. const teamIdMap = this.state.teams.reduce((acc: Record<string, Team>, team: Team) => {
  125. acc[team.id] = team;
  126. return acc;
  127. }, {});
  128. // Replace or insert new user teams
  129. teams.reduce((acc: Record<string, Team>, userTeam: Team) => {
  130. acc[userTeam.id] = userTeam;
  131. return acc;
  132. }, teamIdMap);
  133. return Object.values(teamIdMap).sort((a, b) => a.slug.localeCompare(b.slug));
  134. },
  135. };
  136. const TeamStore = createStore(teamStoreConfig);
  137. export default TeamStore;