Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(settings): show the saved zone without a re-read; commit only on save

M3gA-Mind55c0e426cd4c
diff --git a/app/src/components/TimezoneSelect.test.tsx b/app/src/components/TimezoneSelect.test.tsx
index 8aba2e6054..8f35be2d12 100644
--- a/app/src/components/TimezoneSelect.test.tsx
+++ b/app/src/components/TimezoneSelect.test.tsx
@@ -35,10 +35,8 @@ describe('TimezoneSelect', () => {
).toBeTruthy();
});
- it('saves a chosen zone and re-reads the setting', async () => {
- getMock
- .mockResolvedValueOnce(settings(null))
- .mockResolvedValueOnce(settings('America/New_York'));
+ it('saves a chosen zone and shows it without a second read', async () => {
+ getMock.mockResolvedValue(settings(null));
updateMock.mockResolvedValue({ result: {}, logs: [] });
render();
const select = (await screen.findByTestId('timezone-select')) as HTMLSelectElement;
@@ -47,8 +45,9 @@ describe('TimezoneSelect', () => {
fireEvent.change(select, { target: { value: 'America/New_York' } });
await waitFor(() => expect(updateMock).toHaveBeenCalledWith('America/New_York'));
- await waitFor(() => expect(getMock).toHaveBeenCalledTimes(2));
+ await waitFor(() => expect(select.disabled).toBe(false));
expect(select.value).toBe('America/New_York');
+ expect(getMock).toHaveBeenCalledTimes(1);
});
it('following the device sends null', async () => {
@@ -72,19 +71,13 @@ describe('TimezoneSelect', () => {
expect(select.value).toBe('Europe/Berlin');
});
- it('a failed re-read after a successful save keeps the new zone, without an error', async () => {
- getMock
- .mockResolvedValueOnce(settings('Europe/Berlin'))
- .mockRejectedValueOnce(new Error('core offline'));
- updateMock.mockResolvedValue({ result: {}, logs: [] });
+ it('a saved value that is not an Area/Location zone shows as following the device', async () => {
+ getMock.mockResolvedValue(settings('EST'));
render();
const select = (await screen.findByTestId('timezone-select')) as HTMLSelectElement;
- await waitFor(() => expect(select.value).toBe('Europe/Berlin'));
- fireEvent.change(select, { target: { value: 'Asia/Tokyo' } });
- await waitFor(() => expect(getMock).toHaveBeenCalledTimes(2));
await waitFor(() => expect(select.disabled).toBe(false));
- expect(select.value).toBe('Asia/Tokyo');
- expect(screen.queryByTestId('timezone-error')).toBeNull();
+ expect(select.value).toBe('');
+ expect(screen.queryByRole('option', { name: 'EST' })).toBeNull();
});
it('is disabled while a save is in flight, so picks cannot overlap', async () => {
diff --git a/app/src/components/TimezoneSelect.tsx b/app/src/components/TimezoneSelect.tsx
index d09997ced8..5e119d60b8 100644
--- a/app/src/components/TimezoneSelect.tsx
+++ b/app/src/components/TimezoneSelect.tsx
@@ -11,6 +11,10 @@ import { SELECT_CLASS } from './LanguageSelect';
/** The option that follows the device's zone instead of a chosen one. */
const FOLLOW_DEVICE = '';
+function isAreaZone(zone: string | null | undefined): zone is string {
+ return !!zone && (zone === 'UTC' || zone.includes('/'));
+}
+
function ianaZones(): string[] {
try {
return Intl.supportedValuesOf('timeZone');
@@ -50,11 +54,13 @@ const TimezoneSelect = ({ ariaLabel }: TimezoneSelectProps) => {
};
}, []);
+ // Only an Area/Location zone (or UTC) is ever offered; a saved value that is
+ // not one shows as "follow the device", which is what core resolves it to.
+ const chosen = isAreaZone(settings?.timezone) ? settings?.timezone : null;
const zones = useMemo(() => {
const listed = ianaZones();
- const chosen = settings?.timezone;
return chosen && !listed.includes(chosen) ? [chosen, ...listed] : listed;
- }, [settings?.timezone]);
+ }, [chosen]);
const change = async (value: string) => {
const timezone = value === FOLLOW_DEVICE ? null : value;
@@ -72,12 +78,11 @@ const TimezoneSelect = ({ ariaLabel }: TimezoneSelectProps) => {
setFailed(true);
return;
}
- // Saved. A failed re-read leaves the chosen zone shown: it is stored.
- try {
- setSettings((await openhumanGetUserTimezone()).result);
- } catch {
- // keep the optimistic value
- }
+ // Saved: show exactly what was stored. No re-read, so a late or
+ // stale read can never put the previous zone back on screen.
+ setSettings(current =>
+ current ? { ...current, timezone, effective: timezone ?? current.device ?? 'UTC' } : current
+ );
} finally {
setSaving(false);
}
@@ -87,7 +92,7 @@ const TimezoneSelect = ({ ariaLabel }: TimezoneSelectProps) => {
return (
<div className="flex flex-col items-end gap-1">
<select
- value={settings?.timezone ?? FOLLOW_DEVICE}
+ value={chosen ?? FOLLOW_DEVICE}
onChange={e => void change(e.target.value)}
disabled={!settings || saving}
aria-label={ariaLabel ?? t('settings.timezone')}
diff --git a/crates/openhuman-core/src/config/ops/ui.rs b/crates/openhuman-core/src/config/ops/ui.rs
index 395eb1bee9..c54070f43f 100644
--- a/crates/openhuman-core/src/config/ops/ui.rs
+++ b/crates/openhuman-core/src/config/ops/ui.rs
@@ -186,17 +186,30 @@ pub async fn apply_user_timezone(
timezone: Option<String>,
) -> Result<Outcome<serde_json::Value>, String> {
let requested = timezone.as_deref().map(str::trim).filter(|z| !z.is_empty());
- config.user_timezone = match requested {
+ let next = match requested {
None => None,
Some(zone) => Some(
crate::config::normalize_time_zone(zone)
.ok_or_else(|| format!("{zone:?} is not an IANA time zone (e.g. Asia/Kolkata)"))?,
),
};
- config.save().await.map_err(|e| e.to_string())?;
+ // Build the reply before committing, so nothing after the save can fail
+ // and report a stored change as failed; a failed save restores the
+ // caller's config to what is on disk.
+ let previous = std::mem::replace(&mut config.user_timezone, next);
+ let snapshot = match snapshot_config_json(config) {
+ Ok(snapshot) => snapshot,
+ Err(error) => {
+ config.user_timezone = previous;
+ return Err(error);
+ }
+ };
+ if let Err(error) = config.save().await {
+ config.user_timezone = previous;
+ return Err(error.to_string());
+ }
let chosen = config.user_timezone.as_deref().unwrap_or("device");
log::info!("[config] user time zone set: {chosen}");
- let snapshot = snapshot_config_json(config)?;
Ok(Outcome::new(
snapshot,
vec![format!(
diff --git a/crates/openhuman-core/src/config/ops_model_and_local_ai_tests.rs b/crates/openhuman-core/src/config/ops_model_and_local_ai_tests.rs
index 257b3b77a5..74345d57d4 100644
--- a/crates/openhuman-core/src/config/ops_model_and_local_ai_tests.rs
+++ b/crates/openhuman-core/src/config/ops_model_and_local_ai_tests.rs
@@ -698,3 +698,19 @@ async fn apply_user_timezone_stores_the_canonical_name_refuses_junk_and_clears()
assert!(json["timezone"].is_null());
assert_eq!(json["effective"], cfg.time_zone());
}
+
+#[tokio::test]
+async fn apply_user_timezone_restores_the_value_when_the_save_fails() {
+ let tmp = tempdir().unwrap();
+ let mut cfg = tmp_config(&tmp);
+ cfg.user_timezone = Some("Europe/Berlin".into());
+ // A config path inside a regular file cannot be written.
+ let blocker = tmp.path().join("not-a-dir");
+ std::fs::write(&blocker, "x").unwrap();
+ cfg.config_path = blocker.join("config.toml");
+
+ apply_user_timezone(&mut cfg, Some("Asia/Kolkata".into()))
+ .await
+ .expect_err("the save cannot succeed");
+ assert_eq!(cfg.user_timezone.as_deref(), Some("Europe/Berlin"));
+}