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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. list: true
  31. });
  32. /**
  33. * Reduces the redux actions of the feature recent-list.
  34. */
  35. ReducerRegistry.register(
  36. STORE_NAME,
  37. (state = { list: _getLegacyRecentRoomList() }, action) => {
  38. switch (action.type) {
  39. case STORE_CURRENT_CONFERENCE:
  40. return _storeCurrentConference(state, action);
  41. case UPDATE_CONFERENCE_DURATION:
  42. return _updateConferenceDuration(state, action);
  43. default:
  44. return state;
  45. }
  46. });
  47. /**
  48. * Retrieves the recent room list that was stored using the legacy way.
  49. *
  50. * @returns {Array<Object>}
  51. */
  52. export function _getLegacyRecentRoomList(): Array<Object> {
  53. try {
  54. const list
  55. = JSON.parse(window.localStorage.getItem(LEGACY_STORAGE_KEY));
  56. if (list && list.length) {
  57. return list;
  58. }
  59. } catch (error) {
  60. logger.warn('Failed to parse legacy recent-room list!');
  61. }
  62. return [];
  63. }
  64. /**
  65. * Adds a new list entry to the redux store.
  66. *
  67. * @param {Object} state - The redux state.
  68. * @param {Object} action - The redux action.
  69. * @returns {Object}
  70. */
  71. function _storeCurrentConference(state, action) {
  72. const { locationURL } = action;
  73. const conference = locationURL.href;
  74. // If the current conference is already in the list, we remove it to re-add
  75. // it to the top.
  76. const list = state.list.filter(e => e.conference !== conference);
  77. // The list is a reverse-sorted (i.e. the newer elements are at the end).
  78. list.push({
  79. conference,
  80. conferenceDuration: 0, // We don't have this data yet!
  81. date: Date.now()
  82. });
  83. // Ensure the list doesn't exceed a/the maximum size.
  84. list.splice(0, list.length - MAX_LIST_SIZE);
  85. return {
  86. list
  87. };
  88. }
  89. /**
  90. * Updates the conference length when left.
  91. *
  92. * @param {Object} state - The redux state.
  93. * @param {Object} action - The redux action.
  94. * @returns {Object}
  95. */
  96. function _updateConferenceDuration(state, action) {
  97. const { locationURL } = action;
  98. if (locationURL && locationURL.href) {
  99. const list = state.list;
  100. if (list.length > 0) {
  101. const mostRecentURL = list[list.length - 1];
  102. if (mostRecentURL.conference === locationURL.href) {
  103. // The last conference start was stored so we need to update the
  104. // length.
  105. mostRecentURL.conferenceDuration
  106. = Date.now() - mostRecentURL.date;
  107. return {
  108. list
  109. };
  110. }
  111. }
  112. }
  113. return state;
  114. }