memberListStore.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import {createStore} from 'reflux';
  2. import type {StrictStoreDefinition} from 'sentry/stores/types';
  3. import type {User} from 'sentry/types/user';
  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 StrictStoreDefinition<State> {
  13. getAll(): User[];
  14. getById(memberId: string): User | undefined;
  15. init(): void;
  16. loadInitialData(items: User[], hasMore?: boolean | null, cursor?: string | null): void;
  17. reset(): void;
  18. }
  19. const storeConfig: MemberListStoreDefinition = {
  20. state: {
  21. members: [],
  22. loading: true,
  23. hasMore: null,
  24. cursor: null,
  25. },
  26. init() {
  27. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  28. // listeners due to their leaky nature in tests.
  29. this.reset();
  30. },
  31. reset() {
  32. this.state = {
  33. members: [],
  34. loading: true,
  35. hasMore: null,
  36. cursor: null,
  37. };
  38. },
  39. loadInitialData(items: User[], hasMore, cursor) {
  40. this.state = {
  41. members: items,
  42. loading: false,
  43. hasMore: hasMore ?? this.state.hasMore,
  44. cursor: cursor ?? this.state.cursor,
  45. };
  46. this.trigger(this.state, 'initial');
  47. },
  48. getById(memberId) {
  49. return this.state.members.find(({id}) => memberId === id);
  50. },
  51. getAll() {
  52. return this.state.members;
  53. },
  54. getState() {
  55. return this.state;
  56. },
  57. };
  58. const MemberListStore = createStore(storeConfig);
  59. export default MemberListStore;