committerStore.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. this.reset();
  39. },
  40. reset() {
  41. this.state = {};
  42. this.trigger(this.state);
  43. },
  44. load(orgSlug: string, projectSlug: string, eventId: string) {
  45. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  46. this.state = {
  47. ...this.state,
  48. [key]: {
  49. committers: undefined,
  50. committersLoading: true,
  51. committersError: undefined,
  52. },
  53. };
  54. this.trigger(this.state);
  55. },
  56. loadError(orgSlug: string, projectSlug: string, eventId: string, err: Error) {
  57. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  58. this.state = {
  59. ...this.state,
  60. [key]: {
  61. committers: undefined,
  62. committersLoading: false,
  63. committersError: err,
  64. },
  65. };
  66. this.trigger(this.state);
  67. },
  68. loadSuccess(
  69. orgSlug: string,
  70. projectSlug: string,
  71. eventId: string,
  72. committers: Committer[],
  73. releaseCommitters?: ReleaseCommitter[]
  74. ) {
  75. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  76. this.state = {
  77. ...this.state,
  78. [key]: {
  79. committers,
  80. releaseCommitters,
  81. committersLoading: false,
  82. committersError: undefined,
  83. },
  84. };
  85. this.trigger(this.state);
  86. },
  87. get(orgSlug: string, projectSlug: string, eventId: string) {
  88. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  89. return {...this.state[key]};
  90. },
  91. getState() {
  92. return this.state;
  93. },
  94. };
  95. export function getCommitterStoreKey(
  96. orgSlug: string,
  97. projectSlug: string,
  98. eventId: string
  99. ): string {
  100. return `${orgSlug} ${projectSlug} ${eventId}`;
  101. }
  102. const CommitterStore = createStore(storeConfig);
  103. export default CommitterStore;