selectedGroupStore.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {createStore} from 'reflux';
  2. import GroupStore from 'sentry/stores/groupStore';
  3. import {CommonStoreDefinition} from './types';
  4. interface InternalDefinition {
  5. /**
  6. * The last item to have been selected
  7. */
  8. lastSelected: string | null;
  9. /**
  10. * Mapping of item ID -> if it is selected. This is a map to
  11. * make it easier to track if everything has been selected or not.
  12. */
  13. records: Map<string, boolean>;
  14. }
  15. interface SelectedGroupStoreDefinition
  16. extends CommonStoreDefinition<Record<string, boolean>>,
  17. InternalDefinition {
  18. add(ids: string[]): void;
  19. allSelected(): boolean;
  20. anySelected(): boolean;
  21. deselectAll(): void;
  22. getSelectedIds(): Set<string>;
  23. init(): void;
  24. isSelected(itemId: string): boolean;
  25. multiSelected(): boolean;
  26. onGroupChange(itemIds: Set<string>): void;
  27. prune(): void;
  28. reset(): void;
  29. shiftToggleItems(itemId: string): void;
  30. toggleSelect(itemId: string): void;
  31. toggleSelectAll(): void;
  32. }
  33. const storeConfig: SelectedGroupStoreDefinition = {
  34. records: new Map(),
  35. lastSelected: null,
  36. unsubscribeListeners: [],
  37. init() {
  38. this.reset();
  39. },
  40. reset() {
  41. this.records = new Map();
  42. this.lastSelected = null;
  43. },
  44. getState() {
  45. return Object.fromEntries(this.records);
  46. },
  47. onGroupChange(itemIds) {
  48. this.prune();
  49. this.add([...itemIds]);
  50. this.trigger();
  51. },
  52. add(ids) {
  53. const allSelected = this.allSelected();
  54. ids
  55. .filter(id => !this.records.has(id))
  56. .forEach(id => this.records.set(id, allSelected));
  57. },
  58. prune() {
  59. const existingIds = new Set(GroupStore.getAllItemIds());
  60. this.lastSelected = null;
  61. // Remove everything that no longer exists
  62. [...this.records.keys()]
  63. .filter(id => !existingIds.has(id))
  64. .forEach(id => this.records.delete(id));
  65. },
  66. allSelected() {
  67. const itemIds = this.getSelectedIds();
  68. return itemIds.size > 0 && itemIds.size === this.records.size;
  69. },
  70. numSelected() {
  71. return this.getSelectedIds().size;
  72. },
  73. anySelected() {
  74. return this.getSelectedIds().size > 0;
  75. },
  76. multiSelected() {
  77. return this.getSelectedIds().size > 1;
  78. },
  79. getSelectedIds() {
  80. return new Set([...this.records.keys()].filter(id => this.records.get(id)));
  81. },
  82. isSelected(itemId) {
  83. return !!this.records.get(itemId);
  84. },
  85. deselectAll() {
  86. this.records.forEach((_, id) => this.records.set(id, false));
  87. this.trigger();
  88. },
  89. toggleSelect(itemId) {
  90. if (!this.records.has(itemId)) {
  91. return;
  92. }
  93. const newState = !this.records.get(itemId);
  94. this.records.set(itemId, newState);
  95. this.lastSelected = itemId;
  96. this.trigger();
  97. },
  98. toggleSelectAll() {
  99. const allSelected = !this.allSelected();
  100. this.lastSelected = null;
  101. this.records.forEach((_, id) => this.records.set(id, allSelected));
  102. this.trigger();
  103. },
  104. shiftToggleItems(itemId) {
  105. if (!this.records.has(itemId)) {
  106. return;
  107. }
  108. if (!this.lastSelected) {
  109. this.toggleSelect(itemId);
  110. return;
  111. }
  112. const ids = GroupStore.getAllItemIds();
  113. const lastIdx = ids.findIndex(id => id === this.lastSelected);
  114. const currentIdx = ids.findIndex(id => id === itemId);
  115. if (lastIdx === -1 || currentIdx === -1) {
  116. return;
  117. }
  118. const newValue = !this.records.get(itemId);
  119. const selected =
  120. lastIdx < currentIdx
  121. ? ids.slice(lastIdx, currentIdx)
  122. : ids.slice(currentIdx, lastIdx);
  123. [...selected, this.lastSelected, itemId]
  124. .filter(id => this.records.has(id))
  125. .forEach(id => this.records.set(id, newValue));
  126. this.lastSelected = itemId;
  127. this.trigger();
  128. },
  129. };
  130. const SelectedGroupStore = createStore(storeConfig);
  131. export default SelectedGroupStore;