|
1 | 1 | <script lang="ts"> |
2 | | - import { getCookie, setCookie } from 'typescript-cookie' |
3 | | - import { ViamProvider } from '@viamrobotics/svelte-sdk'; |
4 | | - |
5 | | - import logo from './assets/viam.svg' |
6 | | - import Main from "./main.svelte" |
7 | | -
|
8 | | -
|
9 | | - let myState = $state({error : ""}); |
10 | | -
|
11 | | - function getHostAndCredentials() { |
12 | | - var parts = window.location.pathname.split("/"); |
13 | | - if (parts && parts.length >= 3 && parts[1] == "machine") { |
14 | | - var machineId = parts[2]; |
15 | | - myState.machineId = machineId; |
16 | | - var x = getCookie(machineId); |
17 | | - if (x) { |
18 | | - var x = JSON.parse(x); |
19 | | - return [x.hostname, {type: 'api-key', payload: x.key, authEntity: x.id}]; |
20 | | - } |
21 | | - } |
22 | | -
|
23 | | - var x = getCookie("default-host"); |
24 | | - if (x && x != "") { |
25 | | - var x = JSON.parse(x); |
26 | | - return [x.hostname, {type: 'api-key', payload: x.key, authEntity: x.id}]; |
27 | | - } |
28 | | -
|
29 | | - var host = getCookie("host"); |
30 | | - var keyId = getCookie("api-key-id"); |
31 | | - var key = getCookie("api-key"); |
32 | | -
|
33 | | - if (host && host != "") { |
34 | | - return [host, {type: 'api-key', payload: key, authEntity: keyId}]; |
35 | | - } |
36 | | - |
37 | | - return ["", {}]; |
38 | | - } |
39 | | - |
40 | | - function init() { |
41 | | - const [host, credentials] = getHostAndCredentials(); |
42 | | - if (host != "") { |
43 | | - myState.host = host; |
44 | | - myState.credentials = credentials; |
45 | | - } |
46 | | - } |
47 | | - |
48 | | - function saveHostInfo() { |
49 | | - if (!myState.machineId) { |
50 | | - throw "neeed a machineId"; |
51 | | - } |
52 | | - var host = document.getElementById("in_host").value; |
53 | | - var id = document.getElementById("in_id").value; |
54 | | - var key = document.getElementById("in_key").value; |
55 | | -
|
56 | | - if (host == "") { |
57 | | - myState.error = "need a host"; |
58 | | - return; |
59 | | - } |
60 | | - if (id == "") { |
61 | | - myState.error = "need an id"; |
62 | | - return; |
63 | | - } |
64 | | - if (key == "") { |
65 | | - myState.error = "need a key" |
66 | | - return; |
67 | | - } |
68 | | -
|
69 | | - setCookie(myState.machineId, JSON.stringify({hostname: host, key: key, id: id})) |
70 | | -
|
71 | | - } |
72 | | -
|
73 | | - init(); |
| 2 | + import { getCookie, setCookie } from "typescript-cookie"; |
| 3 | + import { ViamProvider } from "@viamrobotics/svelte-sdk"; |
| 4 | +
|
| 5 | + import logo from "./assets/viam.svg"; |
| 6 | + import Main from "./main.svelte"; |
| 7 | +
|
| 8 | + let myState = $state({ error: "", machineId: "", host: "", credentials: {} }); |
| 9 | +
|
| 10 | + function getHostAndCredentials() { |
| 11 | + const parts = window.location.pathname.split("/"); |
| 12 | + if (parts && parts.length >= 3 && parts[1] == "machine") { |
| 13 | + const machineId = parts[2]; |
| 14 | + myState.machineId = machineId; |
| 15 | + const cookieValue = getCookie(machineId); |
| 16 | + if (cookieValue) { |
| 17 | + const parsedValue = JSON.parse(cookieValue); |
| 18 | + return [ |
| 19 | + parsedValue.hostname, |
| 20 | + { |
| 21 | + type: "api-key", |
| 22 | + payload: parsedValue.key, |
| 23 | + authEntity: parsedValue.id, |
| 24 | + }, |
| 25 | + ]; |
| 26 | + } |
| 27 | + } |
| 28 | +
|
| 29 | + const defaultCookie = getCookie("default-host"); |
| 30 | + if (defaultCookie && defaultCookie != "") { |
| 31 | + const parsedDefault = JSON.parse(defaultCookie); |
| 32 | + return [ |
| 33 | + parsedDefault.hostname, |
| 34 | + { |
| 35 | + type: "api-key", |
| 36 | + payload: parsedDefault.key, |
| 37 | + authEntity: parsedDefault.id, |
| 38 | + }, |
| 39 | + ]; |
| 40 | + } |
| 41 | +
|
| 42 | + const host = getCookie("host"); |
| 43 | + const keyId = getCookie("api-key-id"); |
| 44 | + const key = getCookie("api-key"); |
| 45 | +
|
| 46 | + if (host && host != "") { |
| 47 | + return [host, { type: "api-key", payload: key, authEntity: keyId }]; |
| 48 | + } |
| 49 | +
|
| 50 | + return ["", {}]; |
| 51 | + } |
| 52 | +
|
| 53 | + function init() { |
| 54 | + const [host, credentials] = getHostAndCredentials(); |
| 55 | + if (host != "") { |
| 56 | + myState.host = host; |
| 57 | + myState.credentials = credentials; |
| 58 | + } |
| 59 | + } |
| 60 | +
|
| 61 | + function saveHostInfo() { |
| 62 | + if (!myState.machineId) { |
| 63 | + throw "neeed a machineId"; |
| 64 | + } |
| 65 | + const host = |
| 66 | + (document.getElementById("in_host") as HTMLInputElement)?.value || ""; |
| 67 | + const id = |
| 68 | + (document.getElementById("in_id") as HTMLInputElement)?.value || ""; |
| 69 | + const key = |
| 70 | + (document.getElementById("in_key") as HTMLInputElement)?.value || ""; |
| 71 | +
|
| 72 | + if (host == "") { |
| 73 | + myState.error = "need a host"; |
| 74 | + return; |
| 75 | + } |
| 76 | + if (id == "") { |
| 77 | + myState.error = "need an id"; |
| 78 | + return; |
| 79 | + } |
| 80 | + if (key == "") { |
| 81 | + myState.error = "need a key"; |
| 82 | + return; |
| 83 | + } |
| 84 | +
|
| 85 | + setCookie( |
| 86 | + myState.machineId, |
| 87 | + JSON.stringify({ hostname: host, key: key, id: id }) |
| 88 | + ); |
| 89 | + } |
| 90 | +
|
| 91 | + init(); |
74 | 92 | </script> |
75 | 93 |
|
76 | 94 | <main> |
|
79 | 97 | {myState.error} |
80 | 98 | </h1> |
81 | 99 | {/if} |
82 | | - |
| 100 | + |
83 | 101 | {#if myState.host} |
84 | 102 | <Main host={myState.host} credentials={myState.credentials} /> |
85 | 103 | <!-- <ComponentPreview host={myState.host} credentials={myState.credentials} /> --> |
|
88 | 106 | <p class="form-text">No host found, want to specify a default?</p> |
89 | 107 | <div class="form-group"> |
90 | 108 | <label for="in_host">Host:</label> |
91 | | - <input id="in_host" type="text"> |
| 109 | + <input id="in_host" type="text" /> |
92 | 110 | </div> |
93 | 111 | <div class="form-group"> |
94 | 112 | <label for="in_id">Key Id:</label> |
95 | | - <input id="in_id" type="text"> |
| 113 | + <input id="in_id" type="text" /> |
96 | 114 | </div> |
97 | 115 | <div class="form-group"> |
98 | 116 | <label for="in_key">Key:</label> |
99 | | - <input id="in_key" type="password"> |
| 117 | + <input id="in_key" type="password" /> |
100 | 118 | </div> |
101 | | - <button onclick="{saveHostInfo}" class="save-button">Save</button> |
| 119 | + <button onclick={saveHostInfo} class="save-button">Save</button> |
102 | 120 | </div> |
103 | 121 | {/if} |
104 | | - |
105 | | - |
106 | 122 | </main> |
107 | 123 |
|
108 | 124 | <style> |
|
0 commit comments