Skip to content

Commit

Permalink
Merge pull request #134 from throwaway96/refactoring-20240324
Browse files Browse the repository at this point in the history
Miscellaneous UI/config refactoring
  • Loading branch information
throwaway96 authored Mar 27, 2024
2 parents 9ac14a7 + a271695 commit ea029f0
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 135 deletions.
97 changes: 79 additions & 18 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,100 @@
const CONFIG_KEY = 'ytaf-configuration';
const defaultConfig = {
enableAdBlock: true,
enableSponsorBlock: true,
enableSponsorBlockSponsor: true,
enableSponsorBlockIntro: true,
enableSponsorBlockOutro: true,
enableSponsorBlockInteraction: true,
enableSponsorBlockSelfPromo: true,
enableSponsorBlockMusicOfftopic: true
};

let localConfig;

try {
localConfig = JSON.parse(window.localStorage[CONFIG_KEY]);
} catch (err) {
console.warn('Config read failed:', err);
localConfig = defaultConfig;

export const configOptions = new Map([
['enableAdBlock', { default: true, desc: 'Enable ad blocking' }],
['enableSponsorBlock', { default: true, desc: 'Enable SponsorBlock' }],
[
'enableSponsorBlockSponsor',
{ default: true, desc: 'Skip sponsor segments' }
],
['enableSponsorBlockIntro', { default: true, desc: 'Skip intro segments' }],
['enableSponsorBlockOutro', { default: true, desc: 'Skip outro segments' }],
[
'enableSponsorBlockInteraction',
{
default: true,
desc: 'Skip interaction reminder segments'
}
],
[
'enableSponsorBlockSelfPromo',
{
default: true,
desc: 'Skip self promotion segments'
}
],
[
'enableSponsorBlockMusicOfftopic',
{
default: true,
desc: 'Skip music and off-topic segments'
}
]
]);

const defaultConfig = (() => {
let ret = {};
for (const [k, v] of configOptions) {
ret[k] = v.default;
}
return ret;
})();

function loadStoredConfig() {
const storage = window.localStorage.getItem(CONFIG_KEY);

if (storage === null) {
console.info('Config not set; using defaults.');
return null;
}

try {
return JSON.parse(storage);
} catch (err) {
console.warn('Error parsing stored config:', err);
return null;
}
}

// Use defaultConfig as a prototype so writes to localConfig don't change it.
let localConfig = loadStoredConfig() ?? Object.create(defaultConfig);

function configExists(key) {
return configOptions.has(key);
}

export function getConfigDesc(key) {
if (!configExists(key)) {
throw new Error('tried to get desc for unknown config key:', key);
}

return configOptions.get(key).desc;
}

export function configRead(key) {
if (!configExists(key)) {
throw new Error('tried to read unknown config key:', key);
}

if (localConfig[key] === undefined) {
console.warn(
'Populating key',
key,
'with default value',
defaultConfig[key]
);

localConfig[key] = defaultConfig[key];
}

return localConfig[key];
}

export function configWrite(key, value) {
if (!configExists(key)) {
throw new Error('tried to write unknown config key:', key);
}

console.info('Setting key', key, 'to', value);
localConfig[key] = value;
window.localStorage[CONFIG_KEY] = JSON.stringify(localConfig);
Expand Down
Loading

0 comments on commit ea029f0

Please sign in to comment.