Skip to content

Commit

Permalink
use notyf for notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
suurkivi committed Oct 21, 2024
1 parent 3bdf144 commit f673a37
Show file tree
Hide file tree
Showing 19 changed files with 529 additions and 37 deletions.
1 change: 1 addition & 0 deletions frontend/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
//go:embed z2/*.js
//go:embed z2/css/*.css
//go:embed z2/css/purecss/*.css
//go:embed z2/css/notyf/*.css
//go:embed z2/pages/*.html
//go:embed z2/sounds/*.mp3
var FS embed.FS
Expand Down
6 changes: 6 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"@types/uuid": "^10.0.0",
"calendar": "github:minor-industries/calendar#v0.0.4",
"notyf": "^3.10.0",
"rtgraph": "github:minor-industries/rtgraph#v0.0.22",
"uuid": "^10.0.0"
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import {decode} from "@msgpack/msgpack"
import {DefaultApiClient} from "./api_client";
import {Env} from "./env"
import {runGoWasm, WASMConnector} from "./wasm_connector"
import {localDate, runOnce} from "./util";
import {localDate, notify, runOnce} from "./util";
import {streamEvents} from "./stream_events";


//@ts-ignore
import {runWasm} from "./startup.js";

Expand All @@ -24,6 +25,7 @@ export {
calendar,
decode,
localDate,
notify,
runGoWasm,
runOnce,
runWasm,
Expand Down
16 changes: 16 additions & 0 deletions frontend/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {Notyf} from 'notyf';

export function runOnce<T extends (...args: any[]) => Promise<any>>(asyncFn: T): T {
let initialized = false;
let resultPromise: ReturnType<T>;
Expand All @@ -18,4 +20,18 @@ export function localDate(): string {
const day = now.getDate().toString().padStart(2, '0');

return `${year}-${month}-${day}`;
}

export function notify(type: 'success' | 'error', message: string) {
switch (type) {
case "success":
new Notyf().success(message);
return;
case "error":
new Notyf().error(message);
return;
default:
new Notyf().error("unknown alert type");
return;
}
}
2 changes: 2 additions & 0 deletions frontend/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/pure-min.css">
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/grids-responsive-min.css">

<link rel="stylesheet" type="text/css" href="/z2/css/notyf/notyf.min.css"/>

<style>
body {
margin: 0 0 20px;
Expand Down
7 changes: 7 additions & 0 deletions frontend/templates/sound-events.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{{ define "sound-events" }}

<script type="module">
import {getEnv} from "/z2/env.js";
import {notify} from "/z2/z2-bundle.js";

const bluetoothConnectedHandler = (() => {
const seen = {};
Expand All @@ -20,6 +23,7 @@

const info = `${kind}${name} is connected`;
console.log(info);
notify("success", info);
};
})();

Expand All @@ -34,7 +38,10 @@
audio.play();
}
},

"bluetooth-is-connected": bluetoothConnectedHandler
});
});
</script>

{{end}}
1 change: 1 addition & 0 deletions frontend/z2/css/notyf/notyf.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frontend/z2/pages/backup.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/pure-min.css">
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/grids-responsive-min.css">

<link rel="stylesheet" type="text/css" href="/z2/css/notyf/notyf.min.css"/>

