projectsStore.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import {createStore} from 'reflux';
  2. import ProjectActions from 'sentry/actions/projectActions';
  3. import TeamActions from 'sentry/actions/teamActions';
  4. import {Project, Team} from 'sentry/types';
  5. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  6. import {CommonStoreDefinition} from './types';
  7. type State = {
  8. loading: boolean;
  9. projects: Project[];
  10. };
  11. type StatsData = Record<string, Project['stats']>;
  12. /**
  13. * Attributes that need typing but aren't part of the external interface,
  14. */
  15. type InternalDefinition = {
  16. itemsById: Record<string, Project>;
  17. loading: boolean;
  18. removeTeamFromProject(teamSlug: string, project: Project): void;
  19. };
  20. interface ProjectsStoreDefinition
  21. extends InternalDefinition,
  22. CommonStoreDefinition<State> {
  23. getAll(): Project[];
  24. getById(id?: string): Project | undefined;
  25. getBySlug(slug?: string): Project | undefined;
  26. init(): void;
  27. isLoading(): boolean;
  28. loadInitialData(projects: Project[]): void;
  29. onAddTeam(team: Team, projectSlug: string): void;
  30. onChangeSlug(prevSlug: string, newSlug: string): void;
  31. onCreateSuccess(project: Project): void;
  32. onDeleteTeam(slug: string): void;
  33. onRemoveTeam(teamSlug: string, projectSlug: string): void;
  34. onStatsLoadSuccess(data: StatsData): void;
  35. onUpdateSuccess(data: Partial<Project>): void;
  36. reset(): void;
  37. }
  38. const storeConfig: ProjectsStoreDefinition = {
  39. itemsById: {},
  40. loading: true,
  41. unsubscribeListeners: [],
  42. init() {
  43. this.reset();
  44. this.unsubscribeListeners.push(
  45. this.listenTo(ProjectActions.addTeamSuccess, this.onAddTeam)
  46. );
  47. this.unsubscribeListeners.push(
  48. this.listenTo(ProjectActions.changeSlug, this.onChangeSlug)
  49. );
  50. this.unsubscribeListeners.push(
  51. this.listenTo(ProjectActions.createSuccess, this.onCreateSuccess)
  52. );
  53. this.unsubscribeListeners.push(
  54. this.listenTo(ProjectActions.loadProjects, this.loadInitialData)
  55. );
  56. this.unsubscribeListeners.push(
  57. this.listenTo(ProjectActions.loadStatsSuccess, this.onStatsLoadSuccess)
  58. );
  59. this.unsubscribeListeners.push(
  60. this.listenTo(ProjectActions.removeTeamSuccess, this.onRemoveTeam)
  61. );
  62. this.unsubscribeListeners.push(this.listenTo(ProjectActions.reset, this.reset));
  63. this.unsubscribeListeners.push(
  64. this.listenTo(ProjectActions.updateSuccess, this.onUpdateSuccess)
  65. );
  66. this.unsubscribeListeners.push(
  67. this.listenTo(TeamActions.removeTeamSuccess, this.onDeleteTeam)
  68. );
  69. },
  70. reset() {
  71. this.itemsById = {};
  72. this.loading = true;
  73. },
  74. loadInitialData(items: Project[]) {
  75. const mapping = items.map(project => [project.id, project] as const);
  76. this.itemsById = Object.fromEntries(mapping);
  77. this.loading = false;
  78. this.trigger(new Set(Object.keys(this.itemsById)));
  79. },
  80. onChangeSlug(prevSlug: string, newSlug: string) {
  81. const prevProject = this.getBySlug(prevSlug);
  82. if (!prevProject) {
  83. return;
  84. }
  85. const newProject = {...prevProject, slug: newSlug};
  86. this.itemsById = {...this.itemsById, [newProject.id]: newProject};
  87. this.trigger(new Set([prevProject.id]));
  88. },
  89. onCreateSuccess(project: Project) {
  90. this.itemsById = {...this.itemsById, [project.id]: project};
  91. this.trigger(new Set([project.id]));
  92. },
  93. onUpdateSuccess(data: Partial<Project>) {
  94. const project = this.getById(data.id);
  95. if (!project) {
  96. return;
  97. }
  98. const newProject = {...project, ...data};
  99. this.itemsById = {...this.itemsById, [project.id]: newProject};
  100. this.trigger(new Set([data.id]));
  101. },
  102. onStatsLoadSuccess(data) {
  103. const entries = Object.entries(data || {}).filter(
  104. ([projectId]) => projectId in this.itemsById
  105. );
  106. // Assign stats into projects
  107. entries.forEach(([projectId, stats]) => {
  108. this.itemsById[projectId].stats = stats;
  109. });
  110. const touchedIds = entries.map(([projectId]) => projectId);
  111. this.trigger(new Set(touchedIds));
  112. },
  113. /**
  114. * Listener for when a team is completely removed
  115. *
  116. * @param teamSlug Team Slug
  117. */
  118. onDeleteTeam(teamSlug: string) {
  119. // Look for team in all projects
  120. const projects = this.getAll().filter(({teams}) =>
  121. teams.find(({slug}) => slug === teamSlug)
  122. );
  123. projects.forEach(project => this.removeTeamFromProject(teamSlug, project));
  124. const affectedProjectIds = projects.map(project => project.id);
  125. this.trigger(new Set(affectedProjectIds));
  126. },
  127. onRemoveTeam(teamSlug: string, projectSlug: string) {
  128. const project = this.getBySlug(projectSlug);
  129. if (!project) {
  130. return;
  131. }
  132. this.removeTeamFromProject(teamSlug, project);
  133. this.trigger(new Set([project.id]));
  134. },
  135. onAddTeam(team: Team, projectSlug: string) {
  136. const project = this.getBySlug(projectSlug);
  137. // Don't do anything if we can't find a project
  138. if (!project) {
  139. return;
  140. }
  141. const newProject = {...project, teams: [...project.teams, team]};
  142. this.itemsById = {...this.itemsById, [project.id]: newProject};
  143. this.trigger(new Set([project.id]));
  144. },
  145. // Internal method, does not trigger
  146. removeTeamFromProject(teamSlug: string, project: Project) {
  147. const newTeams = project.teams.filter(({slug}) => slug !== teamSlug);
  148. const newProject = {...project, teams: newTeams};
  149. this.itemsById = {...this.itemsById, [project.id]: newProject};
  150. },
  151. isLoading() {
  152. return this.loading;
  153. },
  154. getAll() {
  155. return Object.values(this.itemsById).sort((a, b) => a.slug.localeCompare(b.slug));
  156. },
  157. getById(id) {
  158. return this.getAll().find(project => project.id === id);
  159. },
  160. getBySlug(slug) {
  161. return this.getAll().find(project => project.slug === slug);
  162. },
  163. getState() {
  164. return {
  165. projects: this.getAll(),
  166. loading: this.loading,
  167. };
  168. },
  169. };
  170. const ProjectsStore = createStore(makeSafeRefluxStore(storeConfig));
  171. export default ProjectsStore;