-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTroubleshooter.tsx
206 lines (190 loc) · 5.64 KB
/
Troubleshooter.tsx
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import useStore from "../store";
export default function Troubleshooter() {
const state = useStore((state) => state.troubleshooter);
if (state.message !== "") {
return (
<div className="p-1.5 w-full text-sm border rounded-lg border-gray-200 dark:border-slate-700 bg-white dark:bg-slate-800">
{state.message}
</div>
);
}
if (state.isTroubleshooting()) {
return <NextButton />;
} else {
return <StartButton />;
}
}
function StartButton() {
const [troubleshooter, targets] = useStore((state) => [
state.troubleshooter,
state.targets,
]);
const start = async () => {
const range: [number, number] = [0, targets.available.length - 1];
await troubleshooter.setRange(range[0], range[1]);
await troubleshooter.saveBeginningTargets();
await targets.setAll(false, false);
await targets.clear(true);
const newOverrides = half(range, targets.available, "top");
for (const target of newOverrides) {
await targets.set(target, true, false);
}
};
return (
<button
className="p-1.5 w-full text-sm border rounded-lg border-gray-200 dark:border-slate-700 bg-white dark:bg-slate-800"
onClick={start}
>
Troubleshoot Website
</button>
);
}
function NextButton() {
const [troubleshooter, targets, domain] = useStore((state) => [
state.troubleshooter,
state.targets,
state.activeTab.domain,
]);
const setOverrides = async (newOverrides: browser.fppOverrides.Target[]) => {
await targets.setAll(false, false);
for (const target of newOverrides) {
await targets.set(target, true, false);
}
};
const onSolved = async () => {
const direction =
whichHalf(troubleshooter.range, targets.available, targets.global) ===
"top"
? "bottom"
: "top";
const newOverrides = half(
troubleshooter.range,
targets.available,
direction
);
if (newOverrides.length === 1) {
troubleshooter.setMessage(
`Troubleshooting complete! ${newOverrides[0]} was causing the breakage.`
);
setTimeout(() => {
troubleshooter.setMessage("");
}, 7500);
onCancel();
return;
}
await setOverrides(newOverrides);
};
const onNotSolved = async () => {
const direction = whichHalf(
troubleshooter.range,
targets.available,
targets.global
);
const newRange = getRange(troubleshooter.range, direction);
// Only one target was active previously and it is still not solved
// i.e. we don't have any other targets to disable
if (
newRange[0] === newRange[1] &&
troubleshooter.range[1] - troubleshooter.range[0] === 1
) {
troubleshooter.setMessage(
`Troubleshooting complete! ${
targets.available[troubleshooter.range[0]]
} and ${
targets.available[troubleshooter.range[1]]
} was causing the breakage.`
);
setTimeout(() => {
troubleshooter.setMessage("");
}, 7500);
onCancel();
return;
}
await troubleshooter.setRange(newRange[0], newRange[1]);
const newOverrides = half(newRange, targets.available, direction);
await setOverrides(newOverrides);
};
const onCancel = async () => {
await targets.setAll(false, false);
Object.entries(troubleshooter.beginningTargets.global).forEach(
([target, enabled]) => {
targets.set(target, enabled, false);
}
);
Object.entries(troubleshooter.beginningTargets.granular).forEach(
([target, enabled]) => {
targets.set(target, enabled, true);
}
);
await troubleshooter.setRange(0, 0);
};
const forgetWebsite = async () => {
await browser.fppOverrides.forgetWebsite(domain);
};
return (
<div className="inline-flex w-full" role="group">
<button
className="p-1.5 w-full text-sm border rounded-s-lg border-gray-200 dark:border-slate-700 bg-white dark:bg-slate-800"
onClick={onSolved}
>
Solved
</button>
<button
className="p-1.5 w-full text-sm border-t border-b border-gray-200 dark:border-slate-700 bg-white dark:bg-slate-800"
onClick={onNotSolved}
>
Not Solved
</button>
<button
className="p-1.5 w-full text-sm border border-t border-b border-gray-200 dark:border-slate-700 bg-white dark:bg-slate-800"
onClick={onCancel}
>
Cancel
</button>
<button
className="p-1.5 w-full text-sm border rounded-e-lg border-gray-200 dark:border-slate-700 bg-white dark:bg-slate-800"
onClick={forgetWebsite}
>
Forget Website
</button>
</div>
);
}
// Returns which half of the targets were enabled during the last iteration
function whichHalf(
range: [number, number],
availableTargets: browser.fppOverrides.Target[],
overrides: Record<browser.fppOverrides.Target, boolean>
) {
const mid = Math.floor((range[0] + range[1]) / 2);
if (overrides[availableTargets[mid]]) {
return "top";
}
return "bottom";
}
function getRange(
range: [number, number],
direction: "top" | "bottom"
): [number, number] {
const mid = Math.floor((range[0] + range[1]) / 2);
let start = range[0];
let end = mid;
if (direction === "bottom") {
start = mid + 1;
end = range[1];
}
return [start, end];
}
// Returns the range and half of the available targets based on the direction and the range
function half(
range: [number, number],
availableTargets: browser.fppOverrides.Target[],
direction: "top" | "bottom"
) {
const [start, end] = getRange(range, direction);
const half = [];
for (let i = start; i <= end; i++) {
half.push(availableTargets[i]);
}
return half;
}