<style>
body {
margin: 0 0 20px;
Expand Down
2 changes: 2 additions & 0 deletions frontend/z2/pages/bike-presets.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/pure-min.css">
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/grids-responsive-min.css">

<link rel="stylesheet" type="text/css" href="/z2/css/notyf/notyf.min.css"/>

<style>
body {
margin: 0 0 20px;
Expand Down
46 changes: 28 additions & 18 deletions frontend/z2/pages/bike.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/pure-min.css">
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/grids-responsive-min.css">

<link rel="stylesheet" type="text/css" href="/z2/css/notyf/notyf.min.css"/>

<style>
body {
margin: 0 0 20px;
Expand Down Expand Up @@ -86,36 +88,44 @@

<script type="module">
import {getEnv} from "/z2/env.js";
import {notify} from "/z2/z2-bundle.js";

const bluetoothConnectedHandler = (() => {
const seen = {};

return (data) => {
const msg = JSON.parse(data);
const key = msg.address;
if (key === "") {
return;
}
if (seen[key]) {
return;
}
seen[key] = true;

const kind = msg.kind;
const name = msg.name ? " " + msg.name : "";

const info = `${kind}${name} is connected`;
console.log(info);
notify("success", info);
};
})();

document.addEventListener("DOMContentLoaded", async () => {
const env = await getEnv();

const seen = {};


env.streamEvents("/events", {
"play-sound": (data) => {
const audio = document.getElementById(`sound-${data}`);
if (audio) {
audio.play();
}
},
"bluetooth-is-connected": (data) => {
const msg = JSON.parse(data);
const key = msg.address;
if (key === "") {
return;
}
if (seen[key]) {
return;
}
seen[key] = true;

const kind = msg.kind;
const name = msg.name ? " " + msg.name : "";

const info = `${kind}${name} is connected`;
console.log(info);
},
"bluetooth-is-connected": bluetoothConnectedHandler
});
});
</script>
Expand Down
2 changes: 2 additions & 0 deletions frontend/z2/pages/data-bike.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/pure-min.css">
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/grids-responsive-min.css">

<link rel="stylesheet" type="text/css" href="/z2/css/notyf/notyf.min.css"/>

<style>
body {
margin: 0 0 20px;
Expand Down
2 changes: 2 additions & 0 deletions frontend/z2/pages/data-hrm.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/pure-min.css">
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/grids-responsive-min.css">

<link rel="stylesheet" type="text/css" href="/z2/css/notyf/notyf.min.css"/>

<style>
body {
margin: 0 0 20px;
Expand Down
2 changes: 2 additions & 0 deletions frontend/z2/pages/data-rower.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/pure-min.css">
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/grids-responsive-min.css">

<link rel="stylesheet" type="text/css" href="/z2/css/notyf/notyf.min.css"/>

<style>
body {
margin: 0 0 20px;
Expand Down
2 changes: 2 additions & 0 deletions frontend/z2/pages/hrm.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/pure-min.css">
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/grids-responsive-min.css">

<link rel="stylesheet" type="text/css" href="/z2/css/notyf/notyf.min.css"/>

<style>
body {
margin: 0 0 20px;
Expand Down
46 changes: 28 additions & 18 deletions frontend/z2/pages/rower.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/pure-min.css">
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/grids-responsive-min.css">

<link rel="stylesheet" type="text/css" href="/z2/css/notyf/notyf.min.css"/>

<style>
body {
margin: 0 0 20px;
Expand Down Expand Up @@ -86,36 +88,44 @@

<script type="module">
import {getEnv} from "/z2/env.js";
import {notify} from "/z2/z2-bundle.js";

const bluetoothConnectedHandler = (() => {
const seen = {};

return (data) => {
const msg = JSON.parse(data);
const key = msg.address;
if (key === "") {
return;
}
if (seen[key]) {
return;
}
seen[key] = true;

const kind = msg.kind;
const name = msg.name ? " " + msg.name : "";

const info = `${kind}${name} is connected`;
console.log(info);
notify("success", info);
};
})();

document.addEventListener("DOMContentLoaded", async () => {
const env = await getEnv();

const seen = {};


env.streamEvents("/events", {
"play-sound": (data) => {
const audio = document.getElementById(`sound-${data}`);
if (audio) {
audio.play();
}
},
"bluetooth-is-connected": (data) => {
const msg = JSON.parse(data);
const key = msg.address;
if (key === "") {
return;
}
if (seen[key]) {
return;
}
seen[key] = true;

const kind = msg.kind;
const name = msg.name ? " " + msg.name : "";

const info = `${kind}${name} is connected`;
console.log(info);
},
"bluetooth-is-connected": bluetoothConnectedHandler
});
});
</script>
Expand Down
2 changes: 2 additions & 0 deletions frontend/z2/pages/sync.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/pure-min.css">
<link rel="stylesheet" type="text/css" href="/z2/css/purecss/grids-responsive-min.css">

<link rel="stylesheet" type="text/css" href="/z2/css/notyf/notyf.min.css"/>

<style>
body {
margin: 0 0 20px;
Expand Down
Loading

0 comments on commit f673a37

Please sign in to comment.