mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-05 05:39:11 +00:00
Default to using system clock to infer user timezone on js clients
Using system clock to infer user timezone on clients makes Khoj more robust to provide location aware responses. Previously only ip based location was used to infer timezone via API. This didn't provide any decent fallback when calls to ipapi failed or Khoj was being run in offline mode
This commit is contained in:
@@ -61,7 +61,7 @@
|
||||
let city = null;
|
||||
let countryName = null;
|
||||
let countryCode = null;
|
||||
let timezone = null;
|
||||
let timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
let chatMessageState = {
|
||||
newResponseTextEl: null,
|
||||
newResponseEl: null,
|
||||
|
||||
@@ -312,7 +312,7 @@
|
||||
let city = null;
|
||||
let countryName = null;
|
||||
let countryCode = null;
|
||||
let timezone = null;
|
||||
let timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
|
||||
fetch("https://ipapi.co/json")
|
||||
.then(response => response.json())
|
||||
|
||||
@@ -33,10 +33,10 @@ interface ChatMessageState {
|
||||
}
|
||||
|
||||
interface Location {
|
||||
region: string;
|
||||
city: string;
|
||||
countryName: string;
|
||||
countryCode: string;
|
||||
region?: string;
|
||||
city?: string;
|
||||
countryName?: string;
|
||||
countryCode?: string;
|
||||
timezone: string;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ export class KhojChatView extends KhojPaneView {
|
||||
result: string;
|
||||
setting: KhojSetting;
|
||||
waitingForLocation: boolean;
|
||||
location: Location;
|
||||
location: Location = { timezone: Intl.DateTimeFormat().resolvedOptions().timeZone };
|
||||
keyPressTimeout: NodeJS.Timeout | null = null;
|
||||
userMessages: string[] = []; // Store user sent messages for input history cycling
|
||||
currentMessageIndex: number = -1; // Track current message index in userMessages array
|
||||
@@ -1058,13 +1058,11 @@ export class KhojChatView extends KhojPaneView {
|
||||
n: this.setting.resultsCount,
|
||||
stream: true,
|
||||
...(!!conversationId && { conversation_id: conversationId }),
|
||||
...(!!this.location && {
|
||||
city: this.location.city,
|
||||
region: this.location.region,
|
||||
country: this.location.countryName,
|
||||
country_code: this.location.countryCode,
|
||||
timezone: this.location.timezone,
|
||||
}),
|
||||
...(!!this.location && this.location.city && { city: this.location.city }),
|
||||
...(!!this.location && this.location.region && { region: this.location.region }),
|
||||
...(!!this.location && this.location.countryName && { country: this.location.countryName }),
|
||||
...(!!this.location && this.location.countryCode && { country_code: this.location.countryCode }),
|
||||
...(!!this.location && this.location.timezone && { timezone: this.location.timezone }),
|
||||
};
|
||||
|
||||
let newResponseEl = this.createKhojResponseDiv();
|
||||
|
||||
@@ -518,12 +518,14 @@ function EditCard(props: EditCardProps) {
|
||||
updateQueryUrl += `&subject=${encodeURIComponent(values.subject)}`;
|
||||
}
|
||||
updateQueryUrl += `&crontime=${encodeURIComponent(cronFrequency)}`;
|
||||
if (props.locationData) {
|
||||
if (props.locationData && props.locationData.city)
|
||||
updateQueryUrl += `&city=${encodeURIComponent(props.locationData.city)}`;
|
||||
if (props.locationData && props.locationData.region)
|
||||
updateQueryUrl += `®ion=${encodeURIComponent(props.locationData.region)}`;
|
||||
if (props.locationData && props.locationData.country)
|
||||
updateQueryUrl += `&country=${encodeURIComponent(props.locationData.country)}`;
|
||||
if (props.locationData && props.locationData.timezone)
|
||||
updateQueryUrl += `&timezone=${encodeURIComponent(props.locationData.timezone)}`;
|
||||
}
|
||||
|
||||
let method = props.createNew ? "POST" : "PUT";
|
||||
|
||||
|
||||
@@ -136,7 +136,9 @@ export default function Chat() {
|
||||
const [uploadedFiles, setUploadedFiles] = useState<string[]>([]);
|
||||
const [image64, setImage64] = useState<string>("");
|
||||
|
||||
const locationData = useIPLocationData();
|
||||
const locationData = useIPLocationData() || {
|
||||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||
};
|
||||
const authenticatedData = useAuthenticatedData();
|
||||
const isMobileWidth = useIsMobileWidth();
|
||||
|
||||
|
||||
@@ -2,14 +2,10 @@ import { useEffect, useState } from "react";
|
||||
import useSWR from "swr";
|
||||
|
||||
export interface LocationData {
|
||||
ip: string;
|
||||
city: string;
|
||||
region: string;
|
||||
country: string;
|
||||
countryCode: string;
|
||||
postal: string;
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
city?: string;
|
||||
region?: string;
|
||||
country?: string;
|
||||
countryCode?: string;
|
||||
timezone: string;
|
||||
}
|
||||
|
||||
@@ -51,9 +47,7 @@ export function useIPLocationData() {
|
||||
{ revalidateOnFocus: false },
|
||||
);
|
||||
|
||||
if (locationDataError) return null;
|
||||
if (!locationData) return null;
|
||||
|
||||
if (locationDataError || !locationData) return;
|
||||
return locationData;
|
||||
}
|
||||
|
||||
|
||||
@@ -111,7 +111,9 @@ export default function SharedChat() {
|
||||
const [paramSlug, setParamSlug] = useState<string | undefined>(undefined);
|
||||
const [image64, setImage64] = useState<string>("");
|
||||
|
||||
const locationData = useIPLocationData();
|
||||
const locationData = useIPLocationData() || {
|
||||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||
};
|
||||
const authenticatedData = useAuthenticatedData();
|
||||
const isMobileWidth = useIsMobileWidth();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user