committerStore.tsx 2.8 KB

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