Skip to content

Commit

Permalink
Refactor chat components to use dynamic API URLs moving apiurl to store
Browse files Browse the repository at this point in the history
  • Loading branch information
ap0k4 committed Nov 7, 2024
1 parent f69d817 commit fa09e11
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 46 deletions.
14 changes: 7 additions & 7 deletions apps/adminpanel/pages/chatbot.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { useEffect, useState } from 'react'
import Head from 'next/head'
import { SiteConfig } from 'app/conf'
import dynamic from 'next/dynamic';
import { Stack } from 'tamagui';
import { useThemeSetting } from "@tamagui/next-theme";
import {YStack} from 'tamagui'
import dynamic from 'next/dynamic'
import { YStack } from 'tamagui'
import { useRouter } from 'next/router'

export default function Page(props: any) {
//@ts-ignore
const { query } = useRouter()
const apiUrl = query.apiUrl as string
const Chatbot = dynamic(() => import('protolib/components/chatbot'), { ssr: false })
const projectName = SiteConfig.projectName

Expand All @@ -17,8 +17,8 @@ export default function Page(props: any) {
<title>{projectName + " - Chatbot"}</title>
</Head>
<YStack backgroundColor="$bgContent" f={1}>
<Chatbot />
<Chatbot apiUrl={apiUrl} />
</YStack>
</>
)
}
}
2 changes: 1 addition & 1 deletion packages/protolib/src/components/AdminPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const AdminPage = forwardRef(({ pageSession, title, children, integratedC
</AdminPanel>
</SearchContext.Provider>
<Tinted>
{integratedChat && settingsAssistantEnabled && <BubbleChat/>}
{integratedChat && settingsAssistantEnabled && <BubbleChat apiUrl="/api/v1/chatbots/assistant"/>}
</Tinted>
</Page>
)
Expand Down
10 changes: 7 additions & 3 deletions packages/protolib/src/components/BubbleChat.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { useState } from "react";
import { Sparkles, X, Maximize, Minimize } from '@tamagui/lucide-icons';
import { Tinted } from './Tinted'
import { Chatbot } from './Chatbot'
import { Chat } from './Chat'
import { YStack, Button, XStack, Theme, Spinner, Paragraph} from 'tamagui';

export const BubbleChat = () => {
type BubleChatProps = {
apiUrl: string;
};

export const BubbleChat = ({ apiUrl }: BubleChatProps) => {
const [isChatVisible, setIsChatVisible] = useState(false);
const [isExpanded, setIsExpanded] = useState(false);
const [isChatLoaded, setIsChatLoaded] = useState(false);
Expand Down Expand Up @@ -93,7 +97,7 @@ export const BubbleChat = () => {

<Paragraph size="$5">Loading chat...</Paragraph>
</YStack>
{isChatLoaded && <Chatbot/>}
{isChatLoaded && <Chat apiUrl={apiUrl}/>}
</YStack>


Expand Down
15 changes: 15 additions & 0 deletions packages/protolib/src/components/Chat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type ChatProps = {
apiUrl: string;
};

export const Chat = ({ apiUrl }: ChatProps) => {
return (
<iframe
src={"/workspace/chatbot?apiUrl=" + encodeURIComponent(apiUrl)}
width="100%"
height="100%"
style={{ border: "none" }}
title="Chat Widget"
/>
);
};
9 changes: 0 additions & 9 deletions packages/protolib/src/components/Chatbot.tsx

This file was deleted.

63 changes: 38 additions & 25 deletions packages/protolib/src/components/chatbot/index.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
import { useEffect, useState } from "react";
import Navbar from "./components/Navbar/Navbar";
import DefaultIdeas from "./components/DefaultIdea/DefaultIdeas";
import UserQuery from "./components/UserInput/UserQuery";
import { Plus, Menu, PanelLeft } from "lucide-react";
import useChat, { chatsLength, useAuth } from "./store/store";
import classNames from "classnames";
import Chats from "./components/Chat/Chats";
import Modal from "./components/modals/Modal";
import Apikey from "./components/modals/Apikey";
import { useThemeSetting } from "@tamagui/next-theme";
import { Stack } from "tamagui";
import { useEffect, useState } from "react"
import Navbar from "./components/Navbar/Navbar"
import DefaultIdeas from "./components/DefaultIdea/DefaultIdeas"
import UserQuery from "./components/UserInput/UserQuery"
import { Plus, Menu, PanelLeft } from "lucide-react"
import useChat, { chatsLength, useAuth, useSettings } from "./store/store"
import classNames from "classnames"
import Chats from "./components/Chat/Chats"
import Modal from "./components/modals/Modal"
import Apikey from "./components/modals/Apikey"
import { useThemeSetting } from "@tamagui/next-theme"
import { Stack } from "tamagui"

const applyTheme = (resolvedTheme) => {
if (resolvedTheme === "light" && document.documentElement.classList.contains("dark")) {
document.documentElement.classList.remove("dark");
document.documentElement.classList.remove("dark")
} else if (resolvedTheme === "dark" && !document.documentElement.classList.contains("dark")) {
document.documentElement.classList.add("dark");
document.documentElement.classList.add("dark")
}
};
}

type AppProps = {
apiUrl: string
}

function App() {
const [active, setActive] = useState(false);
const isChatsVisible = useChat(chatsLength);
const addNewChat = useChat((state) => state.addNewChat);
let userHasApiKey = useAuth((state) => state.apikey);
const { resolvedTheme } = useThemeSetting();
function App({ apiUrl }: AppProps) {
const [active, setActive] = useState(false)
const isChatsVisible = useChat(chatsLength)
const addNewChat = useChat((state) => state.addNewChat)
let userHasApiKey = useAuth((state) => state.apikey)
const { resolvedTheme } = useThemeSetting()

const setApiUrl = useSettings((state) => state.setApiUrl)

useEffect(() => {
applyTheme(resolvedTheme)
}, [resolvedTheme])

// Almacena apiUrl en el store al montar el componente
useEffect(() => {
applyTheme(resolvedTheme);
}, [resolvedTheme]);
if (apiUrl) {
setApiUrl(apiUrl) // Guarda apiUrl en el store
}
}, [apiUrl, setApiUrl])

return (
<Stack backgroundColor={resolvedTheme === "light" ? "" : "#212121"} f={1} className="h-screen flex flex-col">
Expand Down Expand Up @@ -71,7 +84,7 @@ function App() {
</Modal>
</div>
</Stack>
);
)
}

export default App;
export default App
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ChatMessageType, ModalList, useSettings } from "../store/store";

const apiUrl = "/api/v1/chatbots/assistant";
const IMAGE_GENERATION_API_URL = "https://api.openai.com/v1/images/generations";

export async function fetchResults(
Expand All @@ -11,6 +10,7 @@ export async function fetchResults(
onCompletion: () => void
) {
try {
const apiUrl = useSettings.getState().settings.apiUrl
const response = await fetch(apiUrl, {
method: `POST`,
signal: signal,
Expand Down
10 changes: 10 additions & 0 deletions packages/protolib/src/components/chatbot/store/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface SettingsType {
useSystemMessageForAllChats: boolean;
selectedModal: ModalList;
dalleImageSize: { "dall-e-3": ImageSize };
apiUrl: string;
};
modalsList: readonly string[];
isSystemMessageModalVisible: boolean;
Expand All @@ -70,6 +71,7 @@ export interface SettingsType {
setModalsList: (value: string[]) => void;
setModal: (value: ModalList) => void;
setDalleImageSize: (value: ImageSize, type: "dall-e-3") => void;
setApiUrl: (url: string) => void;
}
export interface ChatType {
chats: ChatMessageType[];
Expand Down Expand Up @@ -261,6 +263,7 @@ const useSettings = createWithEqualityFn<SettingsType>()(
useSystemMessageForAllChats: false,
selectedModal: "gpt-4-turbo",
dalleImageSize: { "dall-e-3": "1024x1024" },
apiUrl: "",
},
modalsList: modalsList,
isSystemMessageModalVisible: false,
Expand Down Expand Up @@ -315,6 +318,13 @@ const useSettings = createWithEqualityFn<SettingsType>()(
})
);
},
setApiUrl: (url) => {
set(
produce((state: SettingsType) => {
state.settings.apiUrl = url;
})
);
},
}),
{
name: "settings",
Expand Down

0 comments on commit fa09e11

Please sign in to comment.