Skip to content

Commit

Permalink
feat(ui): allow to set a custom browser TZ/LOCALE using env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Jan 17, 2024
1 parent 22bd7ab commit 0b71637
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 22 deletions.
22 changes: 13 additions & 9 deletions .env.app.example
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# The host address to bind to
# HOST="::"
HOST="::"
# The port to listen to for incoming requests. Default is `8091`
# PORT=8091
PORT=''
# The absolute path to the directory where all files will be stored. Default is `<path to your zui dir>/store`
# STORE_DIR
STORE_DIR=''
# Used as secret for session. If not provided the default one is used
# SESSION_SECRET
SESSION_SECRET="zwavejsisawesome"


# NETWORK_KEY
# HTTPS=true
# S0 Key
NETWORK_KEY=''
HTTPS=''
# Set the cookie [secure](https://github.com/expressjs/session#cookiesecure) option.
# USE_SECURE_COOKIE
USE_SECURE_COOKIE=''
# Mostly needed for docker users.
# https://zwave-js.github.io/node-zwave-js/#/usage/external-config?id=specifying-an-external-config-db-location
# ZWAVEJS_EXTERNAL_CONFIG
ZWAVEJS_EXTERNAL_CONFIG=''

# Browser preferred locale/tz to use for date/time formatting
TZ=''
LOCALE=''
2 changes: 2 additions & 0 deletions api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,8 @@ app.get(
serial_ports: [],
scales: scales,
sslDisabled: sslDisabled(),
tz: process.env.TZ,
locale: process.env.LOCALE,
deprecationWarning: process.env.TAG_NAME === 'zwavejs2mqtt',
}

Expand Down
2 changes: 2 additions & 0 deletions docs/guide/env-vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ This is the list of the supported environment variables:
- `FORCE_DISABLE_SSL`: Set this env var to `'true'` to disable SSL.
- `BASE_PATH`: Set this env var to the base path where the application is served. Default is `/`.
- `UID_DISCOVERY_PREFIX`: Sets the prefix used for MQTT Discovery `unique_id` of entities. Default is `zwavejs2mqtt_`.
- `TZ`: Set this env var to the timezone you want to use on UI. Default to browser TZ.
- `LOCALE`: Set this env var to the locale you want to use on UI. Default to browser locale.
2 changes: 1 addition & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ export default {
return
}
this.showNodesManager('')
this.$refs.nodesManager.onGrantSecurityCC(requested)
this.$refs.nPodesManager.onGrantSecurityCC(requested)
},
...mapActions(useBaseStore, [
'init',
Expand Down
5 changes: 4 additions & 1 deletion src/components/custom/StatisticsArrows.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<i
>{{
node.lastActive
? new Date(node.lastActive).toLocaleString()
? getDateTimeString(node.lastActive)
: 'Never'
}}
</i>
Expand All @@ -31,6 +31,8 @@

<script>
import { jsonToList } from '@/lib/utils'
import { mapActions } from 'pinia'
import useBaseStore from '../../stores/base.js'
export default {
props: {
Expand All @@ -49,6 +51,7 @@ export default {
}
},
methods: {
...mapActions(useBaseStore, ['getDateTimeString']),
jsonToList(item) {
return jsonToList(item, {
ignore: ['lwr', 'nlwr', 'rssi', 'backgroundRSSI', 'lastSeen'],
Expand Down
58 changes: 47 additions & 11 deletions src/components/nodes-table/ExpandedNode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
<v-tooltip bottom>
<template v-slot:activator="{ on, attrs }">
<v-btn
@click="toggleAutoScroll"
@click="toggleAutoScroll()"
icon
:color="autoScroll ? 'primary' : ''"
:class="
Expand All @@ -241,6 +241,28 @@
</template>
<span>Enable/Disable auto scroll</span>
</v-tooltip>

<v-tooltip bottom>
<template v-slot:activator="{ on, attrs }">
<v-btn
@click="toggleSort()"
icon
:color="
inverseSort ? 'primary' : ''
"
:class="
inverseSort
? 'border-primary'
: ''
"
v-bind="attrs"
v-on="on"
>
<v-icon>swap_vert</v-icon>
</v-btn>
</template>
<span>Inverse Sort</span>
</v-tooltip>
</template>
</v-text-field>

Expand All @@ -252,7 +274,7 @@
>
<span
><i>{{
new Date(event.time).toISOString()
getDateTimeString(event.time)
}}</i></span
>
-
Expand Down Expand Up @@ -378,14 +400,20 @@ export default {
return this.showStatistics ? 'border-primary' : ''
},
filteredNodeEvents() {
return this.node.eventsQueue.filter((event) => {
return (
!this.searchEvents ||
JSON.stringify(event)
.toLowerCase()
.includes(this.searchEvents.toLowerCase())
)
})
return this.node.eventsQueue
.filter((event) => {
return (
!this.searchEvents ||
JSON.stringify(event)
.toLowerCase()
.includes(this.searchEvents.toLowerCase())
)
})
.sort((a, b) => {
a = new Date(a.time)
b = new Date(b.time)
return this.inverseSort ? b - a : a - b
})
},
advancedActions() {
const nodeActions = this.node.isControllerNode
Expand Down Expand Up @@ -558,13 +586,18 @@ export default {
return {
currentTab: 0,
autoScroll: true,
inverseSort: false,
searchEvents: '',
advancedShowDialog: false,
showStatistics: false,
}
},
methods: {
...mapActions(useBaseStore, ['showSnackbar', 'setValue']),
...mapActions(useBaseStore, [
'showSnackbar',
'setValue',
'getDateTimeString',
]),
async updateValue(v, customValue) {
if (v) {
// in this way I can check when the value receives an update
Expand Down Expand Up @@ -704,6 +737,9 @@ export default {
toggleAutoScroll() {
this.autoScroll = !this.autoScroll
},
toggleSort() {
this.inverseSort = !this.inverseSort
},
async scrollBottom() {
if (!this.autoScroll) {
return
Expand Down
19 changes: 19 additions & 0 deletions src/stores/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const useBaseStore = defineStore('base', {
nodes: [],
nodesMap: new Map(),
user: {},
tz: Intl.DateTimeFormat().resolvedOptions().timeZone,
locale: undefined, // uses default browser locale
zwave: {
port: '/dev/zwave',
allowBootloaderOnly: false,
Expand Down Expand Up @@ -112,6 +114,15 @@ const useBaseStore = defineStore('base', {
},
},
actions: {
getDateTimeString(date) {
if (typeof date === 'string' || typeof date === 'number') {
date = new Date(date)
}

return date.toLocaleString(this.locale, {
timeZone: this.tz,
})
},
getNode(id) {
if (typeof id === 'string') {
id = parseInt(id)
Expand Down Expand Up @@ -493,6 +504,14 @@ const useBaseStore = defineStore('base', {
},
init(data) {
if (data) {
if (data.tz) {
this.tz = data.tzd
}

if (data.locale) {
this.locale = data.locale
}

this.initSettings(data.settings)
this.initPorts(data.serial_ports)
this.initScales(data.scales)
Expand Down

0 comments on commit 0b71637

Please sign in to comment.