projectsStatsStore.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import ProjectActions from 'sentry/actions/projectActions';
  3. import {Project} from 'sentry/types';
  4. import {
  5. makeSafeRefluxStore,
  6. SafeRefluxStore,
  7. SafeStoreDefinition,
  8. } from 'sentry/utils/makeSafeRefluxStore';
  9. type ProjectsStatsStoreInterface = {
  10. getAll(): ProjectsStatsStoreInterface['itemsBySlug'];
  11. getBySlug(slug: string): Project;
  12. getInitialState(): ProjectsStatsStoreInterface['itemsBySlug'];
  13. itemsBySlug: Record<string, Project>;
  14. reset(): void;
  15. };
  16. /**
  17. * This is a store specifically used by the dashboard, so that we can
  18. * clear the store when the Dashboard unmounts
  19. * (as to not disrupt ProjectsStore which a lot more components use)
  20. */
  21. const storeConfig: StoreDefinition & ProjectsStatsStoreInterface & SafeStoreDefinition = {
  22. itemsBySlug: {},
  23. unsubscribeListeners: [],
  24. init() {
  25. this.reset();
  26. this.unsubscribeListeners.push(
  27. this.listenTo(ProjectActions.loadStatsForProjectSuccess, this.onStatsLoadSuccess)
  28. );
  29. this.unsubscribeListeners.push(this.listenTo(ProjectActions.update, this.onUpdate));
  30. this.unsubscribeListeners.push(
  31. this.listenTo(ProjectActions.updateError, this.onUpdateError)
  32. );
  33. },
  34. getInitialState() {
  35. return this.itemsBySlug;
  36. },
  37. reset() {
  38. this.itemsBySlug = {};
  39. this.updatingItems = new Map();
  40. },
  41. onStatsLoadSuccess(projects: Project[]) {
  42. projects.forEach(project => {
  43. this.itemsBySlug[project.slug] = project;
  44. });
  45. this.trigger(this.itemsBySlug);
  46. },
  47. /**
  48. * Optimistic updates
  49. * @param projectSlug Project slug
  50. * @param data Project data
  51. */
  52. onUpdate(projectSlug: string, data: Project) {
  53. const project = this.getBySlug(projectSlug);
  54. this.updatingItems.set(projectSlug, project);
  55. if (!project) {
  56. return;
  57. }
  58. const newProject: Project = {
  59. ...project,
  60. ...data,
  61. };
  62. this.itemsBySlug = {
  63. ...this.itemsBySlug,
  64. [project.slug]: newProject,
  65. };
  66. this.trigger(this.itemsBySlug);
  67. },
  68. onUpdateSuccess(data: Project) {
  69. // Remove project from updating map
  70. this.updatingItems.delete(data.slug);
  71. },
  72. /**
  73. * Revert project data when there was an error updating project details
  74. * @param err Error object
  75. * @param data Previous project data
  76. */
  77. onUpdateError(_err: Error, projectSlug: string) {
  78. const project = this.updatingItems.get(projectSlug);
  79. if (!project) {
  80. return;
  81. }
  82. this.updatingItems.delete(projectSlug);
  83. // Restore old project
  84. this.itemsBySlug = {
  85. ...this.itemsBySlug,
  86. [project.slug]: {...project},
  87. };
  88. this.trigger(this.itemsBySlug);
  89. },
  90. getAll() {
  91. return this.itemsBySlug;
  92. },
  93. getBySlug(slug) {
  94. return this.itemsBySlug[slug];
  95. },
  96. };
  97. const ProjectsStatsStore = createStore(
  98. makeSafeRefluxStore(storeConfig)
  99. ) as SafeRefluxStore & ProjectsStatsStoreInterface;
  100. export default ProjectsStatsStore;