-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathSettingsTable.tsx
141 lines (121 loc) · 4.62 KB
/
SettingsTable.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
import { settingDetails } from './settings';
import { getElement, getLatestSettings } from './utils';
import { Fragment } from 'react';
function isFuncEnabled(func: any) {
const btnToDisable = getElement(`[data-func=${func}]`);
if (btnToDisable && btnToDisable.classList.contains('enable')) {
return true;
}
return false;
}
export function SettingsTable() {
const [filterText, setFilterText] = useState('');
const [settings, setSettings] = useState(settingDetails);
useEffect(() => {
refreshSettings();
}, []);
const refreshSettings = () => {
console.log('refreshing settings');
const obj = [...settingDetails];
getLatestSettings()
.then((set: any) => {
console.log('LatestSettings: ', set);
// add current status to settings object
obj.forEach((e: any) => {
e.status = set[e.func] ? 'enable' : 'disable';
});
setSettings([...obj]);
return null;
})
.catch((e) => console.log(e));
};
const handleFeature = useCallback((obj: any) => {
console.log('obj', obj);
console.log('clicked: ');
const func = obj.func;
const funcToDisable = obj.disable_func;
let toEnable = false;
if (obj.status === 'enable') {
toEnable = false;
} else {
toEnable = true;
}
try {
getLatestSettings()
.then((set: any) => {
console.log('getLatestSettings ->', set);
set[func] = toEnable;
set.call_func = {
name: func,
arg: toEnable,
};
// disable other related func if both are enabled
if (funcToDisable && isFuncEnabled(funcToDisable) && toEnable) {
set[funcToDisable] = false;
}
browser.storage.sync.set({ nb_settings: set }).then(() => {
refreshSettings();
});
return null;
})
.catch((e) => console.log(e));
} catch (e) {
console.log(e);
}
}, []);
const filteredItems = settings.filter(
(item) =>
item.name.toLocaleLowerCase().includes(filterText) || item.desc.toLocaleLowerCase().includes(filterText),
);
console.log('filtered items', filteredItems);
const itemsToDisplay = filterText ? filteredItems : settings;
const handleOnchange = (e: any) => {
console.log('value changed');
setFilterText(e.target.value.toLocaleLowerCase());
};
return (
<>
<input
className="settings search"
type="text"
autoFocus
value={filterText}
placeholder="Search feature..."
onInput={handleOnchange}
/>
<div className="settings table">
{!filteredItems.length && <div className="no-setting">No setting found</div>}
{itemsToDisplay.map((obj, index) => (
<Fragment key={obj.func}>
<div
className={`row ${(obj as any).status}`}
data-func={obj.func}
data-disable_func={obj.disable_func}
onClick={() => handleFeature(obj)}>
<div className="text-wrapper">
<div className="name">{obj.name}</div>
{obj.desc && <div className="desc">{obj.desc}</div>}
</div>
<div
className="button toggle"
role="button"
aria-disabled="false"
tabIndex={0}>
<div className="knob">
<div className="pos" />
</div>
</div>
</div>
{/* {index === settingDetails.length - 1 ? (
<div className="divider last" />
) : (
<div className="">
<div className="" />
</div>
)} */}
</Fragment>
))}
</div>
</>
);
}