import * as Uebersicht from "uebersicht";
import * as DataWidget from "./data-widget.jsx";
import * as DataWidgetLoader from "./data-widget-loader.jsx";
import useWidgetRefresh from "../../hooks/use-widget-refresh";
import useServerSocket from "../../hooks/use-server-socket";
import { useSimpleBarContext } from "../simple-bar-context.jsx";
import * as Utils from "../../utils";
export { timeStyles as styles } from "../../styles/components/data/time";
const { React } = Uebersicht;
const DEFAULT_REFRESH_FREQUENCY = 1000;
/**
* Time widget component.
* @returns {JSX.Element|null} The rendered widget.
*/
export const Widget = React.memo(() => {
const { displayIndex, settings } = useSimpleBarContext();
const { widgets, timeWidgetOptions } = settings;
const { timeWidget } = widgets;
const {
refreshFrequency,
hour12,
dayProgress,
showSeconds,
showOnDisplay,
showIcon,
} = timeWidgetOptions;
// Determine if the widget should be visible on the current display
const visible =
Utils.isVisibleOnDisplay(displayIndex, showOnDisplay) && timeWidget;
// Calculate the refresh frequency for the widget
const refresh = React.useMemo(
() =>
Utils.getRefreshFrequency(refreshFrequency, DEFAULT_REFRESH_FREQUENCY),
[refreshFrequency],
);
const [state, setState] = React.useState();
const [loading, setLoading] = React.useState(visible);
// Options for formatting the time string
const options = React.useMemo(
() => ({
hour: "numeric",
minute: "numeric",
second: showSeconds ? "numeric" : undefined,
hour12,
}),
[hour12, showSeconds],
);
/**
* Resets the widget state.
*/
const resetWidget = () => {
setState(undefined);
setLoading(false);
};
/**
* Fetches the current time and updates the widget state.
*/
const getTime = React.useCallback(() => {
if (!visible) return;
const time = new Date().toLocaleString("en-UK", options);
setState({ time });
setLoading(false);
}, [visible, options]);
// Use server socket to get time updates
useServerSocket("time", visible, getTime, resetWidget, setLoading);
// Refresh the widget at the specified interval
useWidgetRefresh(visible, getTime, refresh);
if (loading) return