Skip to content

Commit eb2cd11

Browse files
authored
Abstract into settings-store and fix component-name assignment (#741)
1 parent a01bc69 commit eb2cd11

8 files changed

Lines changed: 96 additions & 69 deletions

File tree

extension/src/components/Graph.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export function GraphVisualization({
4545
x: 0,
4646
y: 0,
4747
depth: currentDepth,
48-
componentName: update.componentName || undefined,
4948
});
5049
}
5150

@@ -73,7 +72,6 @@ export function GraphVisualization({
7372
x: 0,
7473
y: 0,
7574
depth: currentDepth + 1, // Components are one level deeper than the signal that triggers them
76-
componentName: componentName,
7775
});
7876
}
7977

extension/src/components/SettingsPanel.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
import { Signal, useSignal, useSignalEffect } from "@preact/signals";
1+
import { useSignal, useSignalEffect } from "@preact/signals";
22
import { Button } from "./Button";
33
import { Settings } from "../types";
4+
import { settingsStore } from "../models/SettingsModel";
45

56
interface SettingsPanelProps {
6-
isVisible: Signal<boolean>;
7-
settings: Signal<Settings>;
7+
isVisible: boolean;
88
onApply: (settings: Settings) => void;
99
onCancel: () => void;
1010
}
1111

