forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject-text-search.js
More file actions
105 lines (87 loc) · 2.53 KB
/
Copy pathproject-text-search.js
File metadata and controls
105 lines (87 loc) · 2.53 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* 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/>. */
// @flow
// @format
/**
* Project text search reducer
* @module reducers/project-text-search
*/
import * as I from "immutable";
import makeRecord from "../utils/makeRecord";
import type { Action } from "../actions/types";
import type { Record } from "../utils/makeRecord";
import type { List } from "immutable";
export type Search = {
id: string,
filepath: string,
matches: I.List<any>
};
export type StatusType = "INITIAL" | "FETCHING" | "DONE" | "ERROR";
export const statusType = {
initial: "INITIAL",
fetching: "FETCHING",
done: "DONE",
error: "ERROR"
};
export type ResultRecord = Record<Search>;
export type ResultList = List<ResultRecord>;
export type ProjectTextSearchState = {
query: string,
results: ResultList,
status: string
};
export function initialProjectTextSearchState(): Record<
ProjectTextSearchState
> {
return makeRecord(
({
query: "",
results: I.List(),
status: statusType.initial
}: ProjectTextSearchState)
)();
}
function update(
state: Record<ProjectTextSearchState> = initialProjectTextSearchState(),
action: Action
): Record<ProjectTextSearchState> {
switch (action.type) {
case "ADD_QUERY":
const actionCopy = action;
return state.update("query", value => actionCopy.query);
case "CLEAR_QUERY":
return state.merge({
query: "",
status: statusType.initial
});
case "ADD_SEARCH_RESULT":
const results = state.get("results");
return state.merge({ results: results.push(action.result) });
case "UPDATE_STATUS":
return state.merge({ status: action.status });
case "CLEAR_SEARCH_RESULTS":
return state.merge({
results: state.get("results").clear()
});
case "CLEAR_SEARCH":
case "CLOSE_PROJECT_SEARCH":
return state.merge({
query: "",
results: state.get("results").clear(),
status: statusType.initial
});
}
return state;
}
type OuterState = { projectTextSearch: Record<ProjectTextSearchState> };
export function getTextSearchResults(state: OuterState) {
return state.projectTextSearch.get("results");
}
export function getTextSearchStatus(state: OuterState) {
return state.projectTextSearch.get("status");
}
export function getTextSearchQuery(state: OuterState) {
return state.projectTextSearch.get("query");
}
export default update;