Revert "Removed some of the calls to process.env; Used server props instead"

This reverts commit f51f36a411.
This commit is contained in:
Andrew Pennington 2024-08-15 22:29:15 +01:00
parent f51f36a411
commit 73bf7f3d79
No known key found for this signature in database
GPG key ID: E9DA097213FD17EA
5 changed files with 11 additions and 52 deletions

View file

@ -1,31 +1,7 @@
import ChatWindow from '@/components/ChatWindow'; import ChatWindow from '@/components/ChatWindow';
import { FC } from 'react';
import process from 'process';
import { GetServerSideProps } from 'next';
interface PageProps { const Page = ({ params }: { params: { chatId: string } }) => {
backendApiUrl: string; return <ChatWindow id={params.chatId} />;
params: {
chatId: string;
};
}
export async function getServerSideProps(context): GetServerSideProps<PageProps> {
const backendApiUrl = process.env.BACKEND_API_URL;
const { chatId } = context.params || {};
return {
props: {
backendApiUrl,
params: {
chatId: chatId || '',
},
},
};
}
const Page: FC<PageProps> = ({ params, backendApiUrl }) => {
return <ChatWindow id={params.chatId} backendApiUrl={backendApiUrl} />;
}; };
export default Page; export default Page;

View file

@ -4,9 +4,8 @@ import DeleteChat from '@/components/DeleteChat';
import { formatTimeDifference } from '@/lib/utils'; import { formatTimeDifference } from '@/lib/utils';
import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react'; import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react';
import Link from 'next/link'; import Link from 'next/link';
import { useEffect, useState, FC } from 'react'; import { useEffect, useState } from 'react';
import process from 'process'; import process from 'process';
import { GetServerSideProps } from 'next';
export interface Chat { export interface Chat {
id: string; id: string;
@ -15,21 +14,7 @@ export interface Chat {
focusMode: string; focusMode: string;
} }
interface PageProps { const Page = () => {
backendApiUrl: string;
}
export async function getServerSideProps(): GetServerSideProps<PageProps> {
const backendApiUrl = process.env.BACKEND_API_URL;
return {
props: {
backendApiUrl,
},
};
}
const Page: FC<PageProps> = ({ backendApiUrl }) => {
const [chats, setChats] = useState<Chat[]>([]); const [chats, setChats] = useState<Chat[]>([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
@ -37,7 +22,7 @@ const Page: FC<PageProps> = ({ backendApiUrl }) => {
const fetchChats = async () => { const fetchChats = async () => {
setLoading(true); setLoading(true);
const res = await fetch(`${backendApiUrl}/chats`, { const res = await fetch(`${process.env.BACKEND_API_URL}/chats`, {
method: 'GET', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -113,7 +98,6 @@ const Page: FC<PageProps> = ({ backendApiUrl }) => {
chatId={chat.id} chatId={chat.id}
chats={chats} chats={chats}
setChats={setChats} setChats={setChats}
backendApiUrl={backendApiUrl}
/> />
</div> </div>
</div> </div>

View file

@ -260,7 +260,7 @@ const loadMessages = async (
setIsMessagesLoaded(true); setIsMessagesLoaded(true);
}; };
const ChatWindow = ({ id, backendApiUrl }: { id?: string, backendApiUrl: string }) => { const ChatWindow = ({ id }: { id?: string }) => {
const searchParams = useSearchParams(); const searchParams = useSearchParams();
const initialMessage = searchParams.get('q'); const initialMessage = searchParams.get('q');
@ -434,7 +434,7 @@ const ChatWindow = ({ id, backendApiUrl }: { id?: string, backendApiUrl: string
lastMsg.sources.length > 0 && lastMsg.sources.length > 0 &&
!lastMsg.suggestions !lastMsg.suggestions
) { ) {
const suggestions = await getSuggestions(messagesRef.current, backendApiUrl); const suggestions = await getSuggestions(messagesRef.current);
setMessages((prev) => setMessages((prev) =>
prev.map((msg) => { prev.map((msg) => {
if (msg.messageId === lastMsg.messageId) { if (msg.messageId === lastMsg.messageId) {

View file

@ -9,12 +9,10 @@ const DeleteChat = ({
chatId, chatId,
chats, chats,
setChats, setChats,
backendApiUrl
}: { }: {
chatId: string; chatId: string;
chats: Chat[]; chats: Chat[];
setChats: (chats: Chat[]) => void; setChats: (chats: Chat[]) => void;
backendApiUrl: string
}) => { }) => {
const [confirmationDialogOpen, setConfirmationDialogOpen] = useState(false); const [confirmationDialogOpen, setConfirmationDialogOpen] = useState(false);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
@ -23,7 +21,7 @@ const DeleteChat = ({
setLoading(true); setLoading(true);
try { try {
const res = await fetch( const res = await fetch(
`${backendApiUrl}/chats/${chatId}`, `${process.env.BACKEND_API_URL}/chats/${chatId}`,
{ {
method: 'DELETE', method: 'DELETE',
headers: { headers: {

View file

@ -1,10 +1,11 @@
import { Message } from '@/components/ChatWindow'; import { Message } from '@/components/ChatWindow';
import process from 'process';
export const getSuggestions = async (chatHisory: Message[], backendApiUrl: string) => { export const getSuggestions = async (chatHisory: Message[]) => {
const chatModel = localStorage.getItem('chatModel'); const chatModel = localStorage.getItem('chatModel');
const chatModelProvider = localStorage.getItem('chatModelProvider'); const chatModelProvider = localStorage.getItem('chatModelProvider');
const res = await fetch(`${backendApiUrl}/suggestions`, { const res = await fetch(`${process.env.BACKEND_API_URL}/suggestions`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',