Skip to content

Commit 6b62ff7

Browse files
committed
Update SPA instructions
1 parent 660da6e commit 6b62ff7

File tree

6 files changed

+99
-522
lines changed

6 files changed

+99
-522
lines changed

assets/scss/_styles_project.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3365,6 +3365,10 @@ div.tablestep > table td, div.tablestep table td {
33653365
margin-left: 1.0rem;
33663366
}
33673367

3368+
.td-content > ol li {
3369+
margin-bottom: 0.5rem;
3370+
}
3371+
33683372
.td-content > ul {
33693373
padding-left: 1rem;
33703374
}

docs/operate/control/single-page-apps.md

Lines changed: 92 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
title: "Create a custom web interface"
33
linkTitle: "Create a custom web interface"
4-
weight: 11
5-
no_list: true
6-
type: docs
4+
weight: 5
5+
layout: "docs"
6+
type: "docs"
77
description: "Create and deploy custom web interfaces for your machines as single-page applications without managing hosting and authentication."
88
---
99

@@ -13,7 +13,7 @@ Once deployed, apps are accessible from a dedicated URL (`appname_publicnamespac
1313
When opening an app, users log in and then select a machine they have access to.
1414
Then your app is rendered and ready for use.
1515

16-
{{<imgproc src="/operate/spa.png" resize="400x" declaredimensions=true alt="App screen asking for the org, location, and machine." class="imgzoom shadow">}}
16+
{{<gif webm_src="/spa.webm" mp4_src="/spa.mp4" alt="Sample web app" max-width="500px">}}
1717

1818
## Requirements
1919

@@ -28,14 +28,14 @@ Then authenticate your CLI session with Viam using one of the following options:
2828

2929
{{< /expand >}}
3030

31-
## Create a single page app
31+
## Build a custom web interface
3232

33-
{{< table >}}
34-
{{% tablestep number=1 %}}
33+
You can build a custom web interface to access your machines using your preferred framework like React, Vue, Angular, or others.
3534

36-
**Build your application** using your preferred framework like React, Vue, Angular, or others.
37-
While you're developing use any machine's credentials.
38-
For deploying your app you must add code to read the machine API key from your browsers local storage:
35+
### Access machines from your app
36+
37+
Viam apps provide access to a machine by placing its API key in your local storage.
38+
You can access the data from your browser's local storage as follows:
3939

4040
```ts {class="line-numbers linkable-line-numbers" data-line=""}
4141
import Cookies from "js-cookie";
@@ -53,8 +53,82 @@ machineId = window.location.pathname.split("/")[2];
5353
} = JSON.parse(Cookies.get(machineId)!));
5454
```
5555

56-
{{% /tablestep %}}
57-
{{% tablestep number=2 %}}
56+
For developing your app on localhost, add the same information to your browsers local storage.
57+
58+
1. Navigate to [Camera Viewer](https://camera-viewer_naomi.viamapplications.com/).
59+
2. Log in and select the machine you'd like to use for testing.
60+
3. Open Developer tools and go to the console.
61+
4. Execute the following JavaScript to obtain the cookies you need:
62+
63+
```js {class="line-numbers linkable-line-numbers" data-line=""}
64+
function generateCookieSetterScript() {
65+
// Get all cookies from current page
66+
const currentCookies = document.cookie.split(";");
67+
let cookieSetterCode = "// Cookie setter script for localhost\n";
68+
cookieSetterCode +=
69+
"// Copy and paste this entire script into your browser console when on localhost\n\n";
70+
71+
// Process each cookie
72+
let cookieCount = 0;
73+
currentCookies.forEach((cookie) => {
74+
if (cookie.trim()) {
75+
// Extract name and value from the cookie
76+
const [name, value] = cookie.trim().split("=");
77+
78+
// Add code to set this cookie
79+
cookieSetterCode += `document.cookie = "${name}=${value}; path=/";\n`;
80+
cookieCount++;
81+
}
82+
});
83+
84+
// Add summary comment
85+
cookieSetterCode += `\nconsole.log("Set ${cookieCount} cookies on localhost");\n`;
86+
87+
// Display the generated code
88+
console.log(cookieSetterCode);
89+
90+
// Create a textarea element to make copying easier
91+
const textarea = document.createElement("textarea");
92+
textarea.value = cookieSetterCode;
93+
textarea.style.position = "fixed";
94+
textarea.style.top = "0";
95+
textarea.style.left = "0";
96+
textarea.style.width = "100%";
97+
textarea.style.height = "250px";
98+
textarea.style.zIndex = "9999";
99+
document.body.appendChild(textarea);
100+
textarea.focus();
101+
textarea.select();
102+
}
103+
104+
// Execute the function
105+
generateCookieSetterScript();
106+
```
107+
108+
5. Copy the resulting script. It will look like this:
109+
110+
```js {class="line-numbers linkable-line-numbers" data-line=""}
111+
// Cookie setter script for localhost
112+
// Copy and paste this entire script into your browser console when on localhost
113+
114+
document.cookie = "<SECRET COOKIE INFO>; path=/";
115+
document.cookie = "machinesWhoseCredentialsAreStored=<MACHINE ID>; path=/";
116+
117+
console.log("Set 2 cookies on localhost");
118+
```
119+
120+
6. Open the app you are building on localhost and run the resulting script.
121+
7. Reload your app.
122+
123+
### Configure routing
124+
125+
When using your deployed application, static files will be accessible at `https://your-app-name_your-public-namespace.viamapplications.com/machine/<machine-id>/`.
126+
If your HTML file loads other files, use relative paths to ensure your files are accessible.
127+
128+
## Deploy your web interface
129+
130+
{{< table >}}
131+
{{% tablestep number=1 %}}
58132

