Skip to content

Commit 56f7d58

Browse files
authored
chore: refactor to use run config state as an object for guide toolbar v2 (#885)
1 parent 1fc0b37 commit 56f7d58

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

packages/react/src/modules/guide/components/Toolbar/V2/V2.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
DisplayOption,
1717
GuidesListDisplaySelect,
1818
} from "./GuidesListDisplaySelect";
19-
import { detectToolbarParam } from "./helpers";
19+
import { getRunConfig } from "./helpers";
2020
import { useDraggable } from "./useDraggable";
2121
import {
2222
InspectionResult,
@@ -55,20 +55,19 @@ export const V2 = () => {
5555
const [guidesListDisplayOption, setGuidesListDisplayOption] =
5656
React.useState<DisplayOption>("only-displayable");
5757

58-
const [isVisible, setIsVisible] = React.useState(detectToolbarParam());
58+
const [runConfig, setRunConfig] = React.useState(() => getRunConfig());
5959
const [isCollapsed, setIsCollapsed] = React.useState(true);
6060

6161
React.useEffect(() => {
62-
if (!isVisible) {
63-
return;
62+
const isDebugging = client.store.state.debug?.debugging;
63+
if (runConfig?.isVisible && !isDebugging) {
64+
client.setDebug();
6465
}
6566

66-
client.setDebug();
67-
6867
return () => {
6968
client.unsetDebug();
7069
};
71-
}, [isVisible, client]);
70+
}, [runConfig, client]);
7271

7372
const containerRef = React.useRef<HTMLDivElement>(null);
7473
const { position, isDragging, handlePointerDown } = useDraggable({
@@ -79,7 +78,7 @@ export const V2 = () => {
7978
});
8079

8180
const result = useInspectGuideClientStore();
82-
if (!result) {
81+
if (!result || !runConfig?.isVisible) {
8382
return null;
8483
}
8584

@@ -122,7 +121,10 @@ export const V2 = () => {
122121

123122
<Stack gap="2">
124123
<Button
125-
onClick={() => setIsVisible(false)}
124+
onClick={() => {
125+
setRunConfig((curr) => ({ ...curr, isVisible: false }));
126+
client.unsetDebug();
127+
}}
126128
size="1"
127129
variant="soft"
128130
trailingIcon={{ icon: Undo2, "aria-hidden": true }}

packages/react/src/modules/guide/components/Toolbar/V2/helpers.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import { checkForWindow } from "../../../../../modules/core";
44
// it is present and set to true.
55
const TOOLBAR_QUERY_PARAM = "knock_guide_toolbar";
66

7-
export const detectToolbarParam = () => {
7+
export const getRunConfig = () => {
88
const win = checkForWindow();
99
if (!win || !win.location) {
10-
return false;
10+
return undefined;
1111
}
1212

1313
const urlSearchParams = new URLSearchParams(win.location.search);
14-
const hasToolbarParam = urlSearchParams.get(TOOLBAR_QUERY_PARAM) === "true";
1514

16-
return hasToolbarParam;
15+
return {
16+
isVisible: urlSearchParams.get(TOOLBAR_QUERY_PARAM) === "true",
17+
};
1718
};

0 commit comments

Comments
 (0)