@@ -5,18 +5,22 @@ import Icons from '../../../../public/Icons';
55import getUserProfileService from '@/app/services/getUserProfile.service' ;
66import { UserProfileResponseInterface } from '@/store/userProfile/interface' ;
77import logout from '@/app/services/logout' ;
8- import { useSelector } from 'react-redux' ;
8+ import { useDispatch , useSelector } from 'react-redux' ;
99import { RootState } from '@/store/store' ;
10+ import { fetchChatHistoryListRequest } from '@/store/chat/history/list/slice' ;
1011
1112const 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' } />
0 commit comments