committerStore.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {createStore} from 'reflux';
  2. import CommitterActions from 'sentry/actions/committerActions';
  3. import {Committer} from 'sentry/types';
  4. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  5. type State = {
  6. // Use `getCommitterStoreKey` to generate key
  7. [key: string]: {
  8. committers?: Committer[];
  9. committersError?: Error;
  10. committersLoading?: boolean;
  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. data: Committer[]
  32. ): void;
  33. state: State;
  34. }
  35. export const storeConfig: CommitterStoreDefinition = {
  36. listenables: CommitterActions,
  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(orgSlug: string, projectSlug: string, eventId: string, data: Committer[]) {
  70. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  71. this.state = {
  72. ...this.state,
  73. [key]: {
  74. committers: data,
  75. committersLoading: false,
  76. committersError: undefined,
  77. },
  78. };
  79. this.trigger(this.state);
  80. },
  81. get(orgSlug: string, projectSlug: string, eventId: string) {
  82. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  83. return {...this.state[key]};
  84. },
  85. getState() {
  86. return this.state;
  87. },
  88. };
  89. export function getCommitterStoreKey(
  90. orgSlug: string,
  91. projectSlug: string,
  92. eventId: string
  93. ): string {
  94. return `${orgSlug} ${projectSlug} ${eventId}`;
  95. }
  96. const CommitterStore = createStore(makeSafeRefluxStore(storeConfig));
  97. export default CommitterStore;