Commit ceee81e4 by HoMeTown

feat: 处理未登录的情况下不允许进入chat页

parent 79f1f5ac
...@@ -6,13 +6,27 @@ import { HistoryBarList } from './components/HistoryBarList' ...@@ -6,13 +6,27 @@ import { HistoryBarList } from './components/HistoryBarList'
import SearchIcon from '@/assets/svg/search.svg?react' import SearchIcon from '@/assets/svg/search.svg?react'
import HistoryMenuIcon from '@/assets/svg/historyMenu.svg?react' import HistoryMenuIcon from '@/assets/svg/historyMenu.svg?react'
import { ConversationModal } from '@/components/ConversationModal' import { ConversationModal } from '@/components/ConversationModal'
import { useAppSelector } from '@/store/hook'
import useToast from '@/hooks/useToast'
interface HistoryBarProps { interface HistoryBarProps {
isVisible: boolean isVisible: boolean
} }
export const HistoryBar: React.FC<HistoryBarProps> = ({ isVisible }) => { export const HistoryBar: React.FC<HistoryBarProps> = ({ isVisible }) => {
const showToast = useToast()
const [isOpenConversationModal, setIsOpenConversationModal] = useState(false) const [isOpenConversationModal, setIsOpenConversationModal] = useState(false)
const { conversations } = useAppSelector(state => state.conversation)
const handleOpen = () => {
if (conversations.length === 0) {
showToast('暂无记录,快去提问吧!', 'default', {
icon: '🕑',
})
return
}
setIsOpenConversationModal(true)
}
return ( return (
<AnimatePresence> <AnimatePresence>
{isVisible && ( {isVisible && (
...@@ -31,7 +45,7 @@ export const HistoryBar: React.FC<HistoryBarProps> = ({ isVisible }) => { ...@@ -31,7 +45,7 @@ export const HistoryBar: React.FC<HistoryBarProps> = ({ isVisible }) => {
<HistoryBarList /> <HistoryBarList />
</div> </div>
<div className="text-[12px] border-t-solid border-t-[1px] border-t-[#82969C12] w-full h-[48px] flex items-center justify-center"> <div className="text-[12px] border-t-solid border-t-[1px] border-t-[#82969C12] w-full h-[48px] flex items-center justify-center">
<Button onClick={() => setIsOpenConversationModal(true)} className="w-full" color="primary" variant="light" startContent={<HistoryMenuIcon />}> <Button onClick={handleOpen} className="w-full" color="primary" variant="light" startContent={<HistoryMenuIcon />}>
<span className="text-[#82969C]">管理对话记录</span> <span className="text-[#82969C]">管理对话记录</span>
</Button> </Button>
</div> </div>
......
...@@ -44,6 +44,7 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c ...@@ -44,6 +44,7 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c
} }
const handleLogout = () => { const handleLogout = () => {
onSetHistoryVisible(false) onSetHistoryVisible(false)
navigate('/')
} }
useEffect(() => { useEffect(() => {
......
import React, { useEffect } from 'react' import React, { useEffect } from 'react'
import { useLocation } from 'react-router-dom' import { useLocation, useNavigate } from 'react-router-dom'
import { clearCurrentConversation, setCurrentConversation } from '@/store/conversationSlice' import { clearCurrentConversation, setCurrentConversation } from '@/store/conversationSlice'
import { useAppDispatch } from '@/store/hook' import { useAppDispatch } from '@/store/hook'
...@@ -7,6 +7,7 @@ export function withRouteChangeHandler(WrappedComponent: React.ComponentType) { ...@@ -7,6 +7,7 @@ export function withRouteChangeHandler(WrappedComponent: React.ComponentType) {
return (props: any) => { return (props: any) => {
const location = useLocation() const location = useLocation()
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
const navigate = useNavigate()
useEffect(() => { useEffect(() => {
if (location.pathname === '/') { if (location.pathname === '/') {
...@@ -14,6 +15,13 @@ export function withRouteChangeHandler(WrappedComponent: React.ComponentType) { ...@@ -14,6 +15,13 @@ export function withRouteChangeHandler(WrappedComponent: React.ComponentType) {
} }
else if (location.pathname.startsWith('/chat/')) { else if (location.pathname.startsWith('/chat/')) {
const conversationId = location.pathname.split('/')[2] const conversationId = location.pathname.split('/')[2]
if (!JSON.parse(window.localStorage.getItem('__TOKEN__') || '')) {
// 如果没有有效的 token,重定向到首页
navigate('/')
return // 提前返回,不执行后续的 dispatch
}
dispatch(setCurrentConversation(conversationId)) dispatch(setCurrentConversation(conversationId))
} }
// 这里可以添加其他路由相关的逻辑 // 这里可以添加其他路由相关的逻辑
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment