Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

1.7 KB
Raw Blame
import { useState, useEffect } from 'react';
import { getSettings } from '../services/api';
import { isValidTimezone } from '../utils/timezone.js';

/**
 * Resolve the user's CONFIGURED IANA timezone (`settings.timezone`) once on
 * mount, so date-scoped POST surfaces derive "today" the same way the server
 * does (issue #2681). The server stamps and windows POST records on the
 * configured local day; a client that keyed off `new Date().toISOString()` (UTC)
 * would disagree around the local/UTC midnight boundary.
 *
 * Fallback chain mirrors the server's `getUserTimezone`: configured tz →
 * browser's own IANA zone (which `useTimezoneBootstrap` in App.jsx seeds into
 * settings on first load, so the two normally match) → 'UTC'. A transient
 * settings-fetch failure fails open to the browser zone rather than blocking
 * render. Starts at the browser zone so the first paint is already close.
 *
 * @returns {string} IANA timezone string
 */
export default function useUserTimezone() {
  const browserZone = (() => {
    try {
      return Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC';
    } catch {
      return 'UTC';
    }
  })();
  const [timezone, setTimezone] = useState(browserZone);

  useEffect(() => {
    getSettings({ silent: true })
      .then((s) => {
        const tz = s?.timezone;
        if (!tz) return; // unconfigured: keep the browser zone (self-heal seeds it)
        // A hand-edited/invalid configured tz makes the server fall back to its
        // process timezone (UTC under TZ=UTC). Match that instead of letting
        // dayKeyInTimezone silently drift to the browser zone (issue #2681 r4).
        setTimezone(isValidTimezone(tz) ? tz : 'UTC');
      })
      .catch(() => {});
  }, []);

  return timezone;
}