Reducers.jsm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const {actionTypes: at} = ChromeUtils.import("resource://activity-stream/common/Actions.jsm");
  6. const {Dedupe} = ChromeUtils.import("resource://activity-stream/common/Dedupe.jsm");
  7. const TOP_SITES_DEFAULT_ROWS = 1;
  8. const TOP_SITES_MAX_SITES_PER_ROW = 8;
  9. const dedupe = new Dedupe(site => site && site.url);
  10. const INITIAL_STATE = {
  11. App: {
  12. // Have we received real data from the app yet?
  13. initialized: false,
  14. },
  15. ASRouter: {initialized: false},
  16. Snippets: {initialized: false},
  17. TopSites: {
  18. // Have we received real data from history yet?
  19. initialized: false,
  20. // The history (and possibly default) links
  21. rows: [],
  22. // Used in content only to dispatch action to TopSiteForm.
  23. editForm: null,
  24. // Used in content only to open the SearchShortcutsForm modal.
  25. showSearchShortcutsForm: false,
  26. // The list of available search shortcuts.
  27. searchShortcuts: [],
  28. },
  29. Prefs: {
  30. initialized: false,
  31. values: {},
  32. },
  33. Dialog: {
  34. visible: false,
  35. data: {},
  36. },
  37. Sections: [],
  38. Pocket: {
  39. isUserLoggedIn: null,
  40. pocketCta: {},
  41. waitingForSpoc: true,
  42. },
  43. // This is the new pocket configurable layout state.
  44. DiscoveryStream: {
  45. // This is a JSON-parsed copy of the discoverystream.config pref value.
  46. config: {enabled: false, layout_endpoint: ""},
  47. layout: [],
  48. lastUpdated: null,
  49. feeds: {
  50. data: {
  51. // "https://foo.com/feed1": {lastUpdated: 123, data: []}
  52. },
  53. loaded: false,
  54. },
  55. spocs: {
  56. spocs_endpoint: "",
  57. lastUpdated: null,
  58. data: {}, // {spocs: []}
  59. loaded: false,
  60. frequency_caps: [],
  61. },
  62. },
  63. Search: {
  64. // When search hand-off is enabled, we render a big button that is styled to
  65. // look like a search textbox. If the button is clicked, we style
  66. // the button as if it was a focused search box and show a fake cursor but
  67. // really focus the awesomebar without the focus styles ("hidden focus").
  68. fakeFocus: false,
  69. // Hide the search box after handing off to AwesomeBar and user starts typing.
  70. hide: false,
  71. },
  72. };
  73. function App(prevState = INITIAL_STATE.App, action) {
  74. switch (action.type) {
  75. case at.INIT:
  76. return Object.assign({}, prevState, action.data || {}, {initialized: true});
  77. default:
  78. return prevState;
  79. }
  80. }
  81. function ASRouter(prevState = INITIAL_STATE.ASRouter, action) {
  82. switch (action.type) {
  83. case at.AS_ROUTER_INITIALIZED:
  84. return {...action.data, initialized: true};
  85. default:
  86. return prevState;
  87. }
  88. }
  89. /**
  90. * insertPinned - Inserts pinned links in their specified slots
  91. *
  92. * @param {array} a list of links
  93. * @param {array} a list of pinned links
  94. * @return {array} resulting list of links with pinned links inserted
  95. */
  96. function insertPinned(links, pinned) {
  97. // Remove any pinned links
  98. const pinnedUrls = pinned.map(link => link && link.url);
  99. let newLinks = links.filter(link => (link ? !pinnedUrls.includes(link.url) : false));
  100. newLinks = newLinks.map(link => {
  101. if (link && link.isPinned) {
  102. delete link.isPinned;
  103. delete link.pinIndex;
  104. }
  105. return link;
  106. });
  107. // Then insert them in their specified location
  108. pinned.forEach((val, index) => {
  109. if (!val) { return; }
  110. let link = Object.assign({}, val, {isPinned: true, pinIndex: index});
  111. if (index > newLinks.length) {
  112. newLinks[index] = link;
  113. } else {
  114. newLinks.splice(index, 0, link);
  115. }
  116. });
  117. return newLinks;
  118. }
  119. function TopSites(prevState = INITIAL_STATE.TopSites, action) {
  120. let hasMatch;
  121. let newRows;
  122. switch (action.type) {
  123. case at.TOP_SITES_UPDATED:
  124. if (!action.data || !action.data.links) {
  125. return prevState;
  126. }
  127. return Object.assign({}, prevState, {initialized: true, rows: action.data.links}, action.data.pref ? {pref: action.data.pref} : {});
  128. case at.TOP_SITES_PREFS_UPDATED:
  129. return Object.assign({}, prevState, {pref: action.data.pref});
  130. case at.TOP_SITES_EDIT:
  131. return Object.assign({}, prevState, {
  132. editForm: {
  133. index: action.data.index,
  134. previewResponse: null,
  135. },
  136. });
  137. case at.TOP_SITES_CANCEL_EDIT:
  138. return Object.assign({}, prevState, {editForm: null});
  139. case at.TOP_SITES_OPEN_SEARCH_SHORTCUTS_MODAL:
  140. return Object.assign({}, prevState, {showSearchShortcutsForm: true});
  141. case at.TOP_SITES_CLOSE_SEARCH_SHORTCUTS_MODAL:
  142. return Object.assign({}, prevState, {showSearchShortcutsForm: false});
  143. case at.PREVIEW_RESPONSE:
  144. if (!prevState.editForm || action.data.url !== prevState.editForm.previewUrl) {
  145. return prevState;
  146. }
  147. return Object.assign({}, prevState, {
  148. editForm: {
  149. index: prevState.editForm.index,
  150. previewResponse: action.data.preview,
  151. previewUrl: action.data.url,
  152. },
  153. });
  154. case at.PREVIEW_REQUEST:
  155. if (!prevState.editForm) {
  156. return prevState;
  157. }
  158. return Object.assign({}, prevState, {
  159. editForm: {
  160. index: prevState.editForm.index,
  161. previewResponse: null,
  162. previewUrl: action.data.url,
  163. },
  164. });
  165. case at.PREVIEW_REQUEST_CANCEL:
  166. if (!prevState.editForm) {
  167. return prevState;
  168. }
  169. return Object.assign({}, prevState, {
  170. editForm: {
  171. index: prevState.editForm.index,
  172. previewResponse: null,
  173. },
  174. });
  175. case at.SCREENSHOT_UPDATED:
  176. newRows = prevState.rows.map(row => {
  177. if (row && row.url === action.data.url) {
  178. hasMatch = true;
  179. return Object.assign({}, row, {screenshot: action.data.screenshot});
  180. }
  181. return row;
  182. });
  183. return hasMatch ? Object.assign({}, prevState, {rows: newRows}) : prevState;
  184. case at.PLACES_BOOKMARK_ADDED:
  185. if (!action.data) {
  186. return prevState;
  187. }
  188. newRows = prevState.rows.map(site => {
  189. if (site && site.url === action.data.url) {
  190. const {bookmarkGuid, bookmarkTitle, dateAdded} = action.data;
  191. return Object.assign({}, site, {bookmarkGuid, bookmarkTitle, bookmarkDateCreated: dateAdded});
  192. }
  193. return site;
  194. });
  195. return Object.assign({}, prevState, {rows: newRows});
  196. case at.PLACES_BOOKMARK_REMOVED:
  197. if (!action.data) {
  198. return prevState;
  199. }
  200. newRows = prevState.rows.map(site => {
  201. if (site && site.url === action.data.url) {
  202. const newSite = Object.assign({}, site);
  203. delete newSite.bookmarkGuid;
  204. delete newSite.bookmarkTitle;
  205. delete newSite.bookmarkDateCreated;
  206. return newSite;
  207. }
  208. return site;
  209. });
  210. return Object.assign({}, prevState, {rows: newRows});
  211. case at.PLACES_LINK_DELETED:
  212. if (!action.data) {
  213. return prevState;
  214. }
  215. newRows = prevState.rows.filter(site => action.data.url !== site.url);
  216. return Object.assign({}, prevState, {rows: newRows});
  217. case at.UPDATE_SEARCH_SHORTCUTS:
  218. return {...prevState, searchShortcuts: action.data.searchShortcuts};
  219. case at.SNIPPETS_PREVIEW_MODE:
  220. return {...prevState, rows: []};
  221. default:
  222. return prevState;
  223. }
  224. }
  225. function Dialog(prevState = INITIAL_STATE.Dialog, action) {
  226. switch (action.type) {
  227. case at.DIALOG_OPEN:
  228. return Object.assign({}, prevState, {visible: true, data: action.data});
  229. case at.DIALOG_CANCEL:
  230. return Object.assign({}, prevState, {visible: false});
  231. case at.DELETE_HISTORY_URL:
  232. return Object.assign({}, INITIAL_STATE.Dialog);
  233. default:
  234. return prevState;
  235. }
  236. }
  237. function Prefs(prevState = INITIAL_STATE.Prefs, action) {
  238. let newValues;
  239. switch (action.type) {
  240. case at.PREFS_INITIAL_VALUES:
  241. return Object.assign({}, prevState, {initialized: true, values: action.data});
  242. case at.PREF_CHANGED:
  243. newValues = Object.assign({}, prevState.values);
  244. newValues[action.data.name] = action.data.value;
  245. return Object.assign({}, prevState, {values: newValues});
  246. default:
  247. return prevState;
  248. }
  249. }
  250. function Sections(prevState = INITIAL_STATE.Sections, action) {
  251. let hasMatch;
  252. let newState;
  253. switch (action.type) {
  254. case at.SECTION_DEREGISTER:
  255. return prevState.filter(section => section.id !== action.data);
  256. case at.SECTION_REGISTER:
  257. // If section exists in prevState, update it
  258. newState = prevState.map(section => {
  259. if (section && section.id === action.data.id) {
  260. hasMatch = true;
  261. return Object.assign({}, section, action.data);
  262. }
  263. return section;
  264. });
  265. // Otherwise, append it
  266. if (!hasMatch) {
  267. const initialized = !!(action.data.rows && action.data.rows.length > 0);
  268. const section = Object.assign({title: "", rows: [], enabled: false}, action.data, {initialized});
  269. newState.push(section);
  270. }
  271. return newState;
  272. case at.SECTION_UPDATE:
  273. newState = prevState.map(section => {
  274. if (section && section.id === action.data.id) {
  275. // If the action is updating rows, we should consider initialized to be true.
  276. // This can be overridden if initialized is defined in the action.data
  277. const initialized = action.data.rows ? {initialized: true} : {};
  278. // Make sure pinned cards stay at their current position when rows are updated.
  279. // Disabling a section (SECTION_UPDATE with empty rows) does not retain pinned cards.
  280. if (action.data.rows && action.data.rows.length > 0 && section.rows.find(card => card.pinned)) {
  281. const rows = Array.from(action.data.rows);
  282. section.rows.forEach((card, index) => {
  283. if (card.pinned) {
  284. // Only add it if it's not already there.
  285. if (rows[index].guid !== card.guid) {
  286. rows.splice(index, 0, card);
  287. }
  288. }
  289. });
  290. return Object.assign({}, section, initialized, Object.assign({}, action.data, {rows}));
  291. }
  292. return Object.assign({}, section, initialized, action.data);
  293. }
  294. return section;
  295. });
  296. if (!action.data.dedupeConfigurations) {
  297. return newState;
  298. }
  299. action.data.dedupeConfigurations.forEach(dedupeConf => {
  300. newState = newState.map(section => {
  301. if (section.id === dedupeConf.id) {
  302. const dedupedRows = dedupeConf.dedupeFrom.reduce((rows, dedupeSectionId) => {
  303. const dedupeSection = newState.find(s => s.id === dedupeSectionId);
  304. const [, newRows] = dedupe.group(dedupeSection.rows, rows);
  305. return newRows;
  306. }, section.rows);
  307. return Object.assign({}, section, {rows: dedupedRows});
  308. }
  309. return section;
  310. });
  311. });
  312. return newState;
  313. case at.SECTION_UPDATE_CARD:
  314. return prevState.map(section => {
  315. if (section && section.id === action.data.id && section.rows) {
  316. const newRows = section.rows.map(card => {
  317. if (card.url === action.data.url) {
  318. return Object.assign({}, card, action.data.options);
  319. }
  320. return card;
  321. });
  322. return Object.assign({}, section, {rows: newRows});
  323. }
  324. return section;
  325. });
  326. case at.PLACES_BOOKMARK_ADDED:
  327. if (!action.data) {
  328. return prevState;
  329. }
  330. return prevState.map(section => Object.assign({}, section, {
  331. rows: section.rows.map(item => {
  332. // find the item within the rows that is attempted to be bookmarked
  333. if (item.url === action.data.url) {
  334. const {bookmarkGuid, bookmarkTitle, dateAdded} = action.data;
  335. return Object.assign({}, item, {
  336. bookmarkGuid,
  337. bookmarkTitle,
  338. bookmarkDateCreated: dateAdded,
  339. type: "bookmark",
  340. });
  341. }
  342. return item;
  343. }),
  344. }));
  345. case at.PLACES_SAVED_TO_POCKET:
  346. if (!action.data) {
  347. return prevState;
  348. }
  349. return prevState.map(section => Object.assign({}, section, {
  350. rows: section.rows.map(item => {
  351. if (item.url === action.data.url) {
  352. return Object.assign({}, item, {
  353. open_url: action.data.open_url,
  354. pocket_id: action.data.pocket_id,
  355. title: action.data.title,
  356. type: "pocket",
  357. });
  358. }
  359. return item;
  360. }),
  361. }));
  362. case at.PLACES_BOOKMARK_REMOVED:
  363. if (!action.data) {
  364. return prevState;
  365. }
  366. return prevState.map(section => Object.assign({}, section, {
  367. rows: section.rows.map(item => {
  368. // find the bookmark within the rows that is attempted to be removed
  369. if (item.url === action.data.url) {
  370. const newSite = Object.assign({}, item);
  371. delete newSite.bookmarkGuid;
  372. delete newSite.bookmarkTitle;
  373. delete newSite.bookmarkDateCreated;
  374. if (!newSite.type || newSite.type === "bookmark") {
  375. newSite.type = "history";
  376. }
  377. return newSite;
  378. }
  379. return item;
  380. }),
  381. }));
  382. case at.PLACES_LINK_DELETED:
  383. case at.PLACES_LINK_BLOCKED:
  384. if (!action.data) {
  385. return prevState;
  386. }
  387. return prevState.map(section =>
  388. Object.assign({}, section, {rows: section.rows.filter(site => site.url !== action.data.url)}));
  389. case at.DELETE_FROM_POCKET:
  390. case at.ARCHIVE_FROM_POCKET:
  391. return prevState.map(section =>
  392. Object.assign({}, section, {rows: section.rows.filter(site => site.pocket_id !== action.data.pocket_id)}));
  393. case at.SNIPPETS_PREVIEW_MODE:
  394. return prevState.map(section => ({...section, rows: []}));
  395. default:
  396. return prevState;
  397. }
  398. }
  399. function Snippets(prevState = INITIAL_STATE.Snippets, action) {
  400. switch (action.type) {
  401. case at.SNIPPETS_DATA:
  402. return Object.assign({}, prevState, {initialized: true}, action.data);
  403. case at.SNIPPET_BLOCKED:
  404. return Object.assign({}, prevState, {blockList: prevState.blockList.concat(action.data)});
  405. case at.SNIPPETS_BLOCKLIST_CLEARED:
  406. return Object.assign({}, prevState, {blockList: []});
  407. case at.SNIPPETS_RESET:
  408. return INITIAL_STATE.Snippets;
  409. default:
  410. return prevState;
  411. }
  412. }
  413. function Pocket(prevState = INITIAL_STATE.Pocket, action) {
  414. switch (action.type) {
  415. case at.POCKET_WAITING_FOR_SPOC:
  416. return {...prevState, waitingForSpoc: action.data};
  417. case at.POCKET_LOGGED_IN:
  418. return {...prevState, isUserLoggedIn: !!action.data};
  419. case at.POCKET_CTA:
  420. return {
  421. ...prevState,
  422. pocketCta: {
  423. ctaButton: action.data.cta_button,
  424. ctaText: action.data.cta_text,
  425. ctaUrl: action.data.cta_url,
  426. useCta: action.data.use_cta,
  427. },
  428. };
  429. default:
  430. return prevState;
  431. }
  432. }
  433. function DiscoveryStream(prevState = INITIAL_STATE.DiscoveryStream, action) {
  434. // Return if action data is empty, or spocs or feeds data is not loaded
  435. const isNotReady = () =>
  436. !action.data || !prevState.spocs.loaded || !prevState.feeds.loaded;
  437. const nextState = handleSites => (
  438. {
  439. ...prevState,
  440. spocs: {
  441. ...prevState.spocs,
  442. data: prevState.spocs.data.spocs ? {
  443. spocs: handleSites(prevState.spocs.data.spocs),
  444. } : {},
  445. },
  446. feeds: {
  447. ...prevState.feeds,
  448. data: Object.keys(prevState.feeds.data).reduce((accumulator, feed_url) => {
  449. accumulator[feed_url] = {
  450. data: {
  451. ...prevState.feeds.data[feed_url].data,
  452. recommendations: handleSites(prevState.feeds.data[feed_url].data.recommendations),
  453. },
  454. };
  455. return accumulator;
  456. }, {}),
  457. },
  458. });
  459. switch (action.type) {
  460. case at.DISCOVERY_STREAM_CONFIG_CHANGE:
  461. // The reason this is a separate action is so it doesn't trigger a listener update on init
  462. case at.DISCOVERY_STREAM_CONFIG_SETUP:
  463. return {...prevState, config: action.data || {}};
  464. case at.DISCOVERY_STREAM_LAYOUT_UPDATE:
  465. return {...prevState, lastUpdated: action.data.lastUpdated || null, layout: action.data.layout || []};
  466. case at.DISCOVERY_STREAM_LAYOUT_RESET:
  467. return {...INITIAL_STATE.DiscoveryStream, config: prevState.config};
  468. case at.DISCOVERY_STREAM_FEEDS_UPDATE:
  469. return {
  470. ...prevState,
  471. feeds: {
  472. ...prevState.feeds,
  473. loaded: true,
  474. },
  475. };
  476. case at.DISCOVERY_STREAM_FEED_UPDATE:
  477. const newData = {};
  478. newData[action.data.url] = action.data.feed;
  479. return {
  480. ...prevState,
  481. feeds: {
  482. ...prevState.feeds,
  483. data: {
  484. ...prevState.feeds.data,
  485. ...newData,
  486. },
  487. },
  488. };
  489. case at.DISCOVERY_STREAM_SPOCS_CAPS:
  490. return {
  491. ...prevState,
  492. spocs: {
  493. ...prevState.spocs,
  494. frequency_caps: [...prevState.spocs.frequency_caps, ...action.data],
  495. },
  496. };
  497. case at.DISCOVERY_STREAM_SPOCS_ENDPOINT:
  498. return {
  499. ...prevState,
  500. spocs: {
  501. ...INITIAL_STATE.DiscoveryStream.spocs,
  502. spocs_endpoint: action.data || INITIAL_STATE.DiscoveryStream.spocs.spocs_endpoint,
  503. },
  504. };
  505. case at.DISCOVERY_STREAM_SPOCS_UPDATE:
  506. if (action.data) {
  507. return {
  508. ...prevState,
  509. spocs: {
  510. ...prevState.spocs,
  511. lastUpdated: action.data.lastUpdated,
  512. data: action.data.spocs,
  513. loaded: true,
  514. },
  515. };
  516. }
  517. return prevState;
  518. case at.DISCOVERY_STREAM_LINK_BLOCKED:
  519. return isNotReady() ? prevState :
  520. nextState(items => items.filter(item => item.url !== action.data.url));
  521. case at.PLACES_SAVED_TO_POCKET:
  522. const addPocketInfo = item => {
  523. if (item.url === action.data.url) {
  524. return Object.assign({}, item, {
  525. open_url: action.data.open_url,
  526. pocket_id: action.data.pocket_id,
  527. });
  528. }
  529. return item;
  530. };
  531. return isNotReady() ? prevState :
  532. nextState(items => items.map(addPocketInfo));
  533. case at.DELETE_FROM_POCKET:
  534. case at.ARCHIVE_FROM_POCKET:
  535. return isNotReady() ? prevState :
  536. nextState(items => items.filter(item => item.pocket_id !== action.data.pocket_id));
  537. case at.PLACES_BOOKMARK_ADDED:
  538. const updateBookmarkInfo = item => {
  539. if (item.url === action.data.url) {
  540. const {bookmarkGuid, bookmarkTitle, dateAdded} = action.data;
  541. return Object.assign({}, item, {
  542. bookmarkGuid,
  543. bookmarkTitle,
  544. bookmarkDateCreated: dateAdded,
  545. });
  546. }
  547. return item;
  548. };
  549. return isNotReady() ? prevState :
  550. nextState(items => items.map(updateBookmarkInfo));
  551. case at.PLACES_BOOKMARK_REMOVED:
  552. const removeBookmarkInfo = item => {
  553. if (item.url === action.data.url) {
  554. const newSite = Object.assign({}, item);
  555. delete newSite.bookmarkGuid;
  556. delete newSite.bookmarkTitle;
  557. delete newSite.bookmarkDateCreated;
  558. return newSite;
  559. }
  560. return item;
  561. };
  562. return isNotReady() ? prevState :
  563. nextState(items => items.map(removeBookmarkInfo));
  564. default:
  565. return prevState;
  566. }
  567. }
  568. function Search(prevState = INITIAL_STATE.Search, action) {
  569. switch (action.type) {
  570. case at.HIDE_SEARCH:
  571. return Object.assign({...prevState, hide: true});
  572. case at.FAKE_FOCUS_SEARCH:
  573. return Object.assign({...prevState, fakeFocus: true});
  574. case at.SHOW_SEARCH:
  575. return Object.assign({...prevState, hide: false, fakeFocus: false});
  576. default:
  577. return prevState;
  578. }
  579. }
  580. this.INITIAL_STATE = INITIAL_STATE;
  581. this.TOP_SITES_DEFAULT_ROWS = TOP_SITES_DEFAULT_ROWS;
  582. this.TOP_SITES_MAX_SITES_PER_ROW = TOP_SITES_MAX_SITES_PER_ROW;
  583. this.reducers = {
  584. TopSites,
  585. App,
  586. ASRouter,
  587. Snippets,
  588. Prefs,
  589. Dialog,
  590. Sections,
  591. Pocket,
  592. DiscoveryStream,
  593. Search,
  594. };
  595. const EXPORTED_SYMBOLS = [
  596. "reducers",
  597. "INITIAL_STATE",
  598. "insertPinned",
  599. "TOP_SITES_DEFAULT_ROWS",
  600. "TOP_SITES_MAX_SITES_PER_ROW",
  601. ];