Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

reducer.js 3.2KB

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