import React, { useState, useEffect } from 'react'; import { useSelector, useDispatch } from 'react-redux'; import Arrow2 from './assets/dropdown-arrow.svg'; import ArrowLeft from './assets/arrow-left.svg'; import ArrowRight from './assets/arrow-right.svg'; import Trash from './assets/trash.svg'; import { selectPrompt, setPrompt, selectSourceDocs, } from './preferences/preferenceSlice'; import { Doc } from './preferences/preferenceApi'; type PromptProps = { prompts: { name: string; id: string; type: string }[]; selectedPrompt: { name: string; id: string; type: string }; onSelectPrompt: (name: string, id: string, type: string) => void; setPrompts: (prompts: { name: string; id: string; type: string }[]) => void; apiHost: string; }; const Setting: React.FC = () => { const tabs = ['General', 'Prompts', 'Documents']; //const tabs = ['General', 'Prompts', 'Documents', 'Widgets']; const [activeTab, setActiveTab] = useState('General'); const [prompts, setPrompts] = useState< { name: string; id: string; type: string }[] >([]); const selectedPrompt = useSelector(selectPrompt); const [isAddPromptModalOpen, setAddPromptModalOpen] = useState(false); const documents = useSelector(selectSourceDocs); const [isAddDocumentModalOpen, setAddDocumentModalOpen] = useState(false); const dispatch = useDispatch(); const apiHost = import.meta.env.VITE_API_HOST || 'https://docsapi.arc53.com'; const [widgetScreenshot, setWidgetScreenshot] = useState(null); const updateWidgetScreenshot = (screenshot: File | null) => { setWidgetScreenshot(screenshot); }; useEffect(() => { const fetchPrompts = async () => { try { const response = await fetch(`${apiHost}/api/get_prompts`); if (!response.ok) { throw new Error('Failed to fetch prompts'); } const promptsData = await response.json(); setPrompts(promptsData); } catch (error) { console.error(error); } }; fetchPrompts(); }, []); const onDeletePrompt = (name: string, id: string) => { setPrompts(prompts.filter((prompt) => prompt.id !== id)); fetch(`${apiHost}/api/delete_prompt`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, // send id in body only body: JSON.stringify({ id: id }), }) .then((response) => { if (!response.ok) { throw new Error('Failed to delete prompt'); } }) .catch((error) => { console.error(error); }); }; const handleDeleteClick = (index: number, doc: Doc) => { const docPath = 'indexes/' + 'local' + '/' + doc.name; fetch(`${apiHost}/api/delete_old?path=${docPath}`, { method: 'GET', }) .then(() => { // remove the image element from the DOM const imageElement = document.querySelector( `#img-${index}`, ) as HTMLElement; const parentElement = imageElement.parentNode as HTMLElement; parentElement.parentNode?.removeChild(parentElement); }) .catch((error) => console.error(error)); }; return (

Settings

{tabs.map((tab, index) => ( ))}
{renderActiveTab()} {/* {activeTab === 'Widgets' && ( )} */}
); function scrollTabs(direction: number) { const container = document.querySelector('.flex-nowrap'); if (container) { container.scrollLeft += direction * 100; // Adjust the scroll amount as needed } } function renderActiveTab() { switch (activeTab) { case 'General': return ; case 'Prompts': return ( dispatch(setPrompt({ name: name, id: id, type: type })) } setPrompts={setPrompts} apiHost={apiHost} /> ); case 'Documents': return ( ); case 'Widgets': return ( ); default: return null; } } }; const General: React.FC = () => { const themes = ['Light', 'Dark']; const languages = ['English']; const [selectedTheme, setSelectedTheme] = useState(localStorage.getItem('selectedTheme') || themes[0]); const [selectedLanguage, setSelectedLanguage] = useState(languages[0]); useEffect(() => { if (selectedTheme === 'Dark') { document.documentElement.classList.add('dark'); document.documentElement.classList.add('dark:bg-raisin-black'); } else { document.documentElement.classList.remove('dark'); } localStorage.setItem('selectedTheme', selectedTheme); }, [selectedTheme]); return (

Select Theme

Select Language

); }; export default Setting; const Prompts: React.FC = ({ prompts, selectedPrompt, onSelectPrompt, setPrompts, apiHost, }) => { const handleSelectPrompt = ({ name, id, type, }: { name: string; id: string; type: string; }) => { setNewPromptName(name); onSelectPrompt(name, id, type); }; const [newPromptName, setNewPromptName] = useState(selectedPrompt.name); const [newPromptContent, setNewPromptContent] = useState(''); const handleAddPrompt = async () => { try { const response = await fetch(`${apiHost}/api/create_prompt`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name: newPromptName, content: newPromptContent, }), }); if (!response.ok) { throw new Error('Failed to add prompt'); } const newPrompt = await response.json(); if (setPrompts) { setPrompts([ ...prompts, { name: newPromptName, id: newPrompt.id, type: 'private' }, ]); } onSelectPrompt(newPromptName, newPrompt.id, newPromptContent); setNewPromptName(newPromptName); } catch (error) { console.error(error); } }; const handleDeletePrompt = () => { setPrompts(prompts.filter((prompt) => prompt.id !== selectedPrompt.id)); console.log('selectedPrompt.id', selectedPrompt.id); fetch(`${apiHost}/api/delete_prompt`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ id: selectedPrompt.id }), }) .then((response) => { if (!response.ok) { throw new Error('Failed to delete prompt'); } // get 1st prompt and set it as selected if (prompts.length > 0) { onSelectPrompt(prompts[0].name, prompts[0].id, prompts[0].type); setNewPromptName(prompts[0].name); } }) .catch((error) => { console.error(error); }); }; useEffect(() => { const fetchPromptContent = async () => { console.log('fetching prompt content'); try { const response = await fetch( `${apiHost}/api/get_single_prompt?id=${selectedPrompt.id}`, { method: 'GET', headers: { 'Content-Type': 'application/json', }, }, ); if (!response.ok) { throw new Error('Failed to fetch prompt content'); } const promptContent = await response.json(); setNewPromptContent(promptContent.content); } catch (error) { console.error(error); } }; fetchPromptContent(); }, [selectedPrompt]); const handleSaveChanges = () => { fetch(`${apiHost}/api/update_prompt`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ id: selectedPrompt.id, name: newPromptName, content: newPromptContent, }), }) .then((response) => { if (!response.ok) { throw new Error('Failed to update prompt'); } onSelectPrompt(newPromptName, selectedPrompt.id, selectedPrompt.type); setNewPromptName(newPromptName); }) .catch((error) => { console.error(error); }); }; return (

Active Prompt

Prompt name

{' '}

start by editing name

setNewPromptName(e.target.value)} />

Prompt content