diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /devtools/client/netmonitor/src/reducers/search.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/netmonitor/src/reducers/search.js')
-rw-r--r-- | devtools/client/netmonitor/src/reducers/search.js | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/devtools/client/netmonitor/src/reducers/search.js b/devtools/client/netmonitor/src/reducers/search.js new file mode 100644 index 0000000000..91e843e18c --- /dev/null +++ b/devtools/client/netmonitor/src/reducers/search.js @@ -0,0 +1,118 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +const { + ADD_SEARCH_QUERY, + ADD_SEARCH_RESULT, + CLEAR_SEARCH_RESULTS, + ADD_ONGOING_SEARCH, + SEARCH_STATUS, + TOGGLE_SEARCH_CASE_SENSITIVE_SEARCH, + UPDATE_SEARCH_STATUS, + SET_TARGET_SEARCH_RESULT, +} = require("resource://devtools/client/netmonitor/src/constants.js"); + +/** + * Search reducer stores the following data: + * - query [String]: the string the user is looking for + * - results [Object]: the list of search results + * - ongoingSearch [Object]: the object representing the current search + * - status [String]: status of the current search (see constants.js) + */ +function Search(overrideParams = {}) { + return Object.assign( + { + query: "", + results: [], + ongoingSearch: null, + status: SEARCH_STATUS.INITIAL, + caseSensitive: false, + targetSearchResult: null, + }, + overrideParams + ); +} + +function search(state = new Search(), action) { + switch (action.type) { + case ADD_SEARCH_QUERY: + return onAddSearchQuery(state, action); + case ADD_SEARCH_RESULT: + return onAddSearchResult(state, action); + case CLEAR_SEARCH_RESULTS: + return onClearSearchResults(state); + case ADD_ONGOING_SEARCH: + return onAddOngoingSearch(state, action); + case TOGGLE_SEARCH_CASE_SENSITIVE_SEARCH: + return onToggleCaseSensitiveSearch(state); + case UPDATE_SEARCH_STATUS: + return onUpdateSearchStatus(state, action); + case SET_TARGET_SEARCH_RESULT: + return onSetTargetSearchResult(state, action); + } + return state; +} + +function onAddSearchQuery(state, action) { + return { + ...state, + query: action.query, + }; +} + +function onAddSearchResult(state, action) { + const { resource } = action; + const results = state.results.slice(); + results.push({ + resource, + results: action.result, + }); + + return { + ...state, + results, + }; +} + +function onClearSearchResults(state) { + return { + ...state, + results: [], + }; +} + +function onAddOngoingSearch(state, action) { + return { + ...state, + ongoingSearch: action.ongoingSearch, + }; +} + +function onToggleCaseSensitiveSearch(state) { + return { + ...state, + caseSensitive: !state.caseSensitive, + }; +} + +function onUpdateSearchStatus(state, action) { + return { + ...state, + status: action.status, + }; +} + +function onSetTargetSearchResult(state, action) { + return { + ...state, + targetSearchResult: action.searchResult, + }; +} + +module.exports = { + Search, + search, +}; |