59133
**Create a <FILE>meta.json</FILE>** file using this template:
60134

@@ -123,7 +197,7 @@ The `applications` field is an array of application objects with the following p
123197
| `entrypoint` | string | The path to the HTML entry point for your application. The `entrypoint` field specifies the path to your application's entry point. For example: <ul><li><code>"dist/index.html"</code>: Static content rooted at the `dist` directory</li><li><code>"dist/foo.html"</code>: Static content rooted at the `dist` directory, with `foo.html` as the entry point</li><li><code>"dist/"</code>: Static content rooted at the `dist` directory (assumes `dist/index.html` exists)</li><li><code>"dist/bar/foo.html"</code>: Static content rooted at `dist/bar` with `foo.html` as the entry point</li></ul> |
124198

125199
{{% /tablestep %}}
126-
{{% tablestep number=3 %}}
200+
{{% tablestep number=2 %}}
127201
**Register your module** with Viam:
128202

129203
{{< tabs >}}
@@ -144,13 +218,13 @@ viam module create --name="air-quality" --public-namespace="naomi"
144218
{{< /tabs >}}
145219

146220
{{% /tablestep %}}
147-
{{% tablestep number=4 %}}
221+
{{% tablestep number=3 %}}
148222

149223
**Package your static files and your <FILE>meta.json</FILE> file and upload them** to the Viam Registry:
150224

151225
```sh {class="command-line" data-prompt="$" data-output="3-10"}
152226
tar -czvf module.tar.gz <static-website-files> meta.json
153-
viam module upload module.tar.gz --platform=any --version=0.0.1
227+
viam module upload --upload=module.tar.gz --platform=any --version=0.0.1
154228
```
155229

156230
For subsequent updates run these commands again with an updated version number.
@@ -176,7 +250,8 @@ Users will be prompted to authenticate with their Viam credentials before access
176250

177251
## Example
178252

179-
For an example see [Monitor Air Quality with a Fleet of Sensors](/tutorials/control/air-quality-fleet/).
253+
For a TypeScript example see [Monitor Air Quality with a Fleet of Sensors](/tutorials/control/air-quality-fleet/).
254+
For a Reach app that shows Camera feeds for a machine, see [Viam Camera Viewer](https://github.com/viam-labs/viam-camera-viewer).
180255

181256
## Limitations
182257

@@ -187,7 +262,7 @@ For an example see [Monitor Air Quality with a Fleet of Sensors](/tutorials/cont
187262

188263
## Security Considerations
189264

190-
- Customer apps are stored in GCS buckets that are publicly available on the internet
265+
- Customer apps are stored publicly available on the internet
191266
- Avoid uploading sensitive information in your application code or assets
192267
- API keys and secrets are stored in the browser's localStorage or sessionStorage
193268
- Single page apps authenticate users with FusionAuth

docs/operate/control/web-app.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
linkTitle: "Create a web app"
3-
title: "Create a web app"
3+
title: "Create a self-hosted web app"
44
weight: 10
55
layout: "docs"
66
type: "docs"

docs/operate/reference/module-configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ The `applications` field is an array of application objects with the following p
285285

286286
For more information about Single Page Apps, see the [Single Page Apps documentation](/operate/reference/single-page-apps/).
287287

288-
#### Environment variables
288+
### Environment variables
289289

290290
Each module has access to the following default environment variables.
291291
Not all of these variables are automatically available on [local modules](/operate/get-started/other-hardware/#test-your-module-locally); you can manually set variables your module requires if necessary.

docs/tutorials/control/air-quality-fleet.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ viam module create --name="air-quality" --public-namespace="your-namespace"
726726
```sh {class="command-line" data-prompt="$" data-output="3-10"}
727727
npm run build
728728
tar -czvf module.tar.gz static meta.json
729-
viam module upload module.tar.gz --platform=any --version=0.0.1
729+
viam module upload --upload=module.tar.gz --platform=any --version=0.0.1
730730
```
731731

732732
For subsequent updates run these commands again with an updated version number.

0 commit comments

Comments
 (0)