11import type React from "react" ;
2- import { useEffect , useState } from "react" ;
2+ import { useCallback , useEffect , useState } from "react" ;
33import { Button , Col , Row } from "react-bootstrap" ;
44import Form from "react-bootstrap/Form" ;
55import { openBrowserUrl } from "../helpers" ;
@@ -48,13 +48,13 @@ export default function Snapshot() {
4848 const handleInputChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
4949 setTokenValidated ( false ) ;
5050 const { name, value } = e . target ;
51- if ( name == "license" ) {
51+ if ( name === "license" ) {
5252 setLicenseKey ( value ) ;
53- } else if ( name == "token" ) {
53+ } else if ( name === "token" ) {
5454 setToken ( value ) ;
55- } else if ( name == "port" ) {
55+ } else if ( name === "port" ) {
5656 setCurlPort ( value ) ;
57- } else if ( name == "restart-policy" ) {
57+ } else if ( name === "restart-policy" ) {
5858 setRestartPolicy ( value ) ;
5959 }
6060 } ;
@@ -64,13 +64,7 @@ export default function Snapshot() {
6464 setArchitecture ( e . target . value ) ;
6565 } ;
6666
67- // type SnapshotData = {
68- // port: string;
69- // license: string;
70- // token: string;
71- // image: string;
72- // }
73- const generateDockerImage = ( ) => {
67+ const generateDockerImage = useCallback ( ( ) => {
7468 let dockerImage = "platerecognizer/" ;
7569 if (
7670 country === "Global" ||
@@ -83,45 +77,49 @@ export default function Snapshot() {
8377 }
8478 setDockerimage ( dockerImage ) ;
8579 return dockerImage ;
86- } ;
87- const generateDockerRunCommand = ( dockerImage : string ) => {
88- let restartOption ;
89- switch ( restartPolicy ) {
90- case "no" :
91- restartOption = "" ;
92- break ;
93- default :
94- restartOption = `--restart=${ restartPolicy } ` ;
95- break ;
96- }
97- const baseCommand = `docker run ${ restartOption } -t -p ${ curlPort } :8080 -v license:/license` ;
98- let platformSpecificCommand = "" ;
80+ } , [ country , architecture ] ) ;
9981
100- switch ( architecture ) {
101- case "alpr-jetson" :
102- case "alpr-jetson:r35" :
103- platformSpecificCommand = ` --runtime nvidia -e LICENSE_KEY=${ licenseKey } -e TOKEN=${ token } ${ dockerImage } ` ;
104- break ;
105- case "alpr-gpu" :
106- platformSpecificCommand = ` --gpus all -e LICENSE_KEY=${ licenseKey } -e TOKEN=${ token } ${ dockerImage } ` ;
107- break ;
108- case "alpr" :
109- case "alpr-no-avx" :
110- case "alpr-arm" :
111- case "alpr-zcu104" :
112- platformSpecificCommand = ` -e LICENSE_KEY=${ licenseKey } -e TOKEN=${ token } ${ dockerImage } ` ;
113- break ;
114- default :
115- break ;
116- }
82+ const generateDockerRunCommand = useCallback (
83+ ( dockerImage : string ) => {
84+ let restartOption : string ;
85+ switch ( restartPolicy ) {
86+ case "no" :
87+ restartOption = "" ;
88+ break ;
89+ default :
90+ restartOption = `--restart=${ restartPolicy } ` ;
91+ break ;
92+ }
93+ const baseCommand = `docker run ${ restartOption } -t -p ${ curlPort } :8080 -v license:/license` ;
94+ let platformSpecificCommand = "" ;
11795
118- setCommand ( `${ baseCommand } ${ platformSpecificCommand } ` ) ;
119- } ;
96+ switch ( architecture ) {
97+ case "alpr-jetson" :
98+ case "alpr-jetson:r35" :
99+ platformSpecificCommand = ` --runtime nvidia -e LICENSE_KEY=${ licenseKey } -e TOKEN=${ token } ${ dockerImage } ` ;
100+ break ;
101+ case "alpr-gpu" :
102+ platformSpecificCommand = ` --gpus all -e LICENSE_KEY=${ licenseKey } -e TOKEN=${ token } ${ dockerImage } ` ;
103+ break ;
104+ case "alpr" :
105+ case "alpr-no-avx" :
106+ case "alpr-arm" :
107+ case "alpr-zcu104" :
108+ platformSpecificCommand = ` -e LICENSE_KEY=${ licenseKey } -e TOKEN=${ token } ${ dockerImage } ` ;
109+ break ;
110+ default :
111+ break ;
112+ }
113+
114+ setCommand ( `${ baseCommand } ${ platformSpecificCommand } ` ) ;
115+ } ,
116+ [ restartPolicy , curlPort , architecture , licenseKey , token ] ,
117+ ) ;
120118
121119 useEffect ( ( ) => {
122120 const imagem = generateDockerImage ( ) ;
123121 generateDockerRunCommand ( imagem ) ;
124- } , [ country , architecture , token , curlPort , licenseKey , restartPolicy ] ) ;
122+ } , [ generateDockerImage , generateDockerRunCommand ] ) ;
125123
126124 // Load any existing data from local storage on component mount
127125 useEffect ( ( ) => {
@@ -148,8 +146,8 @@ export default function Snapshot() {
148146
149147 ddClient . extension . vm ?. service ?. post ( "/verify-token" , data ) . then ( ( res : any ) => {
150148 console . debug ( res ) ;
151- const valid = res [ " valid" ] ;
152- const message = res [ " message" ] ;
149+ const valid = res . valid ;
150+ const message = res . message ;
153151 if ( valid ) {
154152 localStorage . setItem (
155153 "snapshot" ,
@@ -241,7 +239,7 @@ export default function Snapshot() {
241239 label = "No (Docker Default)"
242240 id = "rps1"
243241 value = "no"
244- checked = { restartPolicy == "no" }
242+ checked = { restartPolicy === "no" }
245243 onChange = { handleInputChange }
246244 />
247245 < Form . Check
@@ -250,7 +248,7 @@ export default function Snapshot() {
250248 label = "Unless Stopped"
251249 id = "rps2"
252250 value = "unless-stopped"
253- checked = { restartPolicy == "unless-stopped" }
251+ checked = { restartPolicy === "unless-stopped" }
254252 onChange = { handleInputChange }
255253 />
256254 < Form . Check
@@ -259,7 +257,7 @@ export default function Snapshot() {
259257 label = "Always"
260258 id = "rps3"
261259 value = "always"
262- checked = { restartPolicy == "always" }
260+ checked = { restartPolicy === "always" }
263261 onChange = { handleInputChange }
264262 />
265263 < Form . Check
@@ -268,7 +266,7 @@ export default function Snapshot() {
268266 label = "On Failure"
269267 id = "rps4"
270268 value = "on-failure"
271- checked = { restartPolicy == "on-failure" }
269+ checked = { restartPolicy === "on-failure" }
272270 onChange = { handleInputChange }
273271 />
274272 </ Col >
@@ -306,9 +304,9 @@ export default function Snapshot() {
306304 architecture === "alpr-jetson:r35" || architecture === "alpr-no-avx"
307305 }
308306 >
309- { countryOptions . map ( ( option , index ) => (
310- < option key = { index } value = { option . value } >
311- { option . label }
307+ { countryOptions . map ( ( { value , label } ) => (
308+ < option key = { label } value = { value } >
309+ { label }
312310 </ option >
313311 ) ) }
314312 </ Form . Select >
@@ -320,9 +318,9 @@ export default function Snapshot() {
320318 name = "image"
321319 defaultValue = { architecture }
322320 >
323- { architectureOptionsSnapshot . map ( ( option , index ) => (
324- < option key = { index } value = { option . value } >
325- { option . label }
321+ { architectureOptionsSnapshot . map ( ( { value , label } ) => (
322+ < option key = { value } value = { value } >
323+ { label }
326324 </ option >
327325 ) ) }
328326 </ Form . Select >
@@ -333,12 +331,15 @@ export default function Snapshot() {
333331
334332 < Form . Group as = { Row } className = "mb-3" >
335333 < div className = "col-2" >
336- < Button className = "btn btn-primary" type = "submit" >
334+ < Button className = "btn btn-primary" type = "submit" id = "validateSnapshotBtn" >
337335 < Loader isLoading = { isLoading } />
338336 Show Docker Command
339337 </ Button >
340338 </ div >
341- < label className = "col-auto align-self-center form-label" >
339+ < label
340+ className = "col-auto align-self-center form-label"
341+ htmlFor = "validateSnapshotBtn"
342+ >
342343 Confirm settings and show docker command.
343344 </ label >
344345 </ Form . Group >
0 commit comments