mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-09 13:25: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 city = null;
|
||||||
let countryName = null;
|
let countryName = null;
|
||||||
let countryCode = null;
|
let countryCode = null;
|
||||||
let timezone = null;
|
let timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||||
let chatMessageState = {
|
let chatMessageState = {
|
||||||
newResponseTextEl: null,
|
newResponseTextEl: null,
|
||||||
newResponseEl: null,
|
newResponseEl: null,
|
||||||
|
|||||||
@@ -312,7 +312,7 @@
|
|||||||
let city = null;
|
let city = null;
|
||||||
let countryName = null;
|
let countryName = null;
|
||||||
let countryCode = null;
|
let countryCode = null;
|
||||||
let timezone = null;
|
let timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||||
|
|
||||||
fetch("https://ipapi.co/json")
|
fetch("https://ipapi.co/json")
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
|
|||||||
@@ -33,10 +33,10 @@ interface ChatMessageState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface Location {
|
interface Location {
|
||||||
region: string;
|
region?: string;
|
||||||
city: string;
|
city?: string;
|
||||||
countryName: string;
|
countryName?: string;
|
||||||
countryCode: string;
|
countryCode?: string;
|
||||||
timezone: string;
|
timezone: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ export class KhojChatView extends KhojPaneView {
|
|||||||
result: string;
|
result: string;
|
||||||
setting: KhojSetting;
|
setting: KhojSetting;
|
||||||
waitingForLocation: boolean;
|
waitingForLocation: boolean;
|
||||||
location: Location;
|
location: Location = { timezone: Intl.DateTimeFormat().resolvedOptions().timeZone };
|
||||||
keyPressTimeout: NodeJS.Timeout | null = null;
|
keyPressTimeout: NodeJS.Timeout | null = null;
|
||||||
userMessages: string[] = []; // Store user sent messages for input history cycling
|
userMessages: string[] = []; // Store user sent messages for input history cycling
|
||||||
currentMessageIndex: number = -1; // Track current message index in userMessages array
|
currentMessageIndex: number = -1; // Track current message index in userMessages array
|
||||||
@@ -1058,13 +1058,11 @@ export class KhojChatView extends KhojPaneView {
|
|||||||
n: this.setting.resultsCount,
|
n: this.setting.resultsCount,
|
||||||
stream: true,
|
stream: true,
|
||||||
...(!!conversationId && { conversation_id: conversationId }),
|
...(!!conversationId && { conversation_id: conversationId }),
|
||||||
...(!!this.location && {
|
...(!!this.location && this.location.city && { city: this.location.city }),
|
||||||
city: this.location.city,
|
...(!!this.location && this.location.region && { region: this.location.region }),
|
||||||
region: this.location.region,
|
...(!!this.location && this.location.countryName && { country: this.location.countryName }),
|
||||||
country: this.location.countryName,
|
...(!!this.location && this.location.countryCode && { country_code: this.location.countryCode }),
|
||||||
country_code: this.location.countryCode,
|
...(!!this.location && this.location.timezone && { timezone: this.location.timezone }),
|
||||||
timezone: this.location.timezone,
|
|
||||||
}),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let newResponseEl = this.createKhojResponseDiv();
|
let newResponseEl = this.createKhojResponseDiv();
|
||||||
|
|||||||
@@ -518,12 +518,14 @@ function EditCard(props: EditCardProps) {
|
|||||||
updateQueryUrl += `&subject=${encodeURIComponent(values.subject)}`;
|
updateQueryUrl += `&subject=${encodeURIComponent(values.subject)}`;
|
||||||
}
|
}
|
||||||
updateQueryUrl += `&crontime=${encodeURIComponent(cronFrequency)}`;
|
updateQueryUrl += `&crontime=${encodeURIComponent(cronFrequency)}`;
|
||||||
if (props.locationData) {
|
if (props.locationData && props.locationData.city)
|
||||||
updateQueryUrl += `&city=${encodeURIComponent(props.locationData.city)}`;
|
updateQueryUrl += `&city=${encodeURIComponent(props.locationData.city)}`;
|
||||||
|
if (props.locationData && props.locationData.region)
|
||||||
updateQueryUrl += `®ion=${encodeURIComponent(props.locationData.region)}`;
|
updateQueryUrl += `®ion=${encodeURIComponent(props.locationData.region)}`;
|
||||||
|
if (props.locationData && props.locationData.country)
|
||||||
updateQueryUrl += `&country=${encodeURIComponent(props.locationData.country)}`;
|
updateQueryUrl += `&country=${encodeURIComponent(props.locationData.country)}`;
|
||||||
|
if (props.locationData && props.locationData.timezone)
|
||||||
updateQueryUrl += `&timezone=${encodeURIComponent(props.locationData.timezone)}`;
|
updateQueryUrl += `&timezone=${encodeURIComponent(props.locationData.timezone)}`;
|
||||||
}
|
|
||||||
|
|
||||||
let method = props.createNew ? "POST" : "PUT";
|
let method = props.createNew ? "POST" : "PUT";
|
||||||
|
|
||||||
|
|||||||
@@ -136,7 +136,9 @@ export default function Chat() {
|
|||||||
const [uploadedFiles, setUploadedFiles] = useState<string[]>([]);
|
const [uploadedFiles, setUploadedFiles] = useState<string[]>([]);
|
||||||
const [image64, setImage64] = useState<string>("");
|
const [image64, setImage64] = useState<string>("");
|
||||||
|
|
||||||
const locationData = useIPLocationData();
|
const locationData = useIPLocationData() || {
|
||||||
|
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||||
|
};
|
||||||
const authenticatedData = useAuthenticatedData();
|
const authenticatedData = useAuthenticatedData();
|
||||||
const isMobileWidth = useIsMobileWidth();
|
const isMobileWidth = useIsMobileWidth();
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,10 @@ import { useEffect, useState } from "react";
|
|||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
|
|
||||||
export interface LocationData {
|
export interface LocationData {
|
||||||
ip: string;
|
city?: string;
|
||||||
city: string;
|
region?: string;
|
||||||
region: string;
|
country?: string;
|
||||||
country: string;
|
countryCode?: string;
|
||||||
countryCode: string;
|
|
||||||
postal: string;
|
|
||||||
latitude: number;
|
|
||||||
longitude: number;
|
|
||||||
timezone: string;
|
timezone: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,9 +47,7 @@ export function useIPLocationData() {
|
|||||||
{ revalidateOnFocus: false },
|
{ revalidateOnFocus: false },
|
||||||
);
|
);
|
||||||
|
|
||||||
if (locationDataError) return null;
|
if (locationDataError || !locationData) return;
|
||||||
if (!locationData) return null;
|
|
||||||
|
|
||||||
return locationData;
|
return locationData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -111,7 +111,9 @@ export default function SharedChat() {
|
|||||||
const [paramSlug, setParamSlug] = useState<string | undefined>(undefined);
|
const [paramSlug, setParamSlug] = useState<string | undefined>(undefined);
|
||||||
const [image64, setImage64] = useState<string>("");
|
const [image64, setImage64] = useState<string>("");
|
||||||
|
|
||||||
const locationData = useIPLocationData();
|
const locationData = useIPLocationData() || {
|
||||||
|
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||||
|
};
|
||||||
const authenticatedData = useAuthenticatedData();
|
const authenticatedData = useAuthenticatedData();
|
||||||
const isMobileWidth = useIsMobileWidth();
|
const isMobileWidth = useIsMobileWidth();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user