projectsStore.tsx 5.3 KB

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