mirror of
https://github.com/jambonz/chrome-extension-dialer.git
synced 2026-10-04 02:04:28 +00:00
optimized code
This commit is contained in:
+28
-68
@@ -39,33 +39,15 @@ export const saveSettings = (settings: AppSettings) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const str = localStorage.getItem(SETTINGS_KEY);
|
const str = localStorage.getItem(SETTINGS_KEY);
|
||||||
if (str) {
|
|
||||||
const parsed = JSON.parse(str);
|
|
||||||
|
|
||||||
if (parsed.length < 1) {
|
const parsed = str ? JSON.parse(str) : [];
|
||||||
localStorage.setItem(
|
const newItem = {
|
||||||
SETTINGS_KEY,
|
id: parsed.length + 1,
|
||||||
JSON.stringify([{ id: 1, encoded, active: true }])
|
encoded,
|
||||||
);
|
active: parsed.length === 0,
|
||||||
} else {
|
};
|
||||||
localStorage.setItem(
|
|
||||||
SETTINGS_KEY,
|
localStorage.setItem(SETTINGS_KEY, JSON.stringify([...parsed, newItem]));
|
||||||
JSON.stringify([
|
|
||||||
...parsed,
|
|
||||||
{
|
|
||||||
encoded,
|
|
||||||
active: false,
|
|
||||||
id: parsed.length + 1,
|
|
||||||
},
|
|
||||||
])
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
localStorage.setItem(
|
|
||||||
SETTINGS_KEY,
|
|
||||||
JSON.stringify([{ id: 1, encoded, active: true }])
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const editSettings = (settings: AppSettings, id: number) => {
|
export const editSettings = (settings: AppSettings, id: number) => {
|
||||||
@@ -93,24 +75,14 @@ export const editSettings = (settings: AppSettings, id: number) => {
|
|||||||
};
|
};
|
||||||
export const setActiveSettings = (id: number) => {
|
export const setActiveSettings = (id: number) => {
|
||||||
const str = localStorage.getItem(SETTINGS_KEY);
|
const str = localStorage.getItem(SETTINGS_KEY);
|
||||||
|
|
||||||
if (str) {
|
if (str) {
|
||||||
const parsed = JSON.parse(str);
|
const parsed = JSON.parse(str);
|
||||||
|
|
||||||
// for edit:
|
const newData = parsed.map((el: saveSettingFormat) => ({
|
||||||
const newData = parsed.map((el: saveSettingFormat) => {
|
...el,
|
||||||
if (el.id === id)
|
active: el.id === id,
|
||||||
return {
|
}));
|
||||||
id: el.id,
|
|
||||||
active: true,
|
|
||||||
encoded: el.encoded,
|
|
||||||
};
|
|
||||||
else
|
|
||||||
return {
|
|
||||||
id: el.id,
|
|
||||||
active: false,
|
|
||||||
encoded: el.encoded,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
localStorage.setItem(SETTINGS_KEY, JSON.stringify(newData));
|
localStorage.setItem(SETTINGS_KEY, JSON.stringify(newData));
|
||||||
}
|
}
|
||||||
@@ -150,18 +122,18 @@ export const getSettings = (): IAppSettings[] => {
|
|||||||
export const getActiveSettings = (): IAppSettings => {
|
export const getActiveSettings = (): IAppSettings => {
|
||||||
const str = localStorage.getItem(SETTINGS_KEY);
|
const str = localStorage.getItem(SETTINGS_KEY);
|
||||||
if (str) {
|
if (str) {
|
||||||
const data: { active: boolean; encoded: string; id: number }[] =
|
const parsed: { active: boolean; encoded: string; id: number }[] =
|
||||||
JSON.parse(str);
|
JSON.parse(str);
|
||||||
const decoded: IAppSettings[] = data.map((el) => {
|
|
||||||
return {
|
const activeSettings = parsed.find((el) => el.active);
|
||||||
active: el.active,
|
const decoded = {
|
||||||
decoded: JSON.parse(
|
active: activeSettings?.active,
|
||||||
Buffer.from(el.encoded, "base64").toString("utf-8")
|
decoded: JSON.parse(
|
||||||
),
|
Buffer.from(activeSettings?.encoded!, "base64").toString("utf-8")
|
||||||
id: el.id,
|
),
|
||||||
};
|
id: activeSettings?.id,
|
||||||
});
|
};
|
||||||
return decoded.find((el) => el.active) as IAppSettings;
|
return decoded as IAppSettings;
|
||||||
}
|
}
|
||||||
return {} as IAppSettings;
|
return {} as IAppSettings;
|
||||||
};
|
};
|
||||||
@@ -175,24 +147,12 @@ export const saveAddvancedSettings = (settings: AdvancedAppSettings) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const str = localStorage.getItem(ADVANCED_SETTINGS_KET);
|
const str = localStorage.getItem(ADVANCED_SETTINGS_KET);
|
||||||
|
const data = str ? JSON.parse(str) : [];
|
||||||
|
|
||||||
if (str) {
|
if (data.some((el: { encoded: string }) => el.encoded === encoded)) return;
|
||||||
const data = JSON.parse(str);
|
|
||||||
const alreadyExists = data.filter(
|
|
||||||
(el: { encoded: string; active: boolean }) => el.encoded === encoded
|
|
||||||
);
|
|
||||||
if (!!alreadyExists.length) return;
|
|
||||||
|
|
||||||
localStorage.setItem(
|
data.push({ encoded, active: data.length === 0, id: data.length + 1 });
|
||||||
ADVANCED_SETTINGS_KET,
|
localStorage.setItem(ADVANCED_SETTINGS_KET, JSON.stringify(data));
|
||||||
JSON.stringify([...data, { encoded, active: false, id: data.length + 1 }])
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
localStorage.setItem(
|
|
||||||
ADVANCED_SETTINGS_KET,
|
|
||||||
JSON.stringify([{ encoded, active: true, id: 1 }])
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAdvancedSettings = (): IAdvancedAppSettings[] => {
|
export const getAdvancedSettings = (): IAdvancedAppSettings[] => {
|
||||||
|
|||||||
Reference in New Issue
Block a user