Skip to content

Commit

Permalink
feat: enhanceLocalStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
aelf-lxy committed Aug 7, 2024
1 parent 7479a8f commit 0c101a5
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/login/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aelf-web-login",
"version": "2.1.3",
"version": "2.1.4",
"main": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
"exports": {
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GlobalConfigProps as GlobalConfigPropsV1 } from '@portkey-v1/did-ui-rea
import { GlobalConfigProps } from '@portkey/did-ui-react/dist/_types/src/components/config-provider/types';
import { EventEmitter } from 'ahooks/lib/useEventEmitter';
import { PortkeyDidV1, PortkeyDid } from './index';
import { enhancedLocalStorage as localStorage } from './utils/enhancedLocalStorage';

// copy from @aelf-react/core, cause it's not exported
export type AelfNode = {
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import clsx from 'clsx';
import { PortkeyDid, PortkeyDidV1 } from './index';
import { getStorageVersion } from './utils/getUrl';
import CommonErrorBoundary from './components/ErrorBoundary';
import { enhancedLocalStorage as localStorage } from './utils/enhancedLocalStorage';

const INITIAL_STATE = {
loginState: WebLoginState.initial,
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/hooks/useCallContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { SendOptions } from '@portkey/types';
import { SendOptions as SendOptionsV1 } from '@portkey-v1/types';
import useWebLoginEvent from './useWebLoginEvent';
import { getFaviconUrl, getStorageVersion, getUrl } from '../utils/getUrl';
import { enhancedLocalStorage as localStorage } from '../utils/enhancedLocalStorage';

const getAElfInstance = (() => {
const instances = new Map<string, any>();
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/hooks/useGetAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ChainId } from '@portkey/provider-types';
import { did } from '@portkey/did-ui-react';
import { did as didV1 } from '@portkey-v1/did-ui-react';
import { getStorageVersion } from '../utils/getUrl';
import { enhancedLocalStorage as localStorage } from '../utils/enhancedLocalStorage';

export default function useGetAccount(chainId: string) {
const WEB_LOGIN_VERSION = getStorageVersion();
Expand Down
44 changes: 44 additions & 0 deletions packages/login/src/utils/enhancedLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export interface Storage {
getItem(key: string, ...args: Array<any>): any;
setItem(key: string, value: any, ...args: Array<any>): any;
removeItem(key: string, ...args: Array<any>): any;
clear(): void;
}

const localStorageMock = (function () {
let store: { [key: string]: string } = {};

return {
getItem: function (key: string): string | null {
return store[key] || null;
},
setItem: function (key: string, value: string) {
store[key] = value.toString();
},
removeItem: function (key: string) {
delete store[key];
},
clear: function () {
store = {};
},
};
})();

// let enhancedLocalStorage: Storage = {} as Storage;
// if (typeof window !== 'undefined') {
// enhancedLocalStorage = window.localStorage;
// } else {
// import('node-localstorage').then(({ LocalStorage }) => {
// enhancedLocalStorage = new LocalStorage('./scratch');
// });
// }
// export { enhancedLocalStorage };

let enhancedLocalStorage: Storage;
if (typeof window !== 'undefined') {
console.log('enhancedLocalStorage in window');
enhancedLocalStorage = window.localStorage;
} else {
enhancedLocalStorage = localStorageMock;
}
export { enhancedLocalStorage, localStorageMock };
1 change: 1 addition & 0 deletions packages/login/src/utils/getUrl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LOGIN_VERSION } from '../constants';
import { getConfig } from '../config';
import { enhancedLocalStorage as localStorage } from './enhancedLocalStorage';

export function getUrl() {
return new URL(location.href);
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/discover/detectProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import detectProvider from '@portkey/detect-provider';
import { IPortkeyProvider } from '@portkey/provider-types';
import { getStorageVersion } from '../../utils/getUrl';
import { enhancedLocalStorage as localStorage } from '../../utils/enhancedLocalStorage';

export default async function detectDiscoverProvider(version?: string | null): Promise<IPortkeyProvider | null> {
const WEB_LOGIN_VERSION = getStorageVersion();
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/discover/useDiscover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import useWebLoginEvent from '../../hooks/useWebLoginEvent';
import { removeOtherKey, useWebLogin } from '../../context';
import isPortkeyApp, { isPortkeyV2 } from '../../utils/isPortkeyApp';
import { getStorageVersion } from '../../utils/getUrl';
import { enhancedLocalStorage as localStorage } from '../../utils/enhancedLocalStorage';

export type DiscoverDetectState = 'unknown' | 'detected' | 'not-detected';
export type DiscoverInterface = WalletHookInterface & {
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/elf/useElf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import checkSignatureParams from '../../utils/signatureParams';
import detectNightElf from './detectNightElf';
import { zeroFill } from '../../utils/zeroFill';
import { removeOtherKey } from '../../context';
import { enhancedLocalStorage as localStorage } from '../../utils/enhancedLocalStorage';

export function useElf({
options,
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/portkey/Portkey/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { changePortkeyVersion } from '../../../utils/isPortkeyApp';
import isMobile from '../../../utils/isMobile';
import { useWebLogin } from '../../../context';
import { getStorageVersion } from '../../../utils/getUrl';
import { enhancedLocalStorage as localStorage } from '../../../utils/enhancedLocalStorage';

interface IPortkeyPanelProps {
open: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/portkey/Portkey/indexV1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { FetchRequest } from '@portkey-v1/request';
import { changePortkeyVersion } from '../../../utils/isPortkeyApp';
import isMobile from '../../../utils/isMobile';
import { getStorageVersion } from '../../../utils/getUrl';
import { enhancedLocalStorage as localStorage } from '../../../utils/enhancedLocalStorage';

export default function Portkey({
open,
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/portkey/usePortkey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { addPrefix } from '../../../utils/getDidAndVersion';
import { aes } from '@portkey/utils';
import useTelegram from './useTelegram';
import { NetworkType } from '@portkey/provider-types';
import { enhancedLocalStorage as localStorage } from '../../../utils/enhancedLocalStorage';

export type PortkeyInterface = WalletHookInterface & {
isManagerExists: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/login/src/wallets/portkey/usePortkey/indexV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import checkSignatureParams from '../../../utils/signatureParams';
import { PortkeyOptions } from 'src/types';
import { sendAdapter } from '../../../hooks/useCallContract';
import { message } from 'antd';
import { enhancedLocalStorage as localStorage } from '../../../utils/enhancedLocalStorage';

export type PortkeyInterface = WalletHookInterface & {
isManagerExists: boolean;
Expand Down

0 comments on commit 0c101a5

Please sign in to comment.