From 3f90e069656e78a745030204ed4f927e035a014b Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Fri, 13 Feb 2026 02:04:48 +0000 Subject: [PATCH] Implemented weather formatting. --- .../DashboardWidgets/WeatherWidget.jsx | 30 +++++++++++++------ .../components/dashboard/controls/Widget.jsx | 6 ++-- .../components/dashboard/controls/widgets.js | 29 ++++++++++-------- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/packages/client/src/components/dashboard/controls/DashboardWidgets/WeatherWidget.jsx b/packages/client/src/components/dashboard/controls/DashboardWidgets/WeatherWidget.jsx index c41b62a..07fbfc9 100644 --- a/packages/client/src/components/dashboard/controls/DashboardWidgets/WeatherWidget.jsx +++ b/packages/client/src/components/dashboard/controls/DashboardWidgets/WeatherWidget.jsx @@ -18,7 +18,14 @@ const getWeatherCondition = (code) => { return "Unknown"; }; -export const WeatherWidget = ({ widget }) => { +// Format weather string using template with placeholders +const formatWeather = (format, data) => { + return format.replace(/\{(\w+)\}/g, (match, key) => { + return data[key] !== undefined ? data[key] : match; + }); +}; + +export const WeatherWidget = ({ widget, widgetDef }) => { const { weatherData } = useWebSocket(); // Get weather data from widget or context @@ -33,17 +40,22 @@ export const WeatherWidget = ({ widget }) => { } const current = weather.current; - const temp = current.temperature_2m?.toFixed(1) || "N/A"; - const unit = current.temperature_2m !== undefined ? "°C" : ""; - const humidity = current.relative_humidity_2m?.toFixed(0) || "N/A"; - const windSpeed = current.wind_speed_10m?.toFixed(1) || "N/A"; - const weatherCode = current.weather_code; + + // Prepare data for formatting + const data = { + temperature: current.temperature_2m?.toFixed(1) || "N/A", + humidity: current.relative_humidity_2m?.toFixed(0) || "N/A", + windSpeed: current.wind_speed_10m?.toFixed(1) || "N/A", + weatherCondition: getWeatherCondition(current.weather_code), + }; + + const defaultFormat = widgetDef?.properties?.format || "{weatherCondition}, {temperature}°C, {humidity}%"; + const format = widget.format || defaultFormat; + const formattedWeather = formatWeather(format, data); return (
-
- {getWeatherCondition(weatherCode)}, {temp}{unit}, {humidity}%, {windSpeed}m/s -
+
{formattedWeather}
); }; diff --git a/packages/client/src/components/dashboard/controls/Widget.jsx b/packages/client/src/components/dashboard/controls/Widget.jsx index 9c97b62..5d52096 100644 --- a/packages/client/src/components/dashboard/controls/Widget.jsx +++ b/packages/client/src/components/dashboard/controls/Widget.jsx @@ -9,7 +9,9 @@ import { FaTimes } from "react-icons/fa"; import { RiPencilFill } from "react-icons/ri"; import widgets from "./widgets"; import "./Widget.css"; -import { DateTimeWidget, WeatherWidget } from "./DashboardWidgets"; + +import DateTimeWidget from "./DashboardWidgets/DateTimeWidget"; +import WeatherWidget from "./DashboardWidgets/WeatherWidget"; export const Widget = ({ widget, @@ -142,7 +144,7 @@ export const Widget = ({ case "dateTime": return ; case "weather": - return ; + return ; default: return (
diff --git a/packages/client/src/components/dashboard/controls/widgets.js b/packages/client/src/components/dashboard/controls/widgets.js index 841bf49..a443749 100644 --- a/packages/client/src/components/dashboard/controls/widgets.js +++ b/packages/client/src/components/dashboard/controls/widgets.js @@ -1,16 +1,19 @@ const widgets = [ - { - name: "dateTime", - label: "Date & Time", - properties: { - format: "MM/DD/YYYY HH:mm", - } + { + name: "dateTime", + label: "Date & Time", + properties: { + format: "MM/DD/YYYY HH:mm", }, - { - name: "weather", - label: "Weather", - properties: {} - } -] + }, + { + name: "weather", + label: "Weather", + properties: { + format: + "{weatherCondition}, {temperature}°C, {humidity}%, {windSpeed}m/s", + }, + }, +]; -export default widgets; \ No newline at end of file +export default widgets;