selectedGroupStore.tsx 2.6 KB

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