diff --git a/gui/src/components/tracker/TrackerBattery.tsx b/gui/src/components/tracker/TrackerBattery.tsx index c518a9a1e..d2136d444 100644 --- a/gui/src/components/tracker/TrackerBattery.tsx +++ b/gui/src/components/tracker/TrackerBattery.tsx @@ -1,3 +1,4 @@ +import { useLocaleConfig } from '../../i18n/config'; import { BatteryIcon } from '../commons/icon/BatteryIcon'; import { Typography } from '../commons/Typography'; @@ -6,28 +7,60 @@ export function TrackerBattery({ voltage, disabled, textColor = 'secondary', + deviceId, }: { + /** + * a [0, 1] value range is expected + */ value: number; voltage?: number | null; disabled?: boolean; textColor?: string; + deviceId: number | null; }) { - const percent = value * 100; + const { currentLocales } = useLocaleConfig(); + const percentFormatter = new Intl.NumberFormat(currentLocales, { + style: 'percent', + }); + const voltageFormatter = new Intl.NumberFormat(currentLocales, { + minimumFractionDigits: 2, + }); + + const oldBatteryInfo = localStorage.getItem(`deviceBattery_${deviceId}`); + + if (!disabled) { + const batteryInfo: BatteryInfo = { value, voltage }; + localStorage.setItem( + `deviceBattery_${deviceId}`, + JSON.stringify(batteryInfo) + ); + } else if (oldBatteryInfo) { + const batteryInfo: BatteryInfo = JSON.parse(oldBatteryInfo); + value = batteryInfo.value; + voltage = batteryInfo.voltage; + } + + // Why did I name it like this + const actuallyDisabled = disabled && !oldBatteryInfo; return (
- {!disabled && ( + {!actuallyDisabled && (
- {percent.toFixed(0)} % + + {percentFormatter.format(value)} + {voltage && ( - {voltage.toFixed(2)} V + + {voltageFormatter.format(voltage)} V + )}
)} - {disabled && ( + {actuallyDisabled && (
@@ -35,3 +68,8 @@ export function TrackerBattery({
); } + +interface BatteryInfo { + value: number; + voltage?: number | null; +} diff --git a/gui/src/components/tracker/TrackerCard.tsx b/gui/src/components/tracker/TrackerCard.tsx index b92ab1e61..0d081b2d0 100644 --- a/gui/src/components/tracker/TrackerCard.tsx +++ b/gui/src/components/tracker/TrackerCard.tsx @@ -41,6 +41,7 @@ function TrackerBig({ )}
@@ -87,6 +88,7 @@ function TrackerSmol({ )}
diff --git a/gui/src/components/tracker/TrackersTable.tsx b/gui/src/components/tracker/TrackersTable.tsx index f506a78a2..69192e82a 100644 --- a/gui/src/components/tracker/TrackersTable.tsx +++ b/gui/src/components/tracker/TrackersTable.tsx @@ -268,6 +268,7 @@ export function TrackersTable({ voltage={device.hardwareStatus?.batteryVoltage} disabled={tracker.status === TrackerStatusEnum.DISCONNECTED} textColor={fontColor} + deviceId={device.id?.id || null} /> ), })} diff --git a/gui/src/i18n/config.tsx b/gui/src/i18n/config.tsx index 6624296a6..94e4e6711 100644 --- a/gui/src/i18n/config.tsx +++ b/gui/src/i18n/config.tsx @@ -1,7 +1,15 @@ import { match } from '@formatjs/intl-localematcher'; import { FluentBundle, FluentResource } from '@fluent/bundle'; import { LocalizationProvider, ReactLocalization } from '@fluent/react'; -import { Children, ReactNode, useEffect, useState, createContext } from 'react'; +import { + Children, + ReactNode, + useEffect, + useState, + createContext, + useContext, +} from 'react'; +import { ConfigContext, ConfigContextC } from '../hooks/config'; export const defaultNS = 'translation'; export const DEFAULT_LOCALE = 'en'; @@ -153,3 +161,13 @@ export function AppLocalizationProvider(props: AppLocalizationProviderProps) { ); } + +export function useLocaleConfig() { + const context = useContext(LangContext); + if (!context) { + throw new Error( + 'useLocaleConfig must be within a AppLocalization Provider' + ); + } + return context; +}