mirror of
https://github.com/muerwre/markdown-home-tab.git
synced 2025-04-25 00:46:41 +07:00
remove chrome storage
This commit is contained in:
parent
b5d12750cb
commit
6ede9f157e
3 changed files with 10 additions and 112 deletions
|
@ -1,6 +1,6 @@
|
|||
import { SerializedDockview } from "dockview";
|
||||
import { SettingsValue } from "~/modules/settings/context/SettingsContext";
|
||||
import { hasBrowserStorage, hasChromeStorage } from "~/utils/storage";
|
||||
import { hasBrowserStorage } from "~/utils/storage";
|
||||
|
||||
interface Result {
|
||||
layout: SerializedDockview;
|
||||
|
@ -43,35 +43,6 @@ const getFromBrowserStorage = async (): Promise<Result | null> => {
|
|||
};
|
||||
};
|
||||
|
||||
const getFromChromeStorage = async (): Promise<Result | null> => {
|
||||
const result = await chrome.storage.sync.get();
|
||||
const layout = result[layoutKey] as SerializedDockview | undefined;
|
||||
|
||||
if (!layout) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const panels = Object.keys(layout.panels).reduce(
|
||||
(acc, uuid) => ({
|
||||
...acc,
|
||||
[uuid]: (result[makePanelKey(uuid)] as string) ?? "",
|
||||
}),
|
||||
{} as Record<string, string>
|
||||
);
|
||||
|
||||
const settings =
|
||||
typeof result[settingsKey] === "object" &&
|
||||
Object.keys(result[settingsKey]).length
|
||||
? result[settingsKey]
|
||||
: {};
|
||||
|
||||
return {
|
||||
layout,
|
||||
panels,
|
||||
settings,
|
||||
};
|
||||
};
|
||||
|
||||
const getFromLocalStorage = () => {
|
||||
const rawLayout = localStorage.getItem(layoutKey);
|
||||
|
||||
|
@ -121,10 +92,6 @@ export const hydrateLayout = async (): Promise<Result | null> => {
|
|||
return getFromBrowserStorage();
|
||||
}
|
||||
|
||||
if (hasChromeStorage()) {
|
||||
return getFromChromeStorage();
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
|
@ -132,37 +99,30 @@ export const storeLayoutLocally = (layout: SerializedDockview) =>
|
|||
localStorage.setItem(layoutKey, JSON.stringify(layout));
|
||||
|
||||
export const storeLayoutInSync = (layout: SerializedDockview) => {
|
||||
if (hasBrowserStorage()) {
|
||||
return browser.storage.sync.set({ [layoutKey]: layout });
|
||||
if (!hasBrowserStorage()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (hasChromeStorage()) {
|
||||
return chrome.storage.sync.set({ [layoutKey]: layout });
|
||||
}
|
||||
return browser.storage.sync.set({ [layoutKey]: layout });
|
||||
};
|
||||
|
||||
export const storePanelLocally = (uuid: string, value: string) =>
|
||||
localStorage.setItem(`${panelPrefix}${uuid}`, value);
|
||||
|
||||
export const storePanelInSync = (uuid: string, value: string) => {
|
||||
if (hasBrowserStorage()) {
|
||||
return browser.storage.sync.set({ [`${panelPrefix}${uuid}`]: value });
|
||||
if (!hasBrowserStorage()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (hasChromeStorage()) {
|
||||
return chrome.storage.sync.set({ [`${panelPrefix}${uuid}`]: value });
|
||||
}
|
||||
return browser.storage.sync.set({ [`${panelPrefix}${uuid}`]: value });
|
||||
};
|
||||
|
||||
export const storeSettingsLocally = (settings: Partial<SettingsValue>) =>
|
||||
localStorage.setItem(settingsKey, JSON.stringify(settings));
|
||||
|
||||
export const storeSettingsInSync = (settings: Partial<SettingsValue>) => {
|
||||
if (hasBrowserStorage()) {
|
||||
return browser.storage.sync.set({ [settingsKey]: settings });
|
||||
}
|
||||
|
||||
if (hasChromeStorage()) {
|
||||
return chrome.storage.sync.set({ [settingsKey]: settings });
|
||||
if (!hasBrowserStorage()) {
|
||||
return;
|
||||
}
|
||||
return browser.storage.sync.set({ [settingsKey]: settings });
|
||||
};
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
import { hasBrowserStorage, hasChromeStorage } from "./storage";
|
||||
|
||||
export class BrowserSyncStorage {
|
||||
constructor(private globalPrefix = "") {}
|
||||
|
||||
get engine() {
|
||||
if (hasBrowserStorage()) {
|
||||
return "browser";
|
||||
} else if (hasChromeStorage()) {
|
||||
return "chrome";
|
||||
}
|
||||
|
||||
return "local";
|
||||
}
|
||||
|
||||
makeKey = (key: string) => `${this.globalPrefix}${key}`;
|
||||
|
||||
set = async <T>(key: string, value: T) => {
|
||||
switch (this.engine) {
|
||||
case "browser":
|
||||
await browser.storage.sync.set({ [this.makeKey(key)]: value });
|
||||
return;
|
||||
case "chrome":
|
||||
await chrome.storage.sync.set({ [this.makeKey(key)]: value });
|
||||
return;
|
||||
default:
|
||||
localStorage.setItem(this.makeKey(key), JSON.stringify(value));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
get = async <T>(key: string): Promise<T | undefined> => {
|
||||
if (this.engine === "browser") {
|
||||
const value = await browser.storage.sync
|
||||
.get([this.makeKey(key)])
|
||||
.then((result) => result[this.makeKey(key)] as T | undefined);
|
||||
|
||||
if (value) {
|
||||
return value;
|
||||
}
|
||||
} else if (this.engine === "chrome") {
|
||||
const value = await chrome.storage.sync
|
||||
.get(this.makeKey(key))
|
||||
.then((result) => result[this.makeKey(key)] as T | undefined);
|
||||
|
||||
if (value) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const value = localStorage.getItem(this.makeKey(key));
|
||||
return value ? (JSON.parse(value) as T) : undefined;
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
}
|
|
@ -1,5 +1,2 @@
|
|||
export const hasBrowserStorage = () =>
|
||||
typeof browser !== "undefined" && browser?.storage;
|
||||
|
||||
export const hasChromeStorage = () =>
|
||||
typeof chrome !== "undefined" && chrome?.storage;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue