mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-24 04:10:15 +01:00
adding redux in progress
This commit is contained in:
parent
85efaa4173
commit
7978ddd871
8 changed files with 97 additions and 62 deletions
8
index.js
8
index.js
|
|
@ -1,10 +1,16 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
// import reactDom from 'react-dom'; ---> deprecated
|
// import reactDom from 'react-dom'; ---> deprecated
|
||||||
import { createRoot } from 'react-dom/client';
|
import { createRoot } from 'react-dom/client';
|
||||||
|
import { Provider } from 'react-redux';
|
||||||
|
import { store } from './store';
|
||||||
import App from './src/App';
|
import App from './src/App';
|
||||||
import './src/style.css';
|
import './src/style.css';
|
||||||
|
|
||||||
const container = document.getElementById('root');
|
const container = document.getElementById('root');
|
||||||
const root = createRoot(container); // createRoot(container!) if you use TypeScript
|
const root = createRoot(container); // createRoot(container!) if you use TypeScript
|
||||||
// reactDom.render(<App />, document.getElementById('root'));
|
// reactDom.render(<App />, document.getElementById('root'));
|
||||||
root.render(<App />);
|
root.render(
|
||||||
|
<Provider store={store}>
|
||||||
|
<App />
|
||||||
|
</Provider>
|
||||||
|
);
|
||||||
|
|
|
||||||
45
src/App.jsx
45
src/App.jsx
|
|
@ -1,4 +1,5 @@
|
||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
|
import { useSelector, useDispatch } from 'react-redux';
|
||||||
import Messages from './components/Messages';
|
import Messages from './components/Messages';
|
||||||
import TextChat from './components/TextChat';
|
import TextChat from './components/TextChat';
|
||||||
import Nav from './components/Nav';
|
import Nav from './components/Nav';
|
||||||
|
|
@ -12,27 +13,33 @@ const fetcher = (url) => fetch(url).then((res) => res.json());
|
||||||
const postRequest = async (url, { arg }) => await axios.post(url, { arg });
|
const postRequest = async (url, { arg }) => await axios.post(url, { arg });
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
const [messages, setMessages] = useState([]);
|
// const [messages, setMessages] = useState([]);
|
||||||
const [convo, setConvo] = useState({ conversationId: null, parentMessageId: null });
|
const messages = useSelector((state) => state.messages);
|
||||||
|
// const [convo, setConvo] = useState({ conversationId: null, parentMessageId: null });
|
||||||
const { data, error, isLoading, mutate } = useSWR('http://localhost:3050/convos', fetcher);
|
const { data, error, isLoading, mutate } = useSWR('http://localhost:3050/convos', fetcher);
|
||||||
|
|
||||||
const conversation = useSWRMutation( //{ trigger, isMutating }
|
const convo = useSelector((state) => state.convo);
|
||||||
`http://localhost:3050/messages/${convo.conversationId}`,
|
const conversationId = useSelector((state) => state.convo.conversationId);
|
||||||
fetcher,
|
console.log('conversationId', conversationId);
|
||||||
{
|
|
||||||
onSuccess: function (res) {
|
|
||||||
console.log('success', res);
|
|
||||||
setMessages(res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
useDidMountEffect(() => conversation.trigger(), [convo]);
|
// const conversation = useSWRMutation(
|
||||||
|
// //{ trigger, isMutating }
|
||||||
|
// `http://localhost:3050/messages/${conversationId}`,
|
||||||
|
// fetcher,
|
||||||
|
// {
|
||||||
|
// onSuccess: function (res) {
|
||||||
|
// console.log('success', res);
|
||||||
|
// setMessages(res);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// );
|
||||||
|
|
||||||
const onConvoClick = (conversationId, parentMessageId) => {
|
// useDidMountEffect(() => conversation.trigger(), [conversationId]);
|
||||||
console.log('convo was clicked');
|
|
||||||
setConvo({ conversationId, parentMessageId });
|
// const onConvoClick = (conversationId, parentMessageId) => {
|
||||||
};
|
// console.log('convo was clicked');
|
||||||
|
// setConvo({ conversationId, parentMessageId });
|
||||||
|
// };
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex h-screen">
|
<div className="flex h-screen">
|
||||||
|
|
@ -40,7 +47,6 @@ const App = () => {
|
||||||
<Nav
|
<Nav
|
||||||
conversations={data}
|
conversations={data}
|
||||||
convo={convo}
|
convo={convo}
|
||||||
convoHandler={onConvoClick}
|
|
||||||
/>
|
/>
|
||||||
{/* <div className="flex h-full flex-1 flex-col md:pl-[260px]"> */}
|
{/* <div className="flex h-full flex-1 flex-col md:pl-[260px]"> */}
|
||||||
<div className="flex h-full w-full flex-1 flex-col bg-gray-50 md:pl-[260px]">
|
<div className="flex h-full w-full flex-1 flex-col bg-gray-50 md:pl-[260px]">
|
||||||
|
|
@ -49,10 +55,9 @@ const App = () => {
|
||||||
<Messages messages={messages} />
|
<Messages messages={messages} />
|
||||||
<TextChat
|
<TextChat
|
||||||
messages={messages}
|
messages={messages}
|
||||||
setMessages={setMessages}
|
// setMessages={setMessages}
|
||||||
reloadConvos={mutate}
|
reloadConvos={mutate}
|
||||||
convo={convo}
|
convo={convo}
|
||||||
setConvo={setConvo}
|
|
||||||
/>
|
/>
|
||||||
{/* </main> */}
|
{/* </main> */}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,37 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import RenameButton from './RenameButton';
|
import RenameButton from './RenameButton';
|
||||||
import DeleteButton from './DeleteButton';
|
import DeleteButton from './DeleteButton';
|
||||||
|
import { useSelector, useDispatch } from 'react-redux';
|
||||||
|
import { setConversation } from '../../store/convoSlice';
|
||||||
|
import { setMessages } from '../../store/messageSlice';
|
||||||
|
import useSWRMutation from 'swr/mutation';
|
||||||
|
|
||||||
|
const fetcher = (url) => fetch(url).then((res) => res.json());
|
||||||
|
|
||||||
|
export default function Conversation({ id, parentMessageId, title = 'New conversation' }) {
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
const { trigger, isMutating } = useSWRMutation(
|
||||||
|
//{ trigger, isMutating }
|
||||||
|
`http://localhost:3050/messages/${id}`,
|
||||||
|
fetcher,
|
||||||
|
{
|
||||||
|
onSuccess: function (res) {
|
||||||
|
console.log('success', res);
|
||||||
|
dispatch(setMessages(res));
|
||||||
|
// setMessages(res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const onConvoClick = (id, parentMessageId) => {
|
||||||
|
console.log('convo was clicked');
|
||||||
|
dispatch(setConversation({ conversationId: id, parentMessageId }));
|
||||||
|
trigger();
|
||||||
|
};
|
||||||
|
|
||||||
export default function Conversation({
|
|
||||||
id,
|
|
||||||
parentMessageId,
|
|
||||||
convo,
|
|
||||||
convoHandler,
|
|
||||||
title = 'New conversation'
|
|
||||||
}) {
|
|
||||||
return (
|
return (
|
||||||
<a
|
<a
|
||||||
onClick={() => convoHandler(id, parentMessageId)}
|
onClick={() => onConvoClick(id, parentMessageId)}
|
||||||
className="animate-flash group relative flex cursor-pointer items-center gap-3 break-all rounded-md bg-gray-800 py-3 px-3 pr-14 hover:bg-gray-800"
|
className="animate-flash group relative flex cursor-pointer items-center gap-3 break-all rounded-md bg-gray-800 py-3 px-3 pr-14 hover:bg-gray-800"
|
||||||
>
|
>
|
||||||
<svg
|
<svg
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import Conversation from './Conversation';
|
import Conversation from './Conversation';
|
||||||
|
|
||||||
export default function Conversations({ convoState, conversations, convoHandler }) {
|
export default function Conversations({ conversations }) {
|
||||||
return (
|
return (
|
||||||
<div className="-mr-2 flex-1 flex-col overflow-y-auto border-b border-white/20">
|
<div className="-mr-2 flex-1 flex-col overflow-y-auto border-b border-white/20">
|
||||||
<div className="flex flex-col gap-2 text-sm text-gray-100">
|
<div className="flex flex-col gap-2 text-sm text-gray-100">
|
||||||
|
|
@ -12,8 +12,6 @@ export default function Conversations({ convoState, conversations, convoHandler
|
||||||
id={convo.conversationId}
|
id={convo.conversationId}
|
||||||
parentMessageId={convo.parentMessageId}
|
parentMessageId={convo.parentMessageId}
|
||||||
title={convo.title}
|
title={convo.title}
|
||||||
convo={convoState}
|
|
||||||
convoHandler={convoHandler}
|
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
{conversations && conversations.length >= 12 && (
|
{conversations && conversations.length >= 12 && (
|
||||||
|
|
|
||||||
|
|
@ -2,21 +2,27 @@ import React, { useState } from 'react';
|
||||||
import SubmitButton from './SubmitButton';
|
import SubmitButton from './SubmitButton';
|
||||||
import TextareaAutosize from 'react-textarea-autosize';
|
import TextareaAutosize from 'react-textarea-autosize';
|
||||||
import handleSubmit from '../utils/handleSubmit';
|
import handleSubmit from '../utils/handleSubmit';
|
||||||
|
import { useSelector, useDispatch } from 'react-redux';
|
||||||
|
import { setConversation } from '../../store/convoSlice';
|
||||||
|
import { setMessages } from '../../store/messageSlice';
|
||||||
|
|
||||||
export default function TextChat({ messages, setMessages, reloadConvos, convo, setConvo }) {
|
export default function TextChat({ messages, reloadConvos, convo }) {
|
||||||
const [text, setText] = useState('');
|
const [text, setText] = useState('');
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
const submitMessage = () => {
|
const submitMessage = () => {
|
||||||
const payload = text.trim();
|
const payload = text.trim();
|
||||||
const currentMsg = { sender: 'user', text: payload, current: true };
|
const currentMsg = { sender: 'user', text: payload, current: true };
|
||||||
setMessages([...messages, currentMsg]);
|
dispatch(setMessages([...messages, currentMsg]));
|
||||||
setText('');
|
setText('');
|
||||||
const messageHandler = (data) => {
|
const messageHandler = (data) => {
|
||||||
setMessages([...messages, currentMsg, { sender: 'GPT', text: data }]);
|
dispatch(setMessages([...messages, currentMsg, { sender: 'GPT', text: data }]));
|
||||||
};
|
};
|
||||||
const convoHandler = (data) => {
|
const convoHandler = (data) => {
|
||||||
if (convo.conversationId === null && convo.parentMessageId === null) {
|
if (convo.conversationId === null && convo.parentMessageId === null) {
|
||||||
const { conversationId, parentMessageId } = data;
|
const { conversationId, parentMessageId } = data;
|
||||||
setConvo({ conversationId, parentMessageId: data.id });
|
// setConvo({ conversationId, parentMessageId: data.id });
|
||||||
|
dispatch(setConversation({ conversationId, parentMessageId: data.id }));
|
||||||
}
|
}
|
||||||
|
|
||||||
reloadConvos();
|
reloadConvos();
|
||||||
|
|
@ -60,28 +66,6 @@ export default function TextChat({ messages, setMessages, reloadConvos, convo, s
|
||||||
className="m-0 h-auto max-h-52 resize-none overflow-auto border-0 bg-transparent p-0 pl-2 pr-7 leading-6 focus:outline-none focus:ring-0 focus-visible:ring-0 dark:bg-transparent md:pl-0"
|
className="m-0 h-auto max-h-52 resize-none overflow-auto border-0 bg-transparent p-0 pl-2 pr-7 leading-6 focus:outline-none focus:ring-0 focus-visible:ring-0 dark:bg-transparent md:pl-0"
|
||||||
/>
|
/>
|
||||||
<SubmitButton onClick={() => submitMessage()} />
|
<SubmitButton onClick={() => submitMessage()} />
|
||||||
{/* <button className="absolute bottom-1.5 right-1 rounded-md p-1 text-gray-500 hover:bg-gray-100 disabled:hover:bg-transparent dark:hover:bg-gray-900 dark:hover:text-gray-400 dark:disabled:hover:bg-transparent md:bottom-2.5 md:right-2">
|
|
||||||
<svg
|
|
||||||
stroke="currentColor"
|
|
||||||
fill="none"
|
|
||||||
strokeWidth="2"
|
|
||||||
viewBox="0 0 24 24"
|
|
||||||
strokeLinecap="round"
|
|
||||||
strokeLinejoin="round"
|
|
||||||
className="mr-1 h-4 w-4"
|
|
||||||
height="1em"
|
|
||||||
width="1em"
|
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
|
||||||
>
|
|
||||||
<line
|
|
||||||
x1="22"
|
|
||||||
y1="2"
|
|
||||||
x2="11"
|
|
||||||
y2="13"
|
|
||||||
/>
|
|
||||||
<polygon points="22 2 15 22 11 13 2 9 22 2" />
|
|
||||||
</svg>
|
|
||||||
</button> */}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,10 @@ const currentSlice = createSlice({
|
||||||
initialState,
|
initialState,
|
||||||
reducers: {
|
reducers: {
|
||||||
setConversation: (state, action) => {
|
setConversation: (state, action) => {
|
||||||
const { payload } = action;
|
console.log('in setConversation reducer');
|
||||||
state = { ...state, ...payload };
|
const { conversationId, parentMessageId } = action.payload;
|
||||||
|
state.conversationId = conversationId;
|
||||||
|
state.parentMessageId = parentMessageId;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
import { configureStore } from '@reduxjs/toolkit';
|
import { configureStore } from '@reduxjs/toolkit';
|
||||||
|
|
||||||
import convoReducer from './convoSlice.js';
|
import convoReducer from './convoSlice.js';
|
||||||
// import uploadReducer from './uploadSlice.js'
|
import messageReducer from './messageSlice.js'
|
||||||
|
|
||||||
export const store = configureStore({
|
export const store = configureStore({
|
||||||
reducer: {
|
reducer: {
|
||||||
convo: convoReducer,
|
convo: convoReducer,
|
||||||
// upload: uploadReducer,
|
messages: messageReducer,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
||||||
20
store/messageSlice.js
Normal file
20
store/messageSlice.js
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
import { createSlice } from '@reduxjs/toolkit';
|
||||||
|
|
||||||
|
const initialState = [];
|
||||||
|
|
||||||
|
const currentSlice = createSlice({
|
||||||
|
name: 'messages',
|
||||||
|
initialState,
|
||||||
|
reducers: {
|
||||||
|
setMessages: (state, action) => {
|
||||||
|
console.log('in setMessages reducer');
|
||||||
|
const { payload } = action;
|
||||||
|
state = payload;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//
|
||||||
|
|
||||||
|
export const { setMessages } = currentSlice.actions;
|
||||||
|
|
||||||
|
export default currentSlice.reducer;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue