From fd8dfdbf3fe8aa5c424d8b5eefe60a4a3b7bc9c7 Mon Sep 17 00:00:00 2001 From: Robby6Strings Date: Sun, 14 Apr 2024 04:37:27 +1200 Subject: [PATCH] devtools improvements - is now injected in kaioken module instead of user code, handles SSR better --- .../src/components/SelectedNodeView.tsx | 9 +-- packages/devtools-client/src/store.ts | 6 +- packages/devtools-client/src/utils.ts | 2 +- packages/devtools-host/src/App.tsx | 37 +++++---- packages/devtools-host/src/index.ts | 78 ++++++++++--------- packages/devtools-host/src/options.ts | 8 +- packages/devtools-host/src/store.ts | 15 ++-- packages/devtools-host/tsconfig.json | 2 +- packages/vite-plugin-kaioken/src/index.ts | 24 +++--- 9 files changed, 103 insertions(+), 78 deletions(-) diff --git a/packages/devtools-client/src/components/SelectedNodeView.tsx b/packages/devtools-client/src/components/SelectedNodeView.tsx index e12a70d3..3635fc7e 100644 --- a/packages/devtools-client/src/components/SelectedNodeView.tsx +++ b/packages/devtools-client/src/components/SelectedNodeView.tsx @@ -42,9 +42,8 @@ export function SelectedNodeView() { {selectedNode.hooks && (
{selectedNode.hooks.map((hookData) => { - const data = { ...hookData } - const name = data.name - delete data.name + const { name, debug, ...rest } = hookData + const data = debug ? debug() : rest return (
{name || "anonymous hook"} @@ -56,9 +55,7 @@ export function SelectedNodeView() {
{key}:{" "}
-                            {Array.isArray(value)
-                              ? JSON.stringify(value)
-                              : JSON.stringify(value, null, 2)}
+                            {JSON.stringify(value, null, 2)}
                           
) diff --git a/packages/devtools-client/src/store.ts b/packages/devtools-client/src/store.ts index 85d10811..06306f71 100644 --- a/packages/devtools-client/src/store.ts +++ b/packages/devtools-client/src/store.ts @@ -1,7 +1,11 @@ import { AppContext, createStore } from "kaioken" import { isDevtoolsApp } from "./utils" -export const kaiokenGlobal = window.opener.__kaioken as typeof window.__kaioken +export const kaiokenGlobal = + "window" in globalThis + ? (window.opener.__kaioken as typeof window.__kaioken) + : undefined + const initialApps = (kaiokenGlobal?.apps ?? []).filter( (app) => !isDevtoolsApp(app) ) diff --git a/packages/devtools-client/src/utils.ts b/packages/devtools-client/src/utils.ts index 7f7bdfed..a7c807e5 100644 --- a/packages/devtools-client/src/utils.ts +++ b/packages/devtools-client/src/utils.ts @@ -1,4 +1,4 @@ -import { AppContext } from "kaioken" +import type { AppContext } from "kaioken" export function isDevtoolsApp(app: AppContext) { return app.name === "kaioken.devtools" diff --git a/packages/devtools-host/src/App.tsx b/packages/devtools-host/src/App.tsx index fb9edeb0..32b25e57 100644 --- a/packages/devtools-host/src/App.tsx +++ b/packages/devtools-host/src/App.tsx @@ -1,26 +1,27 @@ -import { useEffect as __devtoolsUseEffect } from "kaioken" -import { __useDevtoolsStore } from "./store" +import { useEffect, useRef } from "kaioken" +import { useDevtoolsStore } from "./store" -function __devtoolsHandleDragOver(e: DragEvent) { - const dragging = __useDevtoolsStore.getState().dragging +function handleDragOver(e: DragEvent) { + const dragging = useDevtoolsStore.getState().dragging if (!dragging) return e.preventDefault() e.stopPropagation() e.dataTransfer && (e.dataTransfer.dropEffect = "move") } -export default function __DevtoolsApp() { +export default function App() { + const bgRef = useRef(null) const { value: { popupWindow, dragging }, setPopupWindow, setCorner, setDragging, - } = __useDevtoolsStore() + } = useDevtoolsStore() - __devtoolsUseEffect(() => { - document.body.addEventListener("dragover", __devtoolsHandleDragOver) + useEffect(() => { + document.body.addEventListener("dragover", handleDragOver) return () => { - document.body.removeEventListener("dragover", __devtoolsHandleDragOver) + document.body.removeEventListener("dragover", handleDragOver) } }, []) @@ -41,26 +42,26 @@ export default function __DevtoolsApp() { } function handleDragStart() { - __useDevtoolsStore.setState((prev) => ({ - ...prev, - dragging: true, - })) + setDragging(true) } function handleDrag(e: DragEvent) { + if (!bgRef.current?.isConnected) return e.preventDefault() e.stopPropagation() + if (!useDevtoolsStore.getState().dragging) return let isLeft = true, isTop = true - if (e.pageX > window.innerWidth / 2) isLeft = false - if (e.pageY > window.innerHeight / 2) isTop = false + if (e.offsetX > window.innerWidth / 2 + window.scrollX) isLeft = false + if (e.offsetY > window.innerHeight / 2 + window.scrollY) isTop = false const corner = isTop ? (isLeft ? "tl" : "tr") : isLeft ? "bl" : "br" setCorner(corner) } function handleDragEnd(e: DragEvent) { + if (!bgRef.current?.isConnected) return e.preventDefault() e.stopPropagation() setDragging(false) @@ -68,6 +69,12 @@ export default function __DevtoolsApp() { return ( <> + {dragging ? ( +
+ ) : null}