mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
feat: simple api call to enable search
This commit is contained in:
parent
1041146fcb
commit
97a6cd801b
6 changed files with 50 additions and 38 deletions
|
|
@ -26,4 +26,5 @@ BING_TOKEN=
|
||||||
# global enable/disable the sample user system.
|
# global enable/disable the sample user system.
|
||||||
# this is not a ready to use user system.
|
# this is not a ready to use user system.
|
||||||
# dont't use it, unless you can write your own code.
|
# dont't use it, unless you can write your own code.
|
||||||
|
SEARCH=
|
||||||
ENABLE_USER_SYSTEM=
|
ENABLE_USER_SYSTEM=
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const { Message } = require('../../models/Message');
|
const { Message } = require('../../models/Message');
|
||||||
const { Conversation, getConvosQueried } = require('../../models/Conversation');
|
const { Conversation, getConvosQueried } = require('../../models/Conversation');
|
||||||
const { reduceMessages, reduceHits } = require('../../lib/utils/reduceHits');
|
const { reduceHits } = require('../../lib/utils/reduceHits');
|
||||||
// const { MeiliSearch } = require('meilisearch');
|
// const { MeiliSearch } = require('meilisearch');
|
||||||
const cache = new Map();
|
const cache = new Map();
|
||||||
|
|
||||||
|
|
@ -53,7 +53,7 @@ router.get('/', async function (req, res) {
|
||||||
const result = await getConvosQueried(user, sortedHits, pageNumber);
|
const result = await getConvosQueried(user, sortedHits, pageNumber);
|
||||||
cache.set(q, result.cache);
|
cache.set(q, result.cache);
|
||||||
delete result.cache;
|
delete result.cache;
|
||||||
result.messages = messages.filter((message) => !result.filter.has(message.conversationId));
|
result.messages = messages.filter(message => !result.filter.has(message.conversationId));
|
||||||
// console.log(result, messages.length);
|
// console.log(result, messages.length);
|
||||||
res.status(200).send(result);
|
res.status(200).send(result);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
@ -78,4 +78,8 @@ router.get('/test', async function (req, res) {
|
||||||
res.send(messages);
|
res.send(messages);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.get('/enable', async function (req, res) {
|
||||||
|
res.send(!!process.env.SEARCH);
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,9 @@ import Nav from './components/Nav';
|
||||||
import MobileNav from './components/Nav/MobileNav';
|
import MobileNav from './components/Nav/MobileNav';
|
||||||
import useDocumentTitle from '~/hooks/useDocumentTitle';
|
import useDocumentTitle from '~/hooks/useDocumentTitle';
|
||||||
import { useSelector, useDispatch } from 'react-redux';
|
import { useSelector, useDispatch } from 'react-redux';
|
||||||
|
import userAuth from './utils/userAuth';
|
||||||
import { setUser } from './store/userReducer';
|
import { setUser } from './store/userReducer';
|
||||||
|
import { setSearchState } from './store/searchSlice';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
|
|
@ -15,33 +17,23 @@ const App = () => {
|
||||||
const { messages, messageTree } = useSelector((state) => state.messages);
|
const { messages, messageTree } = useSelector((state) => state.messages);
|
||||||
const { user } = useSelector((state) => state.user);
|
const { user } = useSelector((state) => state.user);
|
||||||
const { title } = useSelector((state) => state.convo);
|
const { title } = useSelector((state) => state.convo);
|
||||||
const [ navVisible, setNavVisible ]= useState(false)
|
const [navVisible, setNavVisible] = useState(false);
|
||||||
useDocumentTitle(title);
|
useDocumentTitle(title);
|
||||||
|
|
||||||
useEffect(async () => {
|
useEffect(() => {
|
||||||
try {
|
axios.get('/api/search/enable').then((res) => { console.log(res.data); dispatch(setSearchState(res.data))});
|
||||||
const response = await axios.get('/api/me', {
|
userAuth()
|
||||||
timeout: 1000,
|
.then((user) => dispatch(setUser(user)))
|
||||||
withCredentials: true
|
.catch((err) => console.log(err));
|
||||||
});
|
}, []);
|
||||||
const user = response.data;
|
|
||||||
if (user) {
|
|
||||||
dispatch(setUser(user));
|
|
||||||
} else {
|
|
||||||
console.log('Not login!');
|
|
||||||
window.location.href = '/auth/login';
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
console.log('Not login!');
|
|
||||||
window.location.href = '/auth/login';
|
|
||||||
}
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
if (user)
|
if (user)
|
||||||
return (
|
return (
|
||||||
<div className="flex h-screen">
|
<div className="flex h-screen">
|
||||||
<Nav navVisible={navVisible} setNavVisible={setNavVisible} />
|
<Nav
|
||||||
|
navVisible={navVisible}
|
||||||
|
setNavVisible={setNavVisible}
|
||||||
|
/>
|
||||||
<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]">
|
||||||
<div className="transition-width relative flex h-full w-full flex-1 flex-col items-stretch overflow-hidden bg-white dark:bg-gray-800">
|
<div className="transition-width relative flex h-full w-full flex-1 flex-col items-stretch overflow-hidden bg-white dark:bg-gray-800">
|
||||||
<MobileNav setNavVisible={setNavVisible} />
|
<MobileNav setNavVisible={setNavVisible} />
|
||||||
|
|
@ -58,12 +50,7 @@ const App = () => {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
else
|
else return <div className="flex h-screen"></div>;
|
||||||
return (
|
|
||||||
<div className="flex h-screen">
|
|
||||||
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,18 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import NavLink from './NavLink';
|
|
||||||
import LogOutIcon from '../svg/LogOutIcon';
|
|
||||||
import SearchBar from './SearchBar';
|
import SearchBar from './SearchBar';
|
||||||
import ClearConvos from './ClearConvos';
|
import ClearConvos from './ClearConvos';
|
||||||
import DarkMode from './DarkMode';
|
import DarkMode from './DarkMode';
|
||||||
import Logout from './Logout';
|
import Logout from './Logout';
|
||||||
|
import { useSelector } from 'react-redux';
|
||||||
|
|
||||||
export default function NavLinks({ fetch, onSearchSuccess, clearSearch }) {
|
export default function NavLinks({ fetch, onSearchSuccess, clearSearch }) {
|
||||||
|
const { searchEnabled } = useSelector((state) => state.search);
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* <SearchBar fetch={fetch} onSuccess={onSearchSuccess} clearSearch={clearSearch}/> */}
|
{ !!searchEnabled && <SearchBar fetch={fetch} onSuccess={onSearchSuccess} clearSearch={clearSearch}/>}
|
||||||
<ClearConvos />
|
<ClearConvos />
|
||||||
<DarkMode />
|
<DarkMode />
|
||||||
<Logout />
|
<Logout />
|
||||||
{/* <NavLink
|
|
||||||
svg={LogOutIcon}
|
|
||||||
text="Log out"
|
|
||||||
/> */}
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { createSlice } from '@reduxjs/toolkit';
|
import { createSlice } from '@reduxjs/toolkit';
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
|
searchEnabled: false,
|
||||||
search: false,
|
search: false,
|
||||||
query: '',
|
query: '',
|
||||||
};
|
};
|
||||||
|
|
@ -10,7 +11,7 @@ const currentSlice = createSlice({
|
||||||
initialState,
|
initialState,
|
||||||
reducers: {
|
reducers: {
|
||||||
setSearchState: (state, action) => {
|
setSearchState: (state, action) => {
|
||||||
state.search = action.payload;
|
state.searchEnabled = action.payload;
|
||||||
},
|
},
|
||||||
setQuery: (state, action) => {
|
setQuery: (state, action) => {
|
||||||
const q = action.payload;
|
const q = action.payload;
|
||||||
|
|
|
||||||
23
client/src/utils/userAuth.js
Normal file
23
client/src/utils/userAuth.js
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
export default async function fetchData() {
|
||||||
|
try {
|
||||||
|
const response = await axios.get('/api/me', {
|
||||||
|
timeout: 1000,
|
||||||
|
withCredentials: true
|
||||||
|
});
|
||||||
|
const user = response.data;
|
||||||
|
if (user) {
|
||||||
|
// dispatch(setUser(user));
|
||||||
|
// callback(user);
|
||||||
|
return user;
|
||||||
|
} else {
|
||||||
|
console.log('Not login!');
|
||||||
|
window.location.href = '/auth/login';
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
console.log('Not login!');
|
||||||
|
window.location.href = '/auth/login';
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue