Skip to content

Commit

Permalink
feat(storage): support state data synchronization with storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaewoook committed Jun 23, 2024
1 parent 138bc13 commit 59dbd93
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/shared/models/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type DisplayOptions = {
engAddrShown: boolean;
roadAddrShown: boolean;
streetNumAddrShown: boolean;
};
4 changes: 3 additions & 1 deletion src/shared/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { isExtension } from "./utils";

let browser: Browser | null = null;
if (isExtension()) {
browser = await import("webextension-polyfill");
import("webextension-polyfill").then((module) => {
browser = module.default;
});
}

type SearchResultOptions = {
Expand Down
14 changes: 14 additions & 0 deletions src/states/address.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { atom } from "recoil";

import { getRecentAddressList, setRecentAddressList } from "@/shared/storage";
import type { AddressData } from "@/shared/models/address";

export const addressListState = atom<AddressData[]>({
key: "address-list",
default: [],
effects: [({ trigger, setSelf, onSet }) => {
if (trigger === "get") {
getRecentAddressList()?.then((recentAddressList) => {
if (recentAddressList) {
setSelf(recentAddressList);
}
});
}

onSet((newAddressList) => {
setRecentAddressList(newAddressList);
});
}],
});
28 changes: 21 additions & 7 deletions src/states/settings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { atom } from "recoil";

import { getSearchResultSettings, validateSettingsData, DEFAULT_SETTINGS } from "@/shared/storage";
import {
DEFAULT_SETTINGS,
getSearchResultOptions,
setSearchResultOptions,
validateSettingsData,
} from "@/shared/storage";
import type { DisplayOptions } from "@/shared/models/settings";

export const addressDisplayOptionsState = atom({
export const addressDisplayOptionsState = atom<DisplayOptions>({
key: "display-options",
default: {
engAddrShown: true,
Expand All @@ -11,17 +17,25 @@ export const addressDisplayOptionsState = atom({
},
effects: [({ trigger, setSelf, onSet }) => {
if (trigger === "get") {
getSearchResultSettings()?.then((settings) => {
if (!validateSettingsData(settings, DEFAULT_SETTINGS.searchResult)) {
getSearchResultOptions()?.then((options) => {
if (!options || !validateSettingsData(options, DEFAULT_SETTINGS.searchResult)) {
return;
}

setSelf({
engAddrShown: settings.showEng,
roadAddrShown: settings.showRoad,
streetNumAddrShown: settings.showLegacy,
engAddrShown: options.showEng,
roadAddrShown: options.showRoad,
streetNumAddrShown: options.showLegacy,
});
});
}

onSet((newOptions) => {
setSearchResultOptions({
showEng: newOptions.engAddrShown,
showRoad: newOptions.roadAddrShown,
showLegacy: newOptions.streetNumAddrShown,
});
});
}],
});

0 comments on commit 59dbd93

Please sign in to comment.