mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-08 05:39:13 +00:00
Only show greeting once userConfig is fetched from server
- Pass userConfig from Home as prop to chatBodyData component with loading state - Pass loading state of userConfig to allow components to handle rendering dependent elements once it is loaded
This commit is contained in:
@@ -71,9 +71,9 @@ export interface UserConfig {
|
|||||||
|
|
||||||
export function useUserConfig(detailed: boolean = false) {
|
export function useUserConfig(detailed: boolean = false) {
|
||||||
const url = `/api/settings?detailed=${detailed}`;
|
const url = `/api/settings?detailed=${detailed}`;
|
||||||
const { data, error } = useSWR<UserConfig>(url, fetcher, { revalidateOnFocus: false });
|
const { data: userConfig, error, isLoading: isLoadingUserConfig } = useSWR<UserConfig>(url, fetcher, { revalidateOnFocus: false });
|
||||||
|
|
||||||
if (error || !data || data.detail === 'Forbidden') return null;
|
if (error || !userConfig || userConfig?.detail === 'Forbidden') return {userConfig: null, isLoadingUserConfig};
|
||||||
|
|
||||||
return data;
|
return {userConfig, isLoadingUserConfig};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import './globals.css';
|
|||||||
import styles from './page.module.css';
|
import styles from './page.module.css';
|
||||||
import 'katex/dist/katex.min.css';
|
import 'katex/dist/katex.min.css';
|
||||||
|
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { use, useEffect, useState } from 'react';
|
||||||
import useSWR from 'swr';
|
import useSWR from 'swr';
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import { ClockCounterClockwise } from '@phosphor-icons/react';
|
import { ClockCounterClockwise } from '@phosphor-icons/react';
|
||||||
@@ -16,7 +16,7 @@ import Loading from '@/app/components/loading/loading';
|
|||||||
import ChatInputArea, { ChatOptions } from '@/app/components/chatInputArea/chatInputArea';
|
import ChatInputArea, { ChatOptions } from '@/app/components/chatInputArea/chatInputArea';
|
||||||
import { Suggestion, suggestionsData } from '@/app/components/suggestions/suggestionsData';
|
import { Suggestion, suggestionsData } from '@/app/components/suggestions/suggestionsData';
|
||||||
|
|
||||||
import { useAuthenticatedData, useUserConfig } from '@/app/common/auth';
|
import { useAuthenticatedData, UserConfig, useUserConfig } from '@/app/common/auth';
|
||||||
import { convertColorToBorderClass } from '@/app/common/colorUtils';
|
import { convertColorToBorderClass } from '@/app/common/colorUtils';
|
||||||
import { getIconFromIconName } from '@/app/common/iconUtils';
|
import { getIconFromIconName } from '@/app/common/iconUtils';
|
||||||
import { AgentData } from '@/app/agents/page';
|
import { AgentData } from '@/app/agents/page';
|
||||||
@@ -29,6 +29,8 @@ interface ChatBodyDataProps {
|
|||||||
setUploadedFiles: (files: string[]) => void;
|
setUploadedFiles: (files: string[]) => void;
|
||||||
isMobileWidth?: boolean;
|
isMobileWidth?: boolean;
|
||||||
isLoggedIn: boolean;
|
isLoggedIn: boolean;
|
||||||
|
userConfig: UserConfig | null;
|
||||||
|
isLoadingUserConfig: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createNewConvo(slug: string) {
|
async function createNewConvo(slug: string) {
|
||||||
@@ -54,7 +56,6 @@ function ChatBodyData(props: ChatBodyDataProps) {
|
|||||||
const [agentIcons, setAgentIcons] = useState<JSX.Element[]>([]);
|
const [agentIcons, setAgentIcons] = useState<JSX.Element[]>([]);
|
||||||
const [agents, setAgents] = useState<AgentData[]>([]);
|
const [agents, setAgents] = useState<AgentData[]>([]);
|
||||||
|
|
||||||
const userConfig = useUserConfig(true);
|
|
||||||
const agentsFetcher = () => window.fetch('/api/agents').then(res => res.json()).catch(err => console.log(err));
|
const agentsFetcher = () => window.fetch('/api/agents').then(res => res.json()).catch(err => console.log(err));
|
||||||
const { data: agentsData, error } = useSWR<AgentData[]>('agents', agentsFetcher, { revalidateOnFocus: false });
|
const { data: agentsData, error } = useSWR<AgentData[]>('agents', agentsFetcher, { revalidateOnFocus: false });
|
||||||
|
|
||||||
@@ -64,12 +65,17 @@ function ChatBodyData(props: ChatBodyDataProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
console.log(`Loading user config: ${props.isLoadingUserConfig}`);
|
||||||
|
if (props.isLoadingUserConfig) return;
|
||||||
|
|
||||||
|
// Set user config
|
||||||
|
console.log(`Logged In: ${props.isLoggedIn}\nUserConfig: ${props.userConfig}`);
|
||||||
|
|
||||||
// Get today's day
|
// Get today's day
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
const day = today.getDay();
|
const day = today.getDay();
|
||||||
const timeOfDay = today.getHours() > 4 && today.getHours() < 12 ? 'morning' : today.getHours() < 17 ? 'afternoon' : 'evening';
|
const timeOfDay = today.getHours() > 4 && today.getHours() < 12 ? 'morning' : today.getHours() < 17 ? 'afternoon' : 'evening';
|
||||||
const nameSuffix = userConfig?.given_name ? `, ${userConfig?.given_name}` : "";
|
const nameSuffix = props.userConfig?.given_name ? `, ${props.userConfig?.given_name}` : "";
|
||||||
console.log(userConfig);
|
|
||||||
const greetings = [
|
const greetings = [
|
||||||
`What would you like to get done${nameSuffix}?`,
|
`What would you like to get done${nameSuffix}?`,
|
||||||
`Hey${nameSuffix}! How can I help?`,
|
`Hey${nameSuffix}! How can I help?`,
|
||||||
@@ -79,7 +85,7 @@ function ChatBodyData(props: ChatBodyDataProps) {
|
|||||||
];
|
];
|
||||||
const greeting = greetings[Math.floor(Math.random() * greetings.length)];
|
const greeting = greetings[Math.floor(Math.random() * greetings.length)];
|
||||||
setGreeting(greeting);
|
setGreeting(greeting);
|
||||||
}, []);
|
}, [props.isLoadingUserConfig, props.userConfig]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (props.chatOptionsData) {
|
if (props.chatOptionsData) {
|
||||||
@@ -161,7 +167,7 @@ function ChatBodyData(props: ChatBodyDataProps) {
|
|||||||
<div className={`${styles.chatBoxBody}`}>
|
<div className={`${styles.chatBoxBody}`}>
|
||||||
<div className="w-full text-center">
|
<div className="w-full text-center">
|
||||||
<div className="items-center">
|
<div className="items-center">
|
||||||
<h1 className="text-center pb-6 px-4 w-fit ml-auto mr-auto">{greeting}</h1>
|
<h1 className="text-center w-fit pb-6 px-4 mx-auto">{greeting}</h1>
|
||||||
</div>
|
</div>
|
||||||
{
|
{
|
||||||
!props.isMobileWidth &&
|
!props.isMobileWidth &&
|
||||||
@@ -266,12 +272,19 @@ export default function Home() {
|
|||||||
const [uploadedFiles, setUploadedFiles] = useState<string[]>([]);
|
const [uploadedFiles, setUploadedFiles] = useState<string[]>([]);
|
||||||
const [isMobileWidth, setIsMobileWidth] = useState(false);
|
const [isMobileWidth, setIsMobileWidth] = useState(false);
|
||||||
|
|
||||||
|
const {userConfig: initialUserConfig, isLoadingUserConfig} = useUserConfig(true);
|
||||||
|
const [userConfig, setUserConfig] = useState<UserConfig | null>(null);
|
||||||
|
|
||||||
const authenticatedData = useAuthenticatedData();
|
const authenticatedData = useAuthenticatedData();
|
||||||
|
|
||||||
const handleConversationIdChange = (newConversationId: string) => {
|
const handleConversationIdChange = (newConversationId: string) => {
|
||||||
setConversationID(newConversationId);
|
setConversationID(newConversationId);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setUserConfig(initialUserConfig);
|
||||||
|
}, [initialUserConfig]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetch('/api/chat/options')
|
fetch('/api/chat/options')
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
@@ -319,6 +332,8 @@ export default function Home() {
|
|||||||
setUploadedFiles={setUploadedFiles}
|
setUploadedFiles={setUploadedFiles}
|
||||||
isMobileWidth={isMobileWidth}
|
isMobileWidth={isMobileWidth}
|
||||||
onConversationIdChange={handleConversationIdChange}
|
onConversationIdChange={handleConversationIdChange}
|
||||||
|
userConfig={userConfig}
|
||||||
|
isLoadingUserConfig={isLoadingUserConfig}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -348,7 +348,7 @@ export default function SettingsView() {
|
|||||||
const [title, setTitle] = useState("Settings");
|
const [title, setTitle] = useState("Settings");
|
||||||
const [isMobileWidth, setIsMobileWidth] = useState(false);
|
const [isMobileWidth, setIsMobileWidth] = useState(false);
|
||||||
const { apiKeys, generateAPIKey, copyAPIKey, deleteAPIKey } = useApiKeys();
|
const { apiKeys, generateAPIKey, copyAPIKey, deleteAPIKey } = useApiKeys();
|
||||||
const initialUserConfig = useUserConfig(true);
|
const {userConfig: initialUserConfig} = useUserConfig(true);
|
||||||
const [userConfig, setUserConfig] = useState<UserConfig | null>(null);
|
const [userConfig, setUserConfig] = useState<UserConfig | null>(null);
|
||||||
const [name, setName] = useState<string | undefined>(undefined);
|
const [name, setName] = useState<string | undefined>(undefined);
|
||||||
const [notionToken, setNotionToken] = useState<string | null>(null);
|
const [notionToken, setNotionToken] = useState<string | null>(null);
|
||||||
|
|||||||
Reference in New Issue
Block a user