1212
export function SettingsPanel({
1313
isVisible,
14-
settings,
1514
onApply,
1615
onCancel,
1716
}: SettingsPanelProps) {
18-
const localSettings = useSignal<Settings>(settings.value);
17+
const settings = settingsStore.settings;
18+
const localSettings = useSignal<Settings>(settings);
1919

2020
useSignalEffect(() => {
21-
localSettings.value = settings.value;
21+
localSettings.value = settingsStore.settings;
2222
});
2323

2424
const handleApply = () => {
2525
onApply(localSettings.value);
2626
};
2727

28-
if (!isVisible.value) {
28+
if (!isVisible) {
2929
return null;
3030
}
3131

extension/src/components/UpdateItem.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,16 @@ export function UpdateItem({ update }: UpdateItemProps) {
3434
<div className="update-header">
3535
<span className="signal-name">
3636
{depth}↪️ {update.signalName}
37-
{update.componentName && (
38-
<span className="component-name"> in {update.componentName}</span>
37+
{update.componentNames && update.componentNames.length > 0 && (
38+
<ul class="component-list">
39+
<span class="component-name-header">Rerendered</span>
40+
{update.componentNames?.map((componentName, i) => (
41+
<li key={componentName} class="component-name">
42+
{componentName}
43+
{i < update.componentNames!.length - 1 ? ", " : ""}
44+
</li>
45+
))}
46+
</ul>
3947
)}
4048
</span>
4149
<span className="update-time">{time}</span>

extension/src/models/ConnectionModel.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const createConnectionModel = () => {
4242
switch (type) {
4343
case "SIGNALS_AVAILABILITY":
4444
isConnected.value = payload.available;
45+
connectionStore.isContentScriptConnected = true;
4546
break;
4647

4748
case "CONNECTION_LOST":
@@ -64,9 +65,6 @@ const createConnectionModel = () => {
6465
case "CONTENT_SCRIPT_DISCONNECTED":
6566
connectionStore.isContentScriptConnected = false;
6667
break;
67-
68-
default:
69-
console.log("Unhandled message type:", type);
7068
}
7169
};
7270

@@ -75,8 +73,6 @@ const createConnectionModel = () => {
7573
});
7674

7775
effect(() => {
78-
if (isConnected.value) status.value = "connected";
79-
8076
if (!isBackgroundConnected.value) {
8177
status.value = "disconnected";
8278
} else if (!isContentScriptConnected.value) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { signal, effect } from "@preact/signals";
2+
import { Settings } from "../types";
3+
4+
const createSettingsModel = () => {
5+
const settings = signal<Settings>({
6+
enabled: true,
7+
grouped: true,
8+
maxUpdatesPerSecond: 60,
9+
filterPatterns: [],
10+
});
11+
12+
const showSettings = signal<boolean>(false);
13+
14+
const applySettings = (newSettings: Settings) => {
15+
settings.value = newSettings;
16+
window.postMessage(
17+
{
18+
type: "CONFIGURE_DEBUG",
19+
payload: newSettings,
20+
},
21+
"*"
22+
);
23+
showSettings.value = false;
24+
};
25+
26+
const toggleSettings = () => {
27+
showSettings.value = !showSettings.value;
28+
};
29+
30+
const hideSettings = () => {
31+
showSettings.value = false;
32+
};
33+
34+
effect(() => {
35+
const handleMessage = (event: MessageEvent) => {
36+
// Only accept messages from the same origin (devtools context)
37+
if (event.origin !== window.location.origin) return;
38+
39+
const { type, payload } = event.data;
40+
41+
switch (type) {
42+
case "SIGNALS_CONFIG":
43+
settings.value = payload.settings;
44+
break;
45+
}
46+
};
47+
48+
window.addEventListener("message", handleMessage);
49+
return () => window.removeEventListener("message", handleMessage);
50+
});
51+
52+
return {
53+
get settings() {
54+
return settings.value;
55+
},
56+
get showSettings() {
57+
return showSettings.value;
58+
},
59+
// Actions
60+
set settings(newSettings: Settings) {
61+
settings.value = newSettings;
62+
},
63+
applySettings,
64+
toggleSettings,
65+
hideSettings,
66+
};
67+
};
68+
69+
export const settingsStore = createSettingsModel();

extension/src/panel.tsx

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,25 @@
11
import { render } from "preact";
2-
import { useSignal, useSignalEffect } from "@preact/signals";
2+
import { useSignal } from "@preact/signals";
33
import { EmptyState } from "./components/EmptyState";
44
import { Header } from "./components/Header";
55
import { SettingsPanel } from "./components/SettingsPanel";
6-
import { Settings } from "./types";
76
import { GraphVisualization } from "./components/Graph";
87
import { updatesStore } from "./models/UpdatesModel";
98
import { UpdatesContainer } from "./components/UpdatesContainer";
10-
import { connectionStore, sendMessage } from "./models/ConnectionModel";
9+
import { connectionStore } from "./models/ConnectionModel";
10+
import { settingsStore } from "./models/SettingsModel";
1111

1212
function SignalsDevToolsPanel() {
13-
const showSettings = useSignal(false);
1413
const activeTab = useSignal<"updates" | "graph">("updates");
1514

16-
// TODO: settings model
17-
const settings = useSignal<Settings>({
18-
enabled: true,
19-
grouped: true,
20-
maxUpdatesPerSecond: 60,
21-
filterPatterns: [],
22-
});
23-
24-
const toggleSettings = () => {
25-
showSettings.value = !showSettings.value;
26-
};
27-
28-
const applySettings = (newSettings: Settings) => {
29-
settings.value = newSettings;
30-
sendMessage({
31-
type: "CONFIGURE_DEBUG",
32-
payload: newSettings,
33-
});
34-
showSettings.value = false;
35-
};
36-
37-
useSignalEffect(() => {
38-
const handleMessage = (event: MessageEvent) => {
39-
// Only accept messages from the same origin (devtools context)
40-
if (event.origin !== window.location.origin) return;
41-
42-
const { type, payload } = event.data;
43-
44-
switch (type) {
45-
case "SIGNALS_CONFIG":
46-
settings.value = payload.settings;
47-
break;
48-
}
49-
};
50-
51-
window.addEventListener("message", handleMessage);
52-
return () => window.removeEventListener("message", handleMessage);
53-
});
54-
5515
return (
5616
<div id="app">
57-
<Header onToggleSettings={toggleSettings} />
17+
<Header onToggleSettings={settingsStore.toggleSettings} />
5818

5919
<SettingsPanel
60-
isVisible={showSettings}
61-
settings={settings}
62-
onApply={applySettings}
63-
onCancel={() => (showSettings.value = false)}
20+
isVisible={settingsStore.showSettings}
21+
onApply={settingsStore.applySettings}
22+
onCancel={settingsStore.hideSettings}
6423
/>
6524

6625
<main className="main-content">

extension/src/types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ export interface SignalUpdate {
33
signalType: "signal" | "computed" | "effect";
44
signalName: string;
55
signalId?: string;
6-
componentName?: string | null;
7-
componentNames?: string[];
6+
componentNames: string[];
87
prevValue?: any;
98
newValue?: any;
109
timestamp?: number;
@@ -32,7 +31,6 @@ export interface GraphNode {
3231
x: number;
3332
y: number;
3433
depth: number;
35-
componentName?: string;
3634
}
3735

3836
export interface GraphLink {

packages/debug/src/devtools.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ class DevToolsCommunicator {
117117
: "signal",
118118
signalName: this.getSignalName(signal),
119119
signalId: this.getSignalId(signal),
120-
componentName: owners.length > 0 ? owners[0] : null,
121-
componentNames: owners.length > 1 ? owners : undefined,
120+
componentNames: owners.length > 0 ? owners : undefined,
122121
};
123122
}),
124123
},

0 commit comments

Comments
 (0)