Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
parkpow
GitHub Repository: parkpow/deep-license-plate-recognition
Path: blob/master/docker/dd-extension/ui/src/components/ShowCommand.tsx
1085 views
1
import { useState } from "react";
2
import { Row } from "react-bootstrap";
3
import Form from "react-bootstrap/Form";
4
import { useDockerDesktopClient } from "../hooks/useDockerDesktopClient";
5
import Loader from "./Loader";
6
7
interface ShowCommandProps {
8
validated: boolean;
9
command: string;
10
curlPort: string;
11
}
12
13
export default function ShowCommand({
14
validated,
15
command,
16
curlPort,
17
}: ShowCommandProps) {
18
const [isRunningCommand, setRunningCommand] = useState(false);
19
const ddClient = useDockerDesktopClient();
20
21
if (!validated) return null;
22
23
function copyToClipboard() {
24
navigator.clipboard
25
.writeText(command)
26
.then(() => {
27
ddClient.desktopUI.toast.success("Text copied to clipboard!");
28
})
29
.catch((err) => {
30
ddClient.desktopUI.toast.error(`Failed to copy text: ${err}`);
31
});
32
}
33
34
function runCommand() {
35
setRunningCommand(true);
36
// Generate list of run options
37
console.debug(command);
38
const cmd: Array<string> = command.match(/[^ ]+/g)?.slice(2) || [];
39
// Run in the background
40
if (!cmd.includes("-d")) {
41
cmd.unshift("-d");
42
}
43
44
ddClient.docker.cli
45
.exec("run", cmd)
46
.then(async (result) => {
47
console.debug(result);
48
setRunningCommand(false);
49
ddClient.desktopUI.toast.success("Command Ran Successfully.");
50
await ddClient.desktopUI.navigate.viewContainer(result.stdout.trim());
51
})
52
.catch((e) => {
53
setRunningCommand(false);
54
console.error(e);
55
ddClient.desktopUI.toast.error(`Run Command Failed: ${e.stderr.trim()}`);
56
});
57
}
58
59
const curlCommand = curlPort ? (
60
<div>
61
<p className="card-title mt-3 mb-0" style={{ display: "inline-block" }}>
62
To use the SDK endpoint call:{" "}
63
</p>
64
<code>
65
{" "}
66
curl -F "upload=@my_file.jpg" http://localhost:{curlPort}
67
/v1/plate-reader/
68
</code>
69
</div>
70
) : null;
71
72
return (
73
<Form.Group as={Row} className="mb-3">
74
<div style={{ display: "block" }}>
75
<div className="mt-3 card" style={{ display: "block" }}>
76
<div className="card-body">
77
<p className="card-title">
78
You can now start {curlPort ? "Snapshot" : "Stream"}. Open a terminal and
79
type the command below. You can save this command for future use.
80
</p>
81
<code className="card-text d-block">{command}</code>
82
<div className="mt-3">
83
<button
84
className="btn btn-sm btn-success mx-2"
85
style={{ borderRadius: 15 }}
86
type="button"
87
onClick={runCommand}
88
>
89
<Loader isLoading={isRunningCommand} /> Run
90
</button>
91
<button
92
className="btn btn-sm btn-warning mx-2"
93
style={{ borderRadius: 15 }}
94
type="button"
95
onClick={copyToClipboard}
96
>
97
copy to clipboard
98
</button>
99
<span className="ms-2" style={{ fontSize: 13, color: "green" }} />
100
</div>
101
{curlCommand}
102
</div>
103
</div>
104
</div>
105
</Form.Group>
106
);
107
}
108
109