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
|
import * as Uebersicht from "uebersicht";
import * as Utils from "../../utils";
import SettingsInner from "./settings-inner.jsx";
import SettingsWidgets from "./settings-widgets.jsx";
import * as Settings from "../../settings";
const { React } = Uebersicht;
const TABS_STORAGE_KEY = "simple-bar-last-current-settings-tab";
const TABS = [
"global",
"themes",
"process",
"spacesDisplay",
"widgets",
"customStyles",
];
/**
* Main settings component.
* @param {Object} props - Component properties.
* @param {Function} props.closeSettings - Function to close the settings.
*/
export default function Component({ closeSettings }) {
// State to keep track of the current tab
const [currentTab, setCurrentTab] = React.useState(getLastCurrentTab());
// State to keep track of pending changes
const [pendingChanges, setPendingChanges] = React.useState(0);
// Get current settings
const settings = Settings.get();
// State to keep track of new settings
const [newSettings, setNewSettings] = React.useState(settings);
/**
* Update the current tab and store it in session storage.
* @param {number} tab - The index of the tab to switch to.
*/
const updateTab = (tab) => {
setCurrentTab(tab);
window.sessionStorage.setItem(TABS_STORAGE_KEY, tab);
};
/**
* Refresh the simple-bar with new settings.
* @param {Event} e - The event object.
*/
const refreshSimpleBar = async (e) => {
Utils.clickEffect(e);
setPendingChanges(0);
await Settings.set(newSettings);
Utils.hardRefresh();
};
// Effect to calculate the number of pending changes
React.useEffect(() => {
const diffs = Utils.compareObjects(settings, newSettings);
const deepDiffs = Object.keys(diffs).reduce(
(acc, key) => [...acc, ...Object.keys(diffs[key])],
[],
);
setPendingChanges(deepDiffs.length);
}, [newSettings, settings]);
return (
<div className="settings">
<div className="settings__overlay" onClick={closeSettings} />
<div className="settings__outer">
<div className="settings__header">
<button
className="settings__header-dot settings__header-dot--close"
onClick={closeSettings}
/>
<span className="settings__header-dot settings__header-dot--disabled" />
<span className="settings__header-dot settings__header-dot--disabled" />
Settings
</div>
<div className="settings__tabs">
{Object.keys(Settings.defaultSettings).map((key) => {
const setting = Settings.data[key];
const hideTab = !TABS.includes(key);
if (!setting || hideTab) return null;
const { label } = setting;
const i = TABS.indexOf(key);
const classes = Utils.classNames("settings__tab", {
"settings__tab--current": i === currentTab,
});
return (
<button key={i} className={classes} onClick={() => updateTab(i)}>
{label}
</button>
);
})}
</div>
<div className="settings__inner">
{Object.keys(Settings.defaultSettings).map((key) => {
const setting = Settings.data[key];
const hideContent = !TABS.includes(key);
if (!setting || hideContent) return null;
const isWidgetsTab = key === "widgets";
const { label } = setting;
return (
<div
key={key}
className="settings__category"
style={{ transform: `translateX(-${100 * currentTab}%)` }}
>
{isWidgetsTab ? (
<SettingsWidgets
newSettings={newSettings}
setNewSettings={setNewSettings}
/>
) : (
<React.Fragment>
<div className="settings__inner-title">{label}</div>
<SettingsInner
settingKey={key}
setting={setting}
newSettings={newSettings}
setNewSettings={setNewSettings}
/>
</React.Fragment>
)}
</div>
);
})}
</div>
<div className="settings__bottom">
{pendingChanges !== 0 && (
<div className="settings__pending-changes">
<b>{pendingChanges}</b> pending change{pendingChanges > 1 && "s"}
</div>
)}
<button
className="settings__refresh-button"
onClick={refreshSimpleBar}
disabled={!pendingChanges}
>
Confirm changes and refresh simple-bar
</button>
</div>
</div>
</div>
);
}
/**
* Get the last current tab from session storage.
* @returns {number} The index of the last current tab.
*/
function getLastCurrentTab() {
const storedLastCurrentTab = window.sessionStorage.getItem(TABS_STORAGE_KEY);
if (storedLastCurrentTab) return parseInt(storedLastCurrentTab, 10);
return 0;
}
|