Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Debug] clear localstorage on reset config #1659

Merged
merged 8 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 28 additions & 8 deletions src/components/core/menu/mainMenu/MainMenuRight.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
import { FC } from 'react';
import { IS_TENDERLY_FORK } from 'libs/wagmi';
import { FC, useEffect, useState } from 'react';
import { useBreakpoints } from 'hooks/useBreakpoints';
import { MainMenuRightWallet } from 'components/core/menu/mainMenu/MainMenuRightWallet';
import { MainMenuRightNotifications } from 'components/core/menu/mainMenu/MainMenuRightNotifications';
import { Button } from 'components/common/button';
import { MainMenuRightBurger } from './MainMenuRightBurger';
import { useBurgerMenuItems } from './MainMenuRightBurger/useBurgerMenuItems';
import { MainMenuRightChainSelector } from './MainMenuRightChainSelector';
import { networks } from 'config';
import { MainMenuCart } from './MainMenuCart';
import { lsService } from 'services/localeStorage';
import { Link } from '@tanstack/react-router';
import config from 'config';

const TenderlyForkAlert = () => {
return IS_TENDERLY_FORK ? (
<Button variant="error" size="sm">
Fork
</Button>
) : null;
const [isDebugMode, setIsDebugMode] = useState(false);
useEffect(() => {
const checkDebugMode = () => {
if (!!lsService.getItem('imposterAccount')) return setIsDebugMode(true);
if (!!lsService.getItem('tenderlyRpc')) return setIsDebugMode(true);
if (!!lsService.getItem('configOverride')) return setIsDebugMode(true);
const api = lsService.getItem('carbonApi');
if (api && api !== config.carbonApi) return setIsDebugMode(true);
setIsDebugMode(false);
};
checkDebugMode();
window.addEventListener('storage', checkDebugMode);
return () => window.removeEventListener('storage', checkDebugMode);
}, []);

if (!isDebugMode) return;

return (
<Link
to="/debug"
className="bg-warning text-14 rounded-full px-16 py-8 text-black"
>
Debug Mode
</Link>
);
};

export const MainMenuRight: FC = () => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/debug/DebugConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export const DebugConfig = () => {
try {
if (!configOverride) {
setConfigOverride('');
localStorage.clear();
lsService.removeItem('configOverride');
window?.location.reload();
} else {
const parsedConfig = JSON.parse(configOverride || '');
const result = v.safeParse(v.partial(AppConfigSchema), parsedConfig);
if (result.success) {
for (let i = 0; i < localStorage.length; i++) {
localStorage.removeItem(localStorage.key(i)!);
}
localStorage.clear();
lsService.setItem('configOverride', parsedConfig);
window?.location.reload();
} else {
Expand Down
25 changes: 15 additions & 10 deletions src/utils/managedLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ export class ManagedLocalStorage<T> {
}
}

private emit(formattedId: string, newValue: string | null) {
// Reproduce "storage" event so it run also within the context of the same document
const event = new StorageEvent('storage', {
key: formattedId,
newValue: newValue,
// If this become needed in the app we can send oldValue too, but it requires an additional getItem() call
oldValue: null,
// eslint-disable-next-line no-restricted-globals
url: location.href,
});
window.dispatchEvent(event);
}

getItem = <K extends keyof T>(key: K): T[K] | undefined => {
const formattedId = this.keyFormatter(key);
const value = localStorage.getItem(formattedId);
Expand Down Expand Up @@ -48,21 +61,13 @@ export class ManagedLocalStorage<T> {

localStorage.setItem(formattedId, stringValue);

// Reproduce "storage" event so it run also within the context of the same document
const event = new StorageEvent('storage', {
key: formattedId,
newValue: stringValue,
// If this become needed in the app we can send oldValue too, but it requires an additional getItem() call
oldValue: null,
// eslint-disable-next-line no-restricted-globals
url: location.href,
});
window.dispatchEvent(event);
this.emit(formattedId, stringValue);
};

removeItem = <K extends keyof T>(key: K) => {
const formattedId = this.keyFormatter(key);
localStorage.removeItem(formattedId);
this.emit(formattedId, null);
};

migrateItems = () => {
Expand Down