Skip to content

Commit

Permalink
Revert "feat: separate browser storage"
Browse files Browse the repository at this point in the history
This reverts commit 2cbc31c.
  • Loading branch information
acaldas committed Apr 16, 2024
1 parent 2cbc31c commit 15dee4f
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 44 deletions.
5 changes: 0 additions & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
},
"plugins": ["react", "@typescript-eslint"],
"rules": {
"no-restricted-imports": ["error", {
"name": "jotai/utils",
"importNames": ["atomWithStorage"],
"message": "Please use atomWithStorage from 'src/store/utils' instead."
}],
"react/prop-types": "off",
"react/react-in-jsx-scope": "off",
"@typescript-eslint/explicit-function-return-type": "off",
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useFeatureFlags/atom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { atomWithStorage } from 'src/store/utils';
import { atomWithStorage } from 'jotai/utils';
import defaultConfig, {
FEATURE_FLAG_KEY_STORAGE,
FeatureFlag,
Expand Down
6 changes: 1 addition & 5 deletions src/services/storage/browser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import connectConfig from 'connect-config';
import { BaseStorage, IStorage } from '.';

const store: IStorage = {
Expand All @@ -23,9 +22,6 @@ export class BrowserStorage<
T extends Record<string, unknown> = Record<string, unknown>,
> extends BaseStorage<T> {
constructor(namespace: string) {
super(
store as IStorage<T>,
`${connectConfig.routerBasename}:${namespace}`,
);
super(store as IStorage<T>, namespace);
}
}
2 changes: 1 addition & 1 deletion src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { atom } from 'jotai';
import { atomWithStorage } from 'src/store/utils';
import { atomWithStorage } from 'jotai/utils';

export const sidebarCollapsedAtom = atomWithStorage('sidebar-collapsed', false);
export const sidebarDisableHoverStyles = atom(false);
Expand Down
41 changes: 10 additions & 31 deletions src/store/utils.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,31 @@
import connectConfig from 'connect-config';
// eslint-disable-next-line no-restricted-imports
import { atomWithStorage as _atomWithStorage } from 'jotai/utils';
import type { SyncStorage } from 'jotai/vanilla/utils/atomWithStorage';

const namespace = connectConfig.routerBasename;

export const atomWithStorage = <T>(
key: string,
initialValue: T,
storage?: SyncStorage<T>,
options?: {
getOnInit?: boolean;
},
) =>
_atomWithStorage<T>(
`${namespace}:${key}`,
initialValue,
storage ? storage : undefined,
options,
);
import { atomWithStorage } from 'jotai/utils';

export const atomWithStorageCallback = <T>(
key: string,
initialValue: T,
callback: (value: T) => void,
callback: (value: T) => void
) =>
_atomWithStorage<T>(key, initialValue, {
atomWithStorage<T>(key, initialValue, {
getItem(key, initialValue) {
const value = localStorage.getItem(`${namespace}:${key}`);
const value = localStorage.getItem(key);
return value ? (JSON.parse(value) as T) : initialValue;
},
setItem(key, value) {
localStorage.setItem(`${namespace}:${key}`, JSON.stringify(value));
localStorage.setItem(key, JSON.stringify(value));
callback(value);
},
removeItem(key) {
localStorage.removeItem(`${namespace}:${key}`);
localStorage.removeItem(key);
},
subscribe(key, callback) {
if (typeof window.addEventListener === 'undefined') {
if (
typeof window?.addEventListener === 'undefined'
) {
return () => null;
}

function listener(e: StorageEvent) {
if (
e.storageArea === localStorage &&
e.key === `${namespace}:${key}`
) {
if (e.storageArea === localStorage && e.key === key) {
callback((e.newValue ?? '') as T);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/browser-document-drive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { documentModels } from 'src/store/document-model';

export const BrowserDocumentDriveServer = new DocumentDriveServer(
documentModels,
new BrowserStorage(connectConfig.routerBasename),
new BrowserStorage(),
);

BrowserDocumentDriveServer.initialize()
Expand Down

0 comments on commit 15dee4f

Please sign in to comment.