Skip to content

Commit 330773d

Browse files
committed
set chat in mobile menu
1 parent 249efdc commit 330773d

2 files changed

Lines changed: 60 additions & 24 deletions

File tree

src/app/components/SafeLLM/Menu.tsx

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@ import Icons from '../../../../public/Icons';
55
import getUserProfileService from '@/app/services/getUserProfile.service';
66
import { UserProfileResponseInterface } from '@/store/userProfile/interface';
77
import logout from '@/app/services/logout';
8-
import { useSelector } from 'react-redux';
8+
import { useDispatch, useSelector } from 'react-redux';
99
import { RootState } from '@/store/store';
10+
import { fetchChatHistoryListRequest } from '@/store/chat/history/list/slice';
1011

1112
const allowedRolesManagement = ['admin', 'developer', 'management'];
1213

13-
const Menu = ({ currentPath, setShowInnerComponent }:
14-
{ currentPath: string, setShowInnerComponent: any }) => {
14+
const Menu = ({ currentPath, setShowInnerComponent }: { currentPath: string, setShowInnerComponent: any }) => {
1515
const [userProfile, setUserProfile] = useState<UserProfileResponseInterface>();
1616
const userProfileState = useSelector((state: RootState) => state.userProfile.data);
1717
const Router = useRouter();
18-
const [role, setRole] = useState<string>('user');
19-
18+
const [role, setRole] = useState<string>('user');
19+
const dispatch = useDispatch();
20+
const chatHistory = useSelector((state: any) => state.chatHistoryList.items);
21+
const hasMore = useSelector((state: any) => state.chatHistoryList.hasMore);
22+
const isLoading = useSelector((state: any) => state.chatHistoryList.isLoading);
23+
const [isHistoryOpen, setIsHistoryOpen] = useState(false);
2024

2125
useEffect(() => {
2226
if (!userProfileState) {
@@ -32,24 +36,32 @@ const Menu = ({ currentPath, setShowInnerComponent }:
3236

3337
getUserProfileService(true).then((res: any) => {
3438
setRole(res.role);
35-
})
36-
}, []);
37-
39+
});
40+
41+
// Fetch chat history
42+
dispatch(fetchChatHistoryListRequest());
43+
}, [dispatch]);
3844

3945
function handleClick(path: string) {
40-
if (currentPath as any == path)
46+
if (currentPath as any === path)
4147
setShowInnerComponent(true);
4248
else
4349
setShowInnerComponent(true);
44-
Router.push(path);
50+
Router.push(path);
4551
}
4652

4753
function logoutFuc() {
48-
4954
logout();
5055
Router.push('/')
5156
}
5257

58+
const handleScroll = (e: React.UIEvent<HTMLUListElement>) => {
59+
const bottom = e.currentTarget.scrollHeight - e.currentTarget.scrollTop === e.currentTarget.clientHeight;
60+
if (bottom && hasMore && !isLoading) {
61+
dispatch(fetchChatHistoryListRequest());
62+
}
63+
};
64+
5365
return (
5466
<>
5567
<div className={'h-40 flex flex-col justify-center items-center'}>
@@ -74,17 +86,42 @@ const Menu = ({ currentPath, setShowInnerComponent }:
7486
</a>
7587
<Icons name={'direction-left-gray'} />
7688
</li>
77-
{ allowedRolesManagement.includes(role) ?
89+
90+
{chatHistory.length > 0 &&
7891
<li className={'flex justify-between p-4 border-b-2 border-secondary-02 cursor-pointer'}>
79-
<a className={'flex flex-1'} onClick={() => handleClick('/management')}>
80-
<Icons name={'management'} />
81-
<p className={'ml-3'}>management</p>
82-
</a>
83-
<Icons name={'direction-left-gray'} />
84-
</li>
85-
: ''
92+
<a onClick={() => setIsHistoryOpen(!isHistoryOpen)} className={'flex flex-1'}>
93+
<Icons name={'history'} />
94+
<p className={'ml-3'}>History</p>
95+
</a>
96+
<Icons name={isHistoryOpen ? 'direction-up-gray' : 'direction-down-gray'} />
97+
</li>
98+
}
99+
100+
{isHistoryOpen && (
101+
<ul className="pl-8 space-y-1 max-h-40 overflow-y-auto" onScroll={handleScroll}>
102+
{chatHistory.map((chat: any) => (
103+
<li key={chat.id}>
104+
<a
105+
href={`/chat/history/${chat.id}`}
106+
className={`block px-4 py-2 hover:bg-primary-02 hover:text-primary ${currentPath.includes(chat.id) ? 'bg-primary-01 text-primary' : ''}`}
107+
>
108+
{chat.title} - {new Date(chat.date).toLocaleDateString()}
109+
</a>
110+
</li>
111+
))}
112+
</ul>
113+
)}
114+
115+
{allowedRolesManagement.includes(role) ?
116+
<li className={'flex justify-between p-4 border-b-2 border-secondary-02 cursor-pointer'}>
117+
<a className={'flex flex-1'} onClick={() => handleClick('/management')}>
118+
<Icons name={'management'} />
119+
<p className={'ml-3'}>management</p>
120+
</a>
121+
<Icons name={'direction-left-gray'} />
122+
</li>
123+
: ''
86124
}
87-
88125
<li className={'flex justify-between p-4 border-b-2 border-secondary-02 cursor-pointer'}>
89126
<a className={'flex flex-1'} onClick={() => handleClick('/profile')}>
90127
<Icons name={'profile-account'} />

src/app/components/Sidebar/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export default function Sidebar(props: SidebarInterface) {
4444
getUserProfileService(false).then((res: any) => {
4545
setUserProfile(res);
4646
})
47-
debugger;
4847
dispatch(fetchChatHistoryListRequest());
4948

5049
}, [dispatch]);
@@ -112,7 +111,7 @@ export default function Sidebar(props: SidebarInterface) {
112111
]
113112

114113
if (chatHistory.length > 0) {
115-
(items as any).unshift({
114+
(items as any).splice(1,0,{
116115
label: 'History',
117116
path: null,
118117
iconName: 'history',
@@ -173,7 +172,7 @@ export default function Sidebar(props: SidebarInterface) {
173172
</a> :
174173
<a onClick={() => { setIsHistoryOpen(!isHistoryOpen) }}
175174
className={itemsStyle + ' ' +
176-
` ${pathname === item.path ? 'bg-primary-01 text-primary border-r-2 border-primary ' : 'cursor-pointer'}`}>
175+
` ${pathname === item.path ? 'relative bg-primary-01 text-primary border-r-2 border-primary ' : 'relative cursor-pointer'}`}>
177176
<div className={'group-hover:hidden'}>
178177
<Icons name={item.iconName} />
179178
</div>
@@ -183,7 +182,7 @@ export default function Sidebar(props: SidebarInterface) {
183182
<span className="ms-3 group-hover:font-semibold">{item.label}</span>
184183

185184
<div
186-
className={'absolute top-8 right-2 cursor-pointer'}>
185+
className={'absolute top-4 right-2 cursor-pointer'}>
187186
<Icons name={'direction-down'} />
188187
</div>
189188

0 commit comments

Comments
 (0)