Skip to content

Commit

Permalink
Refactor settings component and add saveToConfigFile function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Tinland committed Jan 13, 2024
1 parent d661c3f commit 52dc755
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/components/settings/settings-component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ export default function Component({ closeSettings }) {
const settings = Settings.get();
const [newSettings, setNewSettings] = React.useState(settings);

const onTabClick = (tab) => {
const updateTab = (tab) => {
setCurrentTab(tab);
window.sessionStorage.setItem(LAST_CURRENT_TAB, tab);
};

const onRefreshClick = async (e) => {
const refreshSimpleBar = async (e) => {
Utils.clickEffect(e);
setPendingChanges(0);
await Settings.set(newSettings);
Utils.hardRefresh();
};

const onImportClick = async () => {
const importSettings = async () => {
let fileExists = false;
try {
fileExists = Boolean(
Expand All @@ -41,7 +41,7 @@ export default function Component({ closeSettings }) {
setNewSettings(externalConfig);
};

const onExportClick = async () => {
const exportSettings = async () => {
const { externalConfigFile } = newSettings.global;
if (externalConfigFile) {
await Uebersicht.run(
Expand Down Expand Up @@ -84,7 +84,7 @@ export default function Component({ closeSettings }) {
"settings__tab--current": i === currentTab,
});
return (
<button key={i} className={classes} onClick={() => onTabClick(i)}>
<button key={i} className={classes} onClick={() => updateTab(i)}>
{label}
</button>
);
Expand Down Expand Up @@ -179,15 +179,15 @@ export default function Component({ closeSettings }) {
<React.Fragment>
<button
className="settings__import-button"
onClick={onImportClick}
onClick={importSettings}
disabled={!!pendingChanges}
>
Import
</button>
or
<button
className="settings__export-button"
onClick={onExportClick}
onClick={exportSettings}
disabled={!!pendingChanges}
>
Export
Expand All @@ -204,7 +204,7 @@ export default function Component({ closeSettings }) {
)}
<button
className="settings__refresh-button"
onClick={onRefreshClick}
onClick={refreshSimpleBar}
disabled={!pendingChanges}
>
Confirm changes and refresh simple-bar
Expand Down
13 changes: 13 additions & 0 deletions lib/settings.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as Uebersicht from "uebersicht";
import * as Themes from "./styles/themes";
import * as Utils from "./utils";
import UserWidgetsCreator from "./components/settings/user-widgets-creator.jsx";

export { Component, styles, Wrapper } from "./components/settings/settings.jsx";

const EXTERNAL_CONFIG_FILE_PATH = `~/.simplebarrc`;
const SETTINGS_STORAGE_KEY = "simple-bar-settings";

const availableThemes = Object.keys(Themes.collection).map((key) => {
Expand Down Expand Up @@ -669,3 +671,14 @@ export async function set(newSettings) {
JSON.stringify(newSettings)
);
}

export async function saveToConfigFile(newSettings) {
const { externalConfigFile } = newSettings.global;
if (externalConfigFile) {
const settings = JSON.stringify(newSettings, undefined, 2);
const cleanSettings = settings.replace(/'/g, "'\"'\"'");
await Uebersicht.run(
`echo '${cleanSettings}' | tee ${EXTERNAL_CONFIG_FILE_PATH}`
);
}
}

0 comments on commit 52dc755

Please sign in to comment.