Path: blob/master/docker/dd-extension/ui/src/components/Stream.tsx
1091 views
import type React from "react";1import { useEffect, useState } from "react";2import { Button, Col, Row } from "react-bootstrap";3import Form from "react-bootstrap/Form";4import { openBrowserUrl } from "../helpers";5import { useDockerDesktopClient } from "../hooks/useDockerDesktopClient";6import Loader from "./Loader";7import ShowCommand from "./ShowCommand";8import Uninstall from "./Uninstall";9import Update from "./Update";1011export default function Stream() {12const STREAM_IMAGE = "platerecognizer/alpr-stream";13const [command, setCommand] = useState<string>("");14const [license, setLicense] = useState<string>("");15const [token, setToken] = useState<string>("");16const [streamPath, setStreamPath] = useState<string>("");17const [tokenValidated, setTokenValidated] = useState(false);18const [isLoading, setLoading] = useState(false);19const [restartPolicy, setRestartPolicy] = useState("no");2021const ddClient = useDockerDesktopClient();2223const handleLinkClick = (e: React.MouseEvent<HTMLAnchorElement>) => {24e.preventDefault();25openBrowserUrl(ddClient, e.currentTarget.href);26};2728const handleConfigureClick = () => {29if (license) {30const url = `https://app.platerecognizer.com/stream-config/${license}`;31openBrowserUrl(ddClient, url);32} else {33ddClient.desktopUI.toast.error("License Key is required");34}35};36const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {37const { name, value } = e.target;38if (name === "license") {39setLicense(value);40} else if (name === "restart-policy") {41setRestartPolicy(value);42} else if (name === "token") {43setToken(value);44} else if (name === "streamPath") {45setStreamPath(value);46}47setTokenValidated(false);48};4950const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {51event.preventDefault();52const form: HTMLFormElement = event.currentTarget;53const formData = new FormData(form);5455const data: any = Object.fromEntries(formData.entries());56// console.log(data);57setLoading(true);58ddClient.extension.vm?.service?.post("/verify-token", data).then((res: any) => {59console.debug(res);60const valid = res.valid;61const message = res.message;62if (valid) {63localStorage.setItem(64"stream",65JSON.stringify({66token: token,67streamPath: streamPath,68restartPolicy: restartPolicy,69license: license,70}),71);72// Pull image and update73ddClient.docker.cli.exec("pull", [STREAM_IMAGE]).then((result) => {74console.debug(result);75const autoBoot =76restartPolicy !== "no" ? ` --restart ${restartPolicy}` : "--rm";77const command = `docker run ${autoBoot} -t -v ${data.streamPath}:/user-data/ -e LICENSE_KEY=${data.license} -e TOKEN=${data.token} ${STREAM_IMAGE}`;78setCommand(command);79setTokenValidated(valid);80setLoading(false);81});82} else {83setLoading(false);84ddClient.desktopUI.toast.error(`Verify Token: ${message}`);85}86});87};8889// Load any existing data from local storage on component mount90useEffect(() => {91const storedData = localStorage.getItem("stream");92if (storedData) {93const streamData = JSON.parse(storedData);94setToken(streamData?.token);95setStreamPath(streamData?.streamPath);96setRestartPolicy(streamData?.restartPolicy);97setLicense(streamData?.license);98}99}, []);100101return (102<Form onSubmit={handleSubmit}>103<Form.Group as={Row} className="mb-3" controlId="streamToken">104<Form.Label column sm={4}>105Please enter your Plate Recognizer{" "}106<a107href="https://app.platerecognizer.com/service/stream/"108onClick={handleLinkClick}109>110API Token111</a>112:113</Form.Label>114<Col sm={8}>115<Form.Control116type="text"117placeholder="Token"118required119name="token"120value={token}121onChange={handleInputChange}122/>123</Col>124</Form.Group>125126<Form.Group as={Row} className="mb-3" controlId="streamLicense">127<Form.Label column sm={4}>128Please enter your{" "}129<a130href="https://app.platerecognizer.com/service/stream/"131onClick={handleLinkClick}132>133Stream License Key134</a>135:136</Form.Label>137<Col sm={8}>138<Form.Control139type="text"140placeholder="License Key"141required142name="license"143value={license}144onChange={handleInputChange}145/>146</Col>147</Form.Group>148149<Form.Group as={Row} className="mb-3" controlId="streamPath">150<Form.Label column sm={4}>151Path to your Stream installation directory:152</Form.Label>153<Col sm={8}>154<Form.Control155type="text"156placeholder="Path to directory"157required158name="streamPath"159value={streamPath}160onChange={handleInputChange}161/>162</Col>163</Form.Group>164165<Form.Group as={Row} className="mb-3">166<Form.Label column sm={4}>167Restart policy168</Form.Label>169<Col sm={8} className="mt-2 d-flex justify-content-between">170<Form.Check171type="radio"172name="restart-policy"173label="No (Docker Default)"174id="rp1"175value="no"176checked={restartPolicy === "no"}177onChange={handleInputChange}178/>179<Form.Check180type="radio"181name="restart-policy"182label="Unless Stopped"183id="rp2"184value="unless-stopped"185checked={restartPolicy === "unless-stopped"}186onChange={handleInputChange}187/>188<Form.Check189type="radio"190name="restart-policy"191label="Always"192id="rp3"193value="always"194checked={restartPolicy === "always"}195onChange={handleInputChange}196/>197<Form.Check198type="radio"199name="restart-policy"200label="On Failure"201id="rp4"202value="on-failure"203checked={restartPolicy === "on-failure"}204onChange={handleInputChange}205/>206</Col>207</Form.Group>208209<ShowCommand curlPort={""} command={command} validated={tokenValidated} />210211<Form.Group as={Row} className="mb-3">212<div className="col-2">213<Button214onClick={handleConfigureClick}215className="btn btn-success"216type="button"217>218Configure219</Button>220</div>221<label222className="col-auto align-self-center form-label"223htmlFor="button-submit-snapshot"224>225Edit configurations for your Stream license226</label>227</Form.Group>228229<Form.Group as={Row} className="mb-3">230<div className="col-2">231<Button232className="btn btn-primary"233type="submit"234id="button-submit-docker-command"235>236<Loader isLoading={isLoading} />237Show Docker Command238</Button>239</div>240<label241className="col-auto align-self-center form-label"242htmlFor="button-submit-docker-command"243>244Confirm settings and show docker command.245</label>246</Form.Group>247248<Update isEnabled={true} image={STREAM_IMAGE} />249<Uninstall isEnabled={true} image={STREAM_IMAGE} />250</Form>251);252}253254255