committerStore.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {createStore} from 'reflux';
  2. import type {Committer, ReleaseCommitter} from 'sentry/types';
  3. type State = {
  4. // Use `getCommitterStoreKey` to generate key
  5. [key: string]: {
  6. committers?: Committer[];
  7. committersError?: Error;
  8. committersLoading?: boolean;
  9. releaseCommitters?: ReleaseCommitter[];
  10. };
  11. };
  12. interface CommitterStoreDefinition extends Reflux.StoreDefinition {
  13. get(
  14. orgSlug: string,
  15. projectSlug: string,
  16. eventId: string
  17. ): {
  18. committers?: Committer[];
  19. committersError?: Error;
  20. committersLoading?: boolean;
  21. };
  22. getState(): State;
  23. init(): void;
  24. load(orgSlug: string, projectSlug: string, eventId: string): void;
  25. loadError(orgSlug: string, projectSlug: string, eventId: string, error: Error): void;
  26. loadSuccess(
  27. orgSlug: string,
  28. projectSlug: string,
  29. eventId: string,
  30. committers: Committer[],
  31. releaseCommitters?: ReleaseCommitter[]
  32. ): void;
  33. state: State;
  34. }
  35. export const storeConfig: CommitterStoreDefinition = {
  36. state: {},
  37. init() {
  38. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  39. // listeners due to their leaky nature in tests.
  40. this.reset();
  41. },
  42. reset() {
  43. this.state = {};
  44. this.trigger(this.state);
  45. },
  46. load(orgSlug: string, projectSlug: string, eventId: string) {
  47. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  48. this.state = {
  49. ...this.state,
  50. [key]: {
  51. committers: undefined,
  52. committersLoading: true,
  53. committersError: undefined,
  54. },
  55. };
  56. this.trigger(this.state);
  57. },
  58. loadError(orgSlug: string, projectSlug: string, eventId: string, err: Error) {
  59. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  60. this.state = {
  61. ...this.state,
  62. [key]: {
  63. committers: undefined,
  64. committersLoading: false,
  65. committersError: err,
  66. },
  67. };
  68. this.trigger(this.state);
  69. },
  70. loadSuccess(
  71. orgSlug: string,
  72. projectSlug: string,
  73. eventId: string,
  74. committers: Committer[],
  75. releaseCommitters?: ReleaseCommitter[]
  76. ) {
  77. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  78. this.state = {
  79. ...this.state,
  80. [key]: {
  81. committers,
  82. releaseCommitters,
  83. committersLoading: false,
  84. committersError: undefined,
  85. },
  86. };
  87. this.trigger(this.state);
  88. },
  89. get(orgSlug: string, projectSlug: string, eventId: string) {
  90. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  91. return {...this.state[key]};
  92. },
  93. getState() {
  94. return this.state;
  95. },
  96. };
  97. export function getCommitterStoreKey(
  98. orgSlug: string,
  99. projectSlug: string,
  100. eventId: string
  101. ): string {
  102. return `${orgSlug} ${projectSlug} ${eventId}`;
  103. }
  104. const CommitterStore = createStore(storeConfig);
  105. export default CommitterStore;