Skip to content

Commit

Permalink
all settings under one local key
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Bulat committed Mar 12, 2024
1 parent 15a6c06 commit 407978a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 36 deletions.
56 changes: 24 additions & 32 deletions src/contexts/Settings/Local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,41 @@
// SPDX-License-Identifier: GPL-3.0-only

import { localStorageOrDefault } from '@w3ux/utils';
import type { LocalSettings, SettingsKey } from './types';

// ------------------------------------------------------
// Getters.
// ------------------------------------------------------

// Gets saved auto connect setting from local storage, or return undefined otherwise.
export const getAutoConnect = (): boolean => {
const result = localStorageOrDefault(
'settings:autoConnect',
undefined,
true
) as boolean | undefined;

if (result !== undefined) {
return result as boolean;
export const getSetting = (key: SettingsKey): boolean => {
const result = localStorageOrDefault(`settings`, undefined, true) as
| LocalSettings
| undefined;

if (result) {
const maybeValue = result[key];
if (maybeValue) {
return maybeValue as boolean;
}
}
return true;
};

// Gets saved auto tab naming setting from local storage, or return undefined otherwise.
export const getAutoTabNaming = (): boolean => {
const result = localStorageOrDefault(
'settings:autoTabNaming',
undefined,
true
) as boolean | undefined;

if (result !== undefined) {
return result as boolean;
}
return true;
return false;
};

// ------------------------------------------------------
// Setters.
// ------------------------------------------------------

// Sets auto connect state to local storage.
export const setAutoConnect = (value: boolean) => {
localStorage.setItem('settings:autoConnect', JSON.stringify(value));
};

// Sets tab naming state to local storage.
export const setAutoTabNaming = (value: boolean) => {
localStorage.setItem('settings:autoTabNaming', JSON.stringify(value));
// Sets a setting value to local storage by the given key.
export const setSetting = (key: SettingsKey, value: boolean) => {
const current =
(localStorageOrDefault(`settings`, undefined, true) as
| LocalSettings
| undefined) || {};

const updated = {
...current,
[key]: value,
};
localStorage.setItem(`settings`, JSON.stringify(updated));
};
8 changes: 4 additions & 4 deletions src/contexts/Settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ export const useSettings = () => useContext(SettingsContext);
export const SettingsProvider = ({ children }: { children: ReactNode }) => {
// Whether to Turn on auto connect by default when opening new tabs.
const [autoConnect, setAutoConnectState] = useState<boolean>(
local.getAutoConnect()
local.getSetting('autoConnect')
);

// Whether to automatically rename tabs based on chain being connected to.
const [autoTabNaming, setAutoTabNamingState] = useState<boolean>(
local.getAutoTabNaming()
local.getSetting('autoTabNaming')
);

// Set auto connect state, to save to local storage.
const setAutoConnect = (value: boolean) => {
local.setAutoConnect(value);
local.setSetting('autoConnect', value);
setAutoConnectState(value);
};

// Set auto tab naming state, to save to local storage.
const setAutoTabNaming = (value: boolean) => {
local.setAutoTabNaming(value);
local.setSetting('autoTabNaming', value);
setAutoTabNamingState(value);
};

Expand Down
7 changes: 7 additions & 0 deletions src/contexts/Settings/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ export interface SettingsContextInterface {
setAutoConnect: (value: boolean) => void;
setAutoTabNaming: (value: boolean) => void;
}

export interface LocalSettings {
autoConnect: boolean;
autoTabNaming: boolean;
}

export type SettingsKey = keyof LocalSettings;

0 comments on commit 407978a

Please sign in to comment.