summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/reducers/project-text-search.js
blob: e8037182408e7f11dfd1e7164742b3fd1e7b634c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* 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/>. */

// @format

/**
 * Project text search reducer
 * @module reducers/project-text-search
 */

import { createLocation } from "../utils/location";

export const statusType = {
  initial: "INITIAL",
  fetching: "FETCHING",
  cancelled: "CANCELLED",
  done: "DONE",
  error: "ERROR",
};

export function initialProjectTextSearchState(state) {
  return {
    query: state?.query || "",
    results: [],
    ongoingSearch: null,
    status: statusType.initial,
  };
}

function update(state = initialProjectTextSearchState(), action) {
  switch (action.type) {
    case "ADD_QUERY":
      return { ...state, query: action.query };

    case "ADD_SEARCH_RESULT":
      const { location, matches } = action;
      if (matches.length === 0) {
        return state;
      }

      const result = {
        type: "RESULT",
        location,
        // `matches` are generated by project-search worker's `findSourceMatches` method
        matches: matches.map(m => ({
          type: "MATCH",
          location: createLocation({
            ...location,
            // `matches` only contain line and column
            // `location` will already refer to the right source/sourceActor
            line: m.line,
            column: m.column,
          }),
          matchIndex: m.matchIndex,
          match: m.match,
          value: m.value,
        })),
      };
      return { ...state, results: [...state.results, result] };

    case "UPDATE_STATUS":
      const ongoingSearch =
        action.status == statusType.fetching ? state.ongoingSearch : null;
      return { ...state, status: action.status, ongoingSearch };

    case "CLEAR_SEARCH_RESULTS":
      return { ...state, results: [] };

    case "ADD_ONGOING_SEARCH":
      return { ...state, ongoingSearch: action.ongoingSearch };

    case "CLEAR_SEARCH":
    case "CLOSE_PROJECT_SEARCH":
      return initialProjectTextSearchState();
    case "NAVIGATE":
      return initialProjectTextSearchState(state);
  }
  return state;
}

export default update;