Skip to content

Commit a52b0e8

Browse files
authored
Merge pull request #101 from dockersamples/100-run-button-bug
Fix bug in which run command buttons were using stale commands
2 parents 586b4e7 + 976fcfe commit a52b0e8

File tree

3 files changed

+53
-52
lines changed

3 files changed

+53
-52
lines changed

components/interface/client/src/WorkshopContext.jsx

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -68,53 +68,47 @@ export const WorkshopContextProvider = ({ children }) => {
6868
});
6969
}, [activeSectionId, setActiveSection, refreshCounter]);
7070

71-
const runCommand = useCallback(
72-
(codeBlockIndex) => {
73-
return fetch(`/api/sections/${activeSection.id}/command`, {
74-
method: "POST",
75-
headers: {
76-
"Content-Type": "application/json",
77-
},
78-
body: JSON.stringify({ codeBlockIndex }),
71+
const runCommand = useCallback((activeSectionId, codeBlockIndex) => {
72+
return fetch(`/api/sections/${activeSectionId}/command`, {
73+
method: "POST",
74+
headers: {
75+
"Content-Type": "application/json",
76+
},
77+
body: JSON.stringify({ codeBlockIndex }),
78+
})
79+
.then((response) => {
80+
if (!response.ok) throw new Error("Failed to run command");
81+
return response.json();
7982
})
80-
.then((response) => {
81-
if (!response.ok) throw new Error("Failed to run command");
82-
return response.json();
83-
})
84-
.then((result) => {
85-
console.log("Command result:", result);
86-
})
87-
.catch((error) => {
88-
console.error("Error running command:", error);
89-
toast.error("Failed to run command. Please try again.");
90-
});
91-
},
92-
[activeSection],
93-
);
94-
95-
const saveFileCommand = useCallback(
96-
(codeBlockIndex) => {
97-
return fetch(`/api/sections/${activeSection.id}/save-file`, {
98-
method: "POST",
99-
headers: {
100-
"Content-Type": "application/json",
101-
},
102-
body: JSON.stringify({ codeBlockIndex }),
83+
.then((result) => {
84+
console.log("Command result:", result);
10385
})
104-
.then((response) => {
105-
if (!response.ok) throw new Error("Failed to save file");
106-
return response.json();
107-
})
108-
.then((result) => {
109-
console.log("Save file result:", result);
110-
})
111-
.catch((error) => {
112-
console.error("Error saving file:", error);
113-
toast.error("Failed to save file. Please try again.");
114-
});
115-
},
116-
[activeSection],
117-
);
86+
.catch((error) => {
87+
console.error("Error running command:", error);
88+
toast.error("Failed to run command. Please try again.");
89+
});
90+
}, []);
91+
92+
const saveFileCommand = useCallback((activeSectionId, codeBlockIndex) => {
93+
return fetch(`/api/sections/${activeSectionId}/save-file`, {
94+
method: "POST",
95+
headers: {
96+
"Content-Type": "application/json",
97+
},
98+
body: JSON.stringify({ codeBlockIndex }),
99+
})
100+
.then((response) => {
101+
if (!response.ok) throw new Error("Failed to save file");
102+
return response.json();
103+
})
104+
.then((result) => {
105+
console.log("Save file result:", result);
106+
})
107+
.catch((error) => {
108+
console.error("Error saving file:", error);
109+
toast.error("Failed to save file. Please try again.");
110+
});
111+
}, []);
118112

119113
useEffect(() => {
120114
if (!workshop) return;

components/interface/client/src/components/WorkshopPanel/markdown/CodeBlock.jsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ import { darcula } from "react-syntax-highlighter/dist/cjs/styles/prism";
33

44
import copy from "copy-to-clipboard";
55
import { useCallback } from "react";
6-
import { useRunCommand, useSaveFileCommand } from "../../../WorkshopContext";
6+
import {
7+
useActiveSection,
8+
useRunCommand,
9+
useSaveFileCommand,
10+
} from "../../../WorkshopContext";
711
import { CodeBlockAction } from "./CodeBlockAction";
812

913
export function CodeBlock({ node, inline, className, children, ...props }) {
14+
const { activeSection } = useActiveSection();
1015
const runCommand = useRunCommand();
1116
const saveFileCommand = useSaveFileCommand();
1217

@@ -26,13 +31,15 @@ export function CodeBlock({ node, inline, className, children, ...props }) {
2631
return Promise.resolve();
2732
}, [children]);
2833

34+
// This needs `children` as ReactMarkdown seems to re-use the component instance
2935
const onRunClick = useCallback(() => {
30-
return runCommand(codeIndex);
31-
}, [codeIndex]);
36+
return runCommand(activeSection.id, codeIndex);
37+
}, [codeIndex, activeSection]);
3238

39+
// This needs `children` as ReactMarkdown seems to re-use the component instance
3340
const onSaveAsClick = useCallback(() => {
34-
return saveFileCommand(codeIndex);
35-
}, [codeIndex]);
41+
return saveFileCommand(activeSection.id, codeIndex);
42+
}, [codeIndex, activeSection]);
3643

3744
if (!match || inline) {
3845
return (

components/interface/client/src/components/WorkshopPanel/markdown/reactDirective.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export function tabDirective() {
2525
const data = node.data || (node.data = {});
2626

2727
if (!isNaN(Number(node.name))) {
28-
node.children = [{ type: 'text', value: `:${node.name}` }];
29-
node.name = node.type === 'textDirective' ? 'span' : 'div';
28+
node.children = [{ type: "text", value: `:${node.name}` }];
29+
node.name = node.type === "textDirective" ? "span" : "div";
3030
hastify(node, {});
3131
return;
3232
}

0 commit comments

Comments
 (0)