projectsStore.tsx 5.6 KB

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