selectedGroupStore.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import GroupStore from 'sentry/stores/groupStore';
  3. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  4. interface InternalDefinition {
  5. records: Record<string, boolean>;
  6. }
  7. interface SelectedGroupStoreDefinition extends StoreDefinition, InternalDefinition {
  8. add(ids: string[]): void;
  9. allSelected(): boolean;
  10. anySelected(): boolean;
  11. deselectAll(): void;
  12. getSelectedIds(): Set<string>;
  13. init(): void;
  14. isSelected(itemId: string): boolean;
  15. multiSelected(): boolean;
  16. numSelected(): number;
  17. onGroupChange(itemIds: string[]): void;
  18. prune(): void;
  19. toggleSelect(itemId: string): void;
  20. toggleSelectAll(): void;
  21. }
  22. const storeConfig: SelectedGroupStoreDefinition = {
  23. records: {},
  24. unsubscribeListeners: [],
  25. init() {
  26. this.records = {};
  27. this.unsubscribeListeners.push(
  28. this.listenTo(GroupStore, this.onGroupChange, this.onGroupChange)
  29. );
  30. },
  31. onGroupChange(itemIds) {
  32. this.prune();
  33. this.add(itemIds);
  34. this.trigger();
  35. },
  36. add(ids) {
  37. const allSelected = this.allSelected();
  38. ids.forEach(id => {
  39. if (!this.records.hasOwnProperty(id)) {
  40. this.records[id] = allSelected;
  41. }
  42. });
  43. },
  44. prune() {
  45. const existingIds = new Set(GroupStore.getAllItemIds());
  46. // Remove ids that no longer exist
  47. for (const itemId in this.records) {
  48. if (!existingIds.has(itemId)) {
  49. delete this.records[itemId];
  50. }
  51. }
  52. },
  53. allSelected() {
  54. const itemIds = this.getSelectedIds();
  55. const numRecords = this.numSelected();
  56. return itemIds.size > 0 && itemIds.size === numRecords;
  57. },
  58. numSelected() {
  59. return Object.keys(this.records).length;
  60. },
  61. anySelected() {
  62. const itemIds = this.getSelectedIds();
  63. return itemIds.size > 0;
  64. },
  65. multiSelected() {
  66. const itemIds = this.getSelectedIds();
  67. return itemIds.size > 1;
  68. },
  69. getSelectedIds() {
  70. const selected = new Set<string>();
  71. for (const itemId in this.records) {
  72. if (this.records[itemId]) {
  73. selected.add(itemId);
  74. }
  75. }
  76. return selected;
  77. },
  78. isSelected(itemId) {
  79. return this.records[itemId] === true;
  80. },
  81. deselectAll() {
  82. for (const itemId in this.records) {
  83. this.records[itemId] = false;
  84. }
  85. this.trigger();
  86. },
  87. toggleSelect(itemId) {
  88. if (!this.records.hasOwnProperty(itemId)) {
  89. return;
  90. }
  91. this.records[itemId] = !this.records[itemId];
  92. this.trigger();
  93. },
  94. toggleSelectAll() {
  95. const allSelected = !this.allSelected();
  96. for (const itemId in this.records) {
  97. this.records[itemId] = allSelected;
  98. }
  99. this.trigger();
  100. },
  101. };
  102. const SelectedGroupStore = createStore(makeSafeRefluxStore(storeConfig));
  103. export default SelectedGroupStore;