Skip to content

Commit

Permalink
Add: Rewrite db structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayao0819 committed Dec 3, 2023
1 parent da21cfb commit b616556
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 39 deletions.
8 changes: 6 additions & 2 deletions lib/class/Storage/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ export default class BrowserStorage<T> implements StorageTool<T> {
await this.#item.set({ ...data, [key]: newdata });
}

async getAll() {
return await this.#item.get();
}

async get(value: keyof T) {
return (await this.#item.get())[value];
return (await this.getAll())[value];
}

async set(value: T) {
async set(value: Partial<T>) {
const data = await this.#item.get();

await this.#item.set({ ...data, value });
Expand Down
28 changes: 28 additions & 0 deletions lib/class/Storage/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { WebsiteIds } from "@/data/websites";

import BrowserStorage from "./browser";

export interface UnivCommonConfig {
rainbow?: boolean;
dark?: boolean;
}

export type CommonConfig = Record<WebsiteIds, UnivCommonConfig>;

export const CommonStorage = new (class {
#storage: BrowserStorage<CommonConfig>;
constructor() {
this.#storage = new BrowserStorage("common");
}

async get(id: WebsiteIds, key: keyof UnivCommonConfig) {
const isRainbowEnabled = (await this.#storage.get(id))[key];
return isRainbowEnabled;
}

async set(id: WebsiteIds, value: Partial<UnivCommonConfig>) {
const data = await this.#storage.getAll();

await this.#storage.set({ ...data, [id]: { ...data[id], ...value } });
}
})();
3 changes: 2 additions & 1 deletion lib/class/Storage/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export type StorageKeys =
| "installed"
| "auto-2fa"
| "quick-switch";
export type StorageIds = WebsiteIds | "other";

export type StorageIds = "common" | "other" | WebsiteIds;
25 changes: 10 additions & 15 deletions lib/class/UnivWebsite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@ import { WebsiteIds } from "@/data/websites";
import isTrue from "../../utils/isTrue";
import { DarkApplicator, HiddenApplicator, RainbowApplicator } from "../ClassApplicator";
import BrowserStorage from "../Storage/browser";
import { CommonStorage } from "../Storage/common";
import { OtherStorage } from "../Storage/other";
import StorageTool from "../Storage/storage";

export interface UnivConfig {
rainbow?: boolean;
dark?: boolean;
}

// UnivWebSiteはゲーミング化するウェブサイトを定義したクラス
// 型変数とoptionsプロパティによって任意の情報を追加できる
export class UnivWebsite<T = unknown, U extends UnivConfig = UnivConfig> {
export class UnivWebsite<T = unknown, U = unknown> {
// 基本情報
id: WebsiteIds;

storage: StorageTool<U>;

// Applicator
Expand Down Expand Up @@ -50,19 +45,19 @@ export class UnivWebsite<T = unknown, U extends UnivConfig = UnivConfig> {
}

async initilizeWebsiteDb() {
await this.storage.set({
await CommonStorage.set(this.id, {
rainbow: false,
dark: false,
});
}

async isRainbowEnabled() {
const isRainbowEnabled = await this.storage.get("rainbow");
const isRainbowEnabled = await CommonStorage.get(this.id, "rainbow");
return isTrue(isRainbowEnabled);
}

async isDarkEnabled() {
const isDarkEnabled = await this.storage.get("dark");
const isDarkEnabled = await CommonStorage.get(this.id, "dark");
return isTrue(isDarkEnabled);
}

Expand All @@ -77,23 +72,23 @@ export class UnivWebsite<T = unknown, U extends UnivConfig = UnivConfig> {
if (isRainbowEnabled) {
// CSSのためにHTML要素にデータ属性を追加
document.documentElement.dataset.gaming_gundai = "true";
this.storage.set({ rainbow: true });
CommonStorage.set(this.id, { rainbow: true });
this.rainbow.enable();
} else {
// CSSのためにHTML要素にデータ属性を追加
document.documentElement.dataset.gaming_gundai = "false";
this.storage.set({ rainbow: false });
CommonStorage.set(this.id, { rainbow: false });
this.rainbow.disable();
}

const isDarkEnabled = await this.isDarkEnabled();
if (isDarkEnabled) {
document.documentElement.dataset.gaming_gundai_dark = "true";
this.storage.set({ dark: true });
CommonStorage.set(this.id, { dark: true });
this.dark.enable();
} else {
document.documentElement.dataset.gaming_gundai_dark = "false";
this.storage.set({ dark: false });
CommonStorage.set(this.id, { dark: false });
this.dark.disable();
}

Expand All @@ -117,4 +112,4 @@ export class UnivWebsite<T = unknown, U extends UnivConfig = UnivConfig> {
}
}

export class GundaiWebSite<T> extends UnivWebsite<T> {}
export class GundaiWebSite<T = unknown, U = unknown> extends UnivWebsite<T, U> {}
20 changes: 0 additions & 20 deletions lib/class/UnivWebsite/test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion lib/components/ToggleWithStorage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Toggle } from "react-daisyui";
import { ComponentColor } from "react-daisyui/dist/types";

import { StorageTool } from "@/class";
import { UnivCommonConfig as UnivConfig } from "@/class/Storage/common";
import { OtherConfig, OtherStorage } from "@/class/Storage/other";
import { UnivConfig } from "@/class/UnivWebsite";
import IsTrue from "@/utils/isTrue";
import { sendMsgToAllTab } from "@/utils/sendMsgToAllTab";

Expand Down

0 comments on commit b616556

Please sign in to comment.