projectsStore.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import {createStore} from 'reflux';
  2. import {fetchOrganizationDetails} from 'sentry/actionCreators/organization';
  3. import {Client} from 'sentry/api';
  4. import {Project, Team} from 'sentry/types';
  5. import LatestContextStore from './latestContextStore';
  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. api: Client;
  17. itemsById: Record<string, Project>;
  18. loading: boolean;
  19. removeTeamFromProject(teamSlug: string, project: Project): void;
  20. };
  21. interface ProjectsStoreDefinition
  22. extends InternalDefinition,
  23. CommonStoreDefinition<State> {
  24. getAll(): Project[];
  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. itemsById: {},
  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.itemsById = {};
  50. this.loading = true;
  51. },
  52. loadInitialData(items: Project[]) {
  53. const mapping = items.map(project => [project.id, project] as const);
  54. this.itemsById = Object.fromEntries(mapping);
  55. this.loading = false;
  56. this.trigger(new Set(Object.keys(this.itemsById)));
  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.itemsById = {...this.itemsById, [newProject.id]: newProject};
  65. this.trigger(new Set([prevProject.id]));
  66. },
  67. onCreateSuccess(project: Project, orgSlug: string) {
  68. this.itemsById = {...this.itemsById, [project.id]: project};
  69. // Reload organization details since we've created a new project
  70. fetchOrganizationDetails(this.api, orgSlug, true, false);
  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. LatestContextStore.onUpdateProject(newProject);
  82. },
  83. onStatsLoadSuccess(data) {
  84. const entries = Object.entries(data || {}).filter(
  85. ([projectId]) => projectId in this.itemsById
  86. );
  87. // Assign stats into projects
  88. entries.forEach(([projectId, stats]) => {
  89. this.itemsById[projectId].stats = stats;
  90. });
  91. const touchedIds = entries.map(([projectId]) => projectId);
  92. this.trigger(new Set(touchedIds));
  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.getAll().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.itemsById = {...this.itemsById, [project.id]: newProject};
  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.itemsById = {...this.itemsById, [project.id]: newProject};
  131. },
  132. isLoading() {
  133. return this.loading;
  134. },
  135. getAll() {
  136. return Object.values(this.itemsById).sort((a, b) => a.slug.localeCompare(b.slug));
  137. },
  138. getById(id) {
  139. return this.getAll().find(project => project.id === id);
  140. },
  141. getBySlug(slug) {
  142. return this.getAll().find(project => project.slug === slug);
  143. },
  144. getState() {
  145. return {
  146. projects: this.getAll(),
  147. loading: this.loading,
  148. };
  149. },
  150. };
  151. const ProjectsStore = createStore(storeConfig);
  152. export default ProjectsStore;