2
0

dueCards.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { BlazeComponent } from 'meteor/peerlibrary:blaze-components';
  3. // const subManager = new SubsManager();
  4. BlazeComponent.extendComponent({
  5. dueCardsView() {
  6. // eslint-disable-next-line no-console
  7. // console.log('sort:', Utils.dueCardsView());
  8. return Utils && Utils.dueCardsView ? Utils.dueCardsView() : 'me';
  9. },
  10. events() {
  11. return [
  12. {
  13. 'click .js-due-cards-view-change': Popup.open('dueCardsViewChange'),
  14. },
  15. ];
  16. },
  17. }).register('dueCardsHeaderBar');
  18. Template.dueCards.helpers({
  19. userId() {
  20. return Meteor.userId();
  21. },
  22. dueCardsList() {
  23. const component = BlazeComponent.getComponentForElement(this);
  24. if (component && component.dueCardsList) {
  25. return component.dueCardsList();
  26. }
  27. return [];
  28. },
  29. hasResults() {
  30. const component = BlazeComponent.getComponentForElement(this);
  31. if (component && component.dueCardsList) {
  32. const cards = component.dueCardsList();
  33. return cards && cards.length > 0;
  34. }
  35. return false;
  36. },
  37. searching() {
  38. return false; // No longer using search, so always false
  39. },
  40. hasQueryErrors() {
  41. return false; // No longer using search, so always false
  42. },
  43. errorMessages() {
  44. return []; // No longer using search, so always empty
  45. },
  46. });
  47. BlazeComponent.extendComponent({
  48. events() {
  49. return [
  50. {
  51. 'click .js-due-cards-view-me'() {
  52. if (Utils && Utils.setDueCardsView) {
  53. Utils.setDueCardsView('me');
  54. }
  55. Popup.back();
  56. },
  57. 'click .js-due-cards-view-all'() {
  58. if (Utils && Utils.setDueCardsView) {
  59. Utils.setDueCardsView('all');
  60. }
  61. Popup.back();
  62. },
  63. },
  64. ];
  65. },
  66. }).register('dueCardsViewChangePopup');
  67. class DueCardsComponent extends BlazeComponent {
  68. onCreated() {
  69. super.onCreated();
  70. this._cachedCards = null;
  71. this._cachedTimestamp = null;
  72. this.subscriptionHandle = null;
  73. // Subscribe to the optimized due cards publication
  74. this.autorun(() => {
  75. const allUsers = this.dueCardsView() === 'all';
  76. if (this.subscriptionHandle) {
  77. this.subscriptionHandle.stop();
  78. }
  79. this.subscriptionHandle = Meteor.subscribe('dueCards', allUsers);
  80. });
  81. }
  82. onDestroyed() {
  83. super.onDestroyed();
  84. if (this.subscriptionHandle) {
  85. this.subscriptionHandle.stop();
  86. }
  87. }
  88. dueCardsView() {
  89. // eslint-disable-next-line no-console
  90. //console.log('sort:', Utils.dueCardsView());
  91. return Utils && Utils.dueCardsView ? Utils.dueCardsView() : 'me';
  92. }
  93. sortByBoard() {
  94. return this.dueCardsView() === 'board';
  95. }
  96. dueCardsList() {
  97. // Use cached results if available to avoid expensive re-sorting
  98. if (this._cachedCards && this._cachedTimestamp && (Date.now() - this._cachedTimestamp < 5000)) {
  99. return this._cachedCards;
  100. }
  101. // Get cards directly from the subscription (already sorted by the publication)
  102. const cards = ReactiveCache.getCards({
  103. type: 'cardType-card',
  104. archived: false,
  105. dueAt: { $exists: true, $nin: [null, ''] }
  106. });
  107. // Filter cards based on user view preference
  108. const allUsers = this.dueCardsView() === 'all';
  109. const currentUser = ReactiveCache.getCurrentUser();
  110. let filteredCards = cards;
  111. if (!allUsers && currentUser) {
  112. filteredCards = cards.filter(card => {
  113. return card.members && card.members.includes(currentUser._id) ||
  114. card.assignees && card.assignees.includes(currentUser._id) ||
  115. card.userId === currentUser._id;
  116. });
  117. }
  118. // Cache the results for 5 seconds to avoid re-filtering on every render
  119. this._cachedCards = filteredCards;
  120. this._cachedTimestamp = Date.now();
  121. return filteredCards;
  122. }
  123. }
  124. DueCardsComponent.register('dueCards');