You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reducer.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // @flow
  2. import { ReducerRegistry } from '../base/redux';
  3. import { PersistenceRegistry } from '../base/storage';
  4. import {
  5. STORE_CURRENT_CONFERENCE,
  6. UPDATE_CONFERENCE_DURATION
  7. } from './actionTypes';
  8. const logger = require('jitsi-meet-logger').getLogger(__filename);
  9. /**
  10. * The name of the {@code window.localStorage} item where recent rooms are
  11. * stored.
  12. *
  13. * @type {string}
  14. */
  15. const LEGACY_STORAGE_KEY = 'recentURLs';
  16. /**
  17. * The max size of the list.
  18. *
  19. * @type {number}
  20. */
  21. export const MAX_LIST_SIZE = 30;
  22. /**
  23. * The redux subtree of this feature.
  24. */
  25. const STORE_NAME = 'features/recent-list';
  26. /**
  27. * Sets up the persistence of the feature recent-list.
  28. */
  29. PersistenceRegistry.register(STORE_NAME);
  30. /**
  31. * Reduces the redux actions of the feature recent-list.
  32. */
  33. ReducerRegistry.register(
  34. STORE_NAME,
  35. (state = _getLegacyRecentRoomList(), action) => {
  36. switch (action.type) {
  37. case STORE_CURRENT_CONFERENCE:
  38. return _storeCurrentConference(state, action);
  39. case UPDATE_CONFERENCE_DURATION:
  40. return _updateConferenceDuration(state, action);
  41. default:
  42. return state;
  43. }
  44. });
  45. /**
  46. * Retrieves the recent room list that was stored using the legacy way.
  47. *
  48. * @returns {Array<Object>}
  49. */
  50. export function _getLegacyRecentRoomList(): Array<Object> {
  51. try {
  52. const list
  53. = JSON.parse(window.localStorage.getItem(LEGACY_STORAGE_KEY));
  54. if (list && list.length) {
  55. return list;
  56. }
  57. } catch (error) {
  58. logger.warn('Failed to parse legacy recent-room list!');
  59. }
  60. return [];
  61. }
  62. /**
  63. * Adds a new list entry to the redux store.
  64. *
  65. * @param {Object} state - The redux state.
  66. * @param {Object} action - The redux action.
  67. * @returns {Object}
  68. */
  69. function _storeCurrentConference(state, action) {
  70. const { locationURL } = action;
  71. const conference = locationURL.href;
  72. // If the current conference is already in the list, we remove it to re-add
  73. // it to the top.
  74. const list = (Array.isArray(state) ? state : [])
  75. .filter(e => e.conference !== conference);
  76. // The list is a reverse-sorted (i.e. the newer elements are at the end).
  77. list.push({
  78. conference,
  79. conferenceDuration: 0, // We don't have this data yet!
  80. date: Date.now()
  81. });
  82. // Ensure the list doesn't exceed a/the maximum size.
  83. list.splice(0, list.length - MAX_LIST_SIZE);
  84. return list;
  85. }
  86. /**
  87. * Updates the conference length when left.
  88. *
  89. * @param {Object} state - The redux state.
  90. * @param {Object} action - The redux action.
  91. * @returns {Object}
  92. */
  93. function _updateConferenceDuration(state, action) {
  94. const { locationURL } = action;
  95. if (locationURL && locationURL.href) {
  96. // shallow copy to avoid in-place modification.
  97. const list = (Array.isArray(state) ? state : []).slice();
  98. if (list.length > 0) {
  99. const mostRecentURL = list[list.length - 1];
  100. if (mostRecentURL.conference === locationURL.href) {
  101. // The last conference start was stored so we need to update the
  102. // length.
  103. mostRecentURL.conferenceDuration
  104. = Date.now() - mostRecentURL.date;
  105. return list;
  106. }
  107. }
  108. }
  109. return state;
  110. }