projectsStore.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import {createStore} from 'reflux';
  2. import {fetchOrganizationDetails} from 'sentry/actionCreators/organization';
  3. import {Client} from 'sentry/api';
  4. import type {Project, Team} from 'sentry/types';
  5. import LatestContextStore from './latestContextStore';
  6. import type {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. api: Client;
  17. loading: boolean;
  18. projects: Project[];
  19. removeTeamFromProject(teamSlug: string, project: Project): void;
  20. };
  21. interface ProjectsStoreDefinition
  22. extends InternalDefinition,
  23. CommonStoreDefinition<State> {
  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, orgSlug: string): 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. api: new Client(),
  40. projects: [],
  41. loading: true,
  42. init() {
  43. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  44. // listeners due to their leaky nature in tests.
  45. this.reset();
  46. },
  47. reset() {
  48. this.projects = [];
  49. this.itemsById = {};
  50. this.loading = true;
  51. },
  52. loadInitialData(items: Project[]) {
  53. this.projects = items.toSorted((a, b) => a.slug.localeCompare(b.slug));
  54. this.loading = false;
  55. this.trigger(new Set(items.map(x => x.id)));
  56. },
  57. onChangeSlug(prevSlug: string, newSlug: string) {
  58. const prevProject = this.getBySlug(prevSlug);
  59. if (!prevProject) {
  60. return;
  61. }
  62. const newProject = {...prevProject, slug: newSlug};
  63. this.projects = this.projects
  64. .map(project => (project.slug === prevSlug ? newProject : project))
  65. .sort((a, b) => a.slug.localeCompare(b.slug));
  66. this.trigger(new Set([prevProject.id]));
  67. },
  68. onCreateSuccess(project: Project, orgSlug: string) {
  69. this.projects = this.projects
  70. .concat([project])
  71. .sort((a, b) => a.slug.localeCompare(b.slug));
  72. // Reload organization details since we've created a new project
  73. fetchOrganizationDetails(this.api, orgSlug, true, false);
  74. this.trigger(new Set([project.id]));
  75. },
  76. onUpdateSuccess(data: Partial<Project>) {
  77. const project = this.getById(data.id);
  78. if (!project) {
  79. return;
  80. }
  81. const newProject = {...project, ...data};
  82. this.projects = this.projects.map(p => (p.id === project.id ? newProject : p));
  83. this.trigger(new Set([data.id]));
  84. LatestContextStore.onUpdateProject(newProject);
  85. },
  86. onStatsLoadSuccess(data) {
  87. const statsData = data || {};
  88. // Assign stats into projects
  89. this.projects = this.projects.map(project =>
  90. statsData[project.id] ? {...project, stats: data[project.id]} : project
  91. );
  92. this.trigger(new Set(Object.keys(data)));
  93. },
  94. /**
  95. * Listener for when a team is completely removed
  96. *
  97. * @param teamSlug Team Slug
  98. */
  99. onDeleteTeam(teamSlug: string) {
  100. // Look for team in all projects
  101. const projects = this.projects.filter(({teams}) =>
  102. teams.find(({slug}) => slug === teamSlug)
  103. );
  104. projects.forEach(project => this.removeTeamFromProject(teamSlug, project));
  105. const affectedProjectIds = projects.map(project => project.id);
  106. this.trigger(new Set(affectedProjectIds));
  107. },
  108. onRemoveTeam(teamSlug: string, projectSlug: string) {
  109. const project = this.getBySlug(projectSlug);
  110. if (!project) {
  111. return;
  112. }
  113. this.removeTeamFromProject(teamSlug, project);
  114. this.trigger(new Set([project.id]));
  115. },
  116. onAddTeam(team: Team, projectSlug: string) {
  117. const project = this.getBySlug(projectSlug);
  118. // Don't do anything if we can't find a project
  119. if (!project) {
  120. return;
  121. }
  122. const newProject = {...project, teams: [...project.teams, team]};
  123. this.projects = this.projects.map(p => (p.id === project.id ? newProject : p));
  124. this.trigger(new Set([project.id]));
  125. },
  126. // Internal method, does not trigger
  127. removeTeamFromProject(teamSlug: string, project: Project) {
  128. const newTeams = project.teams.filter(({slug}) => slug !== teamSlug);
  129. const newProject = {...project, teams: newTeams};
  130. this.projects = this.projects.map(p => (p.id === project.id ? newProject : p));
  131. },
  132. isLoading() {
  133. return this.loading;
  134. },
  135. getById(id) {
  136. return this.projects.find(project => project.id === id);
  137. },
  138. getBySlug(slug) {
  139. return this.projects.find(project => project.slug === slug);
  140. },
  141. getState() {
  142. return {
  143. projects: this.projects,
  144. loading: this.loading,
  145. };
  146. },
  147. };
  148. const ProjectsStore = createStore(storeConfig);
  149. export default ProjectsStore;