teamStore.tsx 4.2 KB

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