8000 Fix remounting root for devpanel by Methuselah96 · Pull Request #1302 · reduxjs/redux-devtools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix remounting root for devpanel #1302

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

Merged
merged 2 commits into from
Dec 26, 2022
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
5 changes: 5 additions & 0 deletions .changeset/seven-turkeys-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'remotedev-redux-devtools-extension': patch
---

Fix remounting root for devpanel
29 changes: 16 additions & 13 deletions extension/src/devpanel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { CSSProperties } from 'react';
import React, { CSSProperties, ReactNode } from 'react';
import { createRoot, Root } from 'react-dom/client';
import { Provider } from 'react-redux';
import { Persistor } from 'redux-persist';
Expand All @@ -20,18 +20,24 @@ const messageStyle: CSSProperties = {
};

let rendered: boolean | undefined;
let currentRoot: Root | undefined;
let store: Store<StoreStateWithoutSocket, StoreAction> | undefined;
let persistor: Persistor | undefined;
let bgConnection: chrome.runtime.Port;
let naTimeout: NodeJS.Timeout;

const isChrome = navigator.userAgent.indexOf('Firefox') === -1;

function renderDevTools(root: Root) {
root.unmount();
function renderNodeAtRoot(node: ReactNode) {
if (currentRoot) currentRoot.unmount();
currentRoot = createRoot(document.getElementById('root')!);
currentRoot.render(node);
}

function renderDevTools() {
clearTimeout(naTimeout);
({ store, persistor } = configureStore(position, bgConnection));
root.render(
renderNodeAtRoot(
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<App position={position} />
Expand All @@ -41,7 +47,7 @@ function renderDevTools(root: Root) {
rendered = true;
}

function renderNA(root: Root) {
function renderNA() {
if (rendered === false) return;
rendered = false;
naTimeout = setTimeout(() => {
Expand Down Expand Up @@ -74,31 +80,28 @@ function renderNA(root: Root) {
);
}

root.unmount();
root.render(message);
renderNodeAtRoot(message);
store = undefined;
});
} else {
root.unmount();
root.render(message);
renderNodeAtRoot(message);
store = undefined;
}
}, 3500);
}

function init(id: number) {
const root = createRoot(document.getElementById('root')!);
renderNA(root);
renderNA();
bgConnection = chrome.runtime.connect({
name: id ? id.toString() : undefined,
});
bgConnection.onMessage.addListener(
<S, A extends Action<unknown>>(message: PanelMessage<S, A>) => {
if (message.type === 'NA') {
if (message.id === id) renderNA(root);
if (message.id === id) renderNA();
else store!.dispatch({ type: REMOVE_INSTANCE, id: message.id });
} else {
if (!rendered) renderDevTools(root);
if (!rendered) renderDevTools();
store!.dispatch(message);
}
}
Expand Down
0