@@ -3,8 +3,7 @@ import { useState, useEffect } from "react";
33 export function useSettings(onError?: (error: string) => void) {
44 const [selectedModel, setSelectedModel] = useState<string>("gpt-oss");
55 const [maxPromptLength, setMaxPromptLength] = useState<"none" | 1024 | 4096>("none");
6- const [location, setLocation] = useState<string | undefined>(undefined);
7- const [currentTime, setCurrentTime] = useState<string | undefined>(undefined);
6+ const [location, setLocation] = useState<string>("");
87
98 // Load settings from API and validate model against available models
109 useEffect(() => {
@@ -24,8 +23,7 @@ export function useSettings(onError?: (error: string) => void) {
2423 const settings = settingsData.settings || {};
2524 const savedModel = settings.selectedModel || "";
2625 const savedMaxPromptLength = settings.maxPromptLength || "none";
27- const savedLocation = settings.location;
28- const savedCurrentTime = settings.currentTime;
26+ const savedLocation = settings.location || "";
2927
3028 // Get available models
3129 const modelsRes = await fetch("/api/models");
@@ -42,19 +40,25 @@ export function useSettings(onError?: (error: string) => void) {
4240
4341 // Set maxPromptLength
4442 setMaxPromptLength(savedMaxPromptLength);
45- // Set location and currentTime
46- setLocation(savedLocation);
47- setCurrentTime(savedCurrentTime);
4843
49- // Post current time in ISO format to settings on app start
44+ // Get browser timezone and post current time in ISO format to settings on app start
45+ const browserTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
5046 const currentTimeISO = new Date().toISOString();
47+
48+ // Use timezone as default location if no location is set
49+ const locationToUse = savedLocation || browserTimezone;
50+ setLocation(locationToUse);
51+
5152 try {
5253 await fetch("/api/settings", {
5354 method: "POST",
5455 headers: { "Content-Type": "application/json" },
55- body: JSON.stringify({ currentTime: currentTimeISO }),
56+ body: JSON.stringify({
57+ currentTime: currentTimeISO,
58+ timezone: browserTimezone,
59+ ...(savedLocation ? {} : { location: browserTimezone }),
60+ }),
5661 });
57- setCurrentTime(currentTimeISO);
5862 } catch (error) {
5963 // Silently fail - don't block app startup if time update fails
6064 if (onError && error instanceof Error) {
@@ -94,8 +98,7 @@ export function useSettings(onError?: (error: string) => void) {
9498 // On error, keep defaults
9599 setSelectedModel("");
96100 setMaxPromptLength("none");
97- setLocation(undefined);
98- setCurrentTime(undefined);
101+ setLocation("");
99102 }
100103 };
101104
@@ -150,11 +153,36 @@ export function useSettings(onError?: (error: string) => void) {
150153 }
151154 };
152155
156+ const handleLocationChange = async (value: string) => {
157+ setLocation(value);
158+ // Save to API
159+ try {
160+ const res = await fetch("/api/settings", {
161+ method: "POST",
162+ headers: { "Content-Type": "application/json" },
163+ body: JSON.stringify({ location: value }),
164+ });
165+ if (!res.ok) {
166+ const errorData = await res.json().catch((parseError) => {
167+ throw new Error(
168+ `Failed to parse error response: ${parseError instanceof Error ? parseError.message : "Invalid JSON"}`
169+ );
170+ });
171+ throw new Error(errorData.error || res.statusText);
172+ }
173+ } catch (error) {
174+ if (onError && error instanceof Error) {
175+ onError(error.message);
176+ }
177+ }
178+ };
179+
153180 return {
154181 selectedModel,
155182 handleModelChange,
156183 maxPromptLength,
157184 handleMaxPromptLengthChange,
185+ location,
186+ handleLocationChange,
158187 };
159188 }
160-