memberListStore.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import type {StoreDefinition} from 'reflux';
  2. import {createStore} from 'reflux';
  3. import type {User} from 'sentry/types';
  4. type State = {
  5. cursor: string | null;
  6. hasMore: boolean | null;
  7. loading: boolean;
  8. members: User[];
  9. };
  10. // XXX(epurkhiser): Either this store is completely wrong, or it is misnamed, a
  11. // `Member` has one `User`, this stores users not members.
  12. interface MemberListStoreDefinition extends StoreDefinition {
  13. getAll(): User[];
  14. getById(memberId: string): User | undefined;
  15. getState(): State;
  16. init(): void;
  17. loadInitialData(items: User[], hasMore?: boolean | null, cursor?: string | null): void;
  18. reset(): void;
  19. state: State;
  20. }
  21. const storeConfig: MemberListStoreDefinition = {
  22. state: {
  23. members: [],
  24. loading: true,
  25. hasMore: null,
  26. cursor: null,
  27. },
  28. init() {
  29. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  30. // listeners due to their leaky nature in tests.
  31. this.reset();
  32. },
  33. reset() {
  34. this.state = {
  35. members: [],
  36. loading: true,
  37. hasMore: null,
  38. cursor: null,
  39. };
  40. },
  41. loadInitialData(items: User[], hasMore, cursor) {
  42. this.state = {
  43. members: items,
  44. loading: false,
  45. hasMore: hasMore ?? this.state.hasMore,
  46. cursor: cursor ?? this.state.cursor,
  47. };
  48. this.trigger(this.state, 'initial');
  49. },
  50. getById(memberId) {
  51. return this.state.members.find(({id}) => memberId === id);
  52. },
  53. getAll() {
  54. return this.state.members;
  55. },
  56. getState() {
  57. return this.state;
  58. },
  59. };
  60. const MemberListStore = createStore(storeConfig);
  61. export default MemberListStore;