projectsStore.tsx 5.1 KB

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