This commit is contained in:
liubiren 2026-07-17 07:47:03 +08:00
parent bd1e5912a5
commit 6eb839fd9d
6 changed files with 49 additions and 91 deletions

View File

@ -1,17 +1,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from application.states.auth import AuthState from application.states.auth import AuthState
from application.states.conversation import ConversationState from application.states.conversation import ConversationState
from application.states.conversation_historycollapse import ConversationHistoryCollapseState
from application.states.create_conversation import CreateConversationState
from application.states.database import DatabaseState from application.states.database import DatabaseState
from application.states.sidebar import SidebarState
__all__ = [ __all__ = [
"AuthState", "AuthState",
"ConversationHistoryCollapseState",
"ConversationState", "ConversationState",
"CreateConversationState",
"DatabaseState", "DatabaseState",
"SidebarState",
] ]

View File

@ -127,3 +127,24 @@ class AuthState(rx.State):
self.code = "" self.code = ""
self.agree_policy = False self.agree_policy = False
return rx.toast("登录成功", color="green") return rx.toast("登录成功", color="green")
@rx.var
async def storage_key(self) -> str:
"""
LocalStorage
"""
# 获取认证状态
auth = await self.get_state(AuthState)
# 获取用户唯一标识
user_id = auth.user_id or "default"
return f"sidebar_{user_id}"
# 激活的导航按钮
activated_nav_button: str = rx.LocalStorage(storage_key, sync=True)
@rx.event
def switch_nav_button(self, button: str):
"""
将指定导航按钮设置为激活的导航按钮
"""
self.activated_nav_button = button

View File

@ -26,7 +26,6 @@ import reflex as rx
from application.models import Conversation, Dialog, ThoughtNode from application.models import Conversation, Dialog, ThoughtNode
from application.states.auth import AuthState from application.states.auth import AuthState
from application.states.create_conversation import CreateConversationState
from application.states.database import DatabaseState from application.states.database import DatabaseState
@ -68,15 +67,22 @@ class ConversationState(rx.State):
# 用户唯一标识 # 用户唯一标识
user_id: str = "" user_id: str = ""
# 会话字典 # 会话字典
conversations: Dict[str, Conversation] = {} conversations: Dict[str, Conversation] = {}
# 当前会话唯一标识 # 会话唯一标识
conversation_id: str = "" conversation_id: str = ""
# 会话历史展示状态True表示展示False表示隐藏
is_conversation_history_shown: bool = False
# 正在新建会话状态True表示正在新建False表示未正在新建
is_creating_conversation: bool = False
@rx.event @rx.event
async def on_mount(self): async def on_mount(self):
""" """
页面挂载时初始化会话列表和当前会话唯一标识 页面挂载时初始化会话列表和会话唯一标识
""" """
# 获取登录用户唯一标识 # 获取登录用户唯一标识
auth_state = await self.get_state(AuthState) auth_state = await self.get_state(AuthState)
@ -120,7 +126,7 @@ class ConversationState(rx.State):
# 获取会话描述 # 获取会话描述
description = form_data["description"].strip() or "新会话" description = form_data["description"].strip() or "新会话"
# 先在数据库新建会话,再在会话字典新建会话 # 先在数据库中创建会话记录,再在会话字典中创建会话记录
database_state = await self.get_state(DatabaseState) database_state = await self.get_state(DatabaseState)
conversation_id, created_at = await database_state.create_conversation( conversation_id, created_at = await database_state.create_conversation(
user_id=self.user_id, description=description user_id=self.user_id, description=description
@ -128,11 +134,10 @@ class ConversationState(rx.State):
self.conversations[conversation_id] = Conversation( self.conversations[conversation_id] = Conversation(
description=description, created_at=created_at description=description, created_at=created_at
) )
# 将新建会话的唯一标识设置为当前会话唯一标识
self.conversation_id = conversation_id self.conversation_id = conversation_id
# 将正在新建会话状态设置为已完成
# 关闭新建会话模态窗 self.is_creating_conversation = False
create_conversation_state = await self.get_state(CreateConversationState)
create_conversation_state.is_modal_open = False
@rx.event @rx.event
async def delete_conversation(self, conversation_id: str) -> None: async def delete_conversation(self, conversation_id: str) -> None:
@ -402,3 +407,18 @@ class ConversationState(rx.State):
# 指定运行 # 指定运行
dialog = self.conversations[self.conversation_id].dialogs[dialog_id] dialog = self.conversations[self.conversation_id].dialogs[dialog_id]
dialog.is_collapse_expanded = not dialog.is_collapse_expanded dialog.is_collapse_expanded = not dialog.is_collapse_expanded
@rx.event
def toggle_conversation_history_shown(self) -> None:
"""
切换会话历史展示状态
:return: None
"""
self.is_conversation_history_shown = not self.is_conversation_history_shown
@rx.event
def toggle_creating_conversation(self) -> None:
"""
切换正在新建会话状态
"""
self.is_creating_conversation = not self.is_creating_conversation

View File

@ -1,22 +0,0 @@
# -*- coding: utf-8 -*-
"""
会话历史折叠面板状态
"""
import reflex as rx
class ConversationHistoryCollapseState(rx.State):
"""
会话历史折叠面板状态
"""
# 展开状态True表示展开False表示折叠
is_expanded: bool = False
@rx.event
def toggle_expanded(self) -> None:
"""
切换展开状态
:return: None
"""
self.is_expanded = not self.is_expanded

View File

@ -1,21 +0,0 @@
# -*- coding: utf-8 -*-
"""
新建会话状态
"""
import reflex as rx
class CreateConversationState(rx.State):
"""
新建会话状态
"""
# 新建会话模态窗打开状态True表示打开False表示关闭
is_modal_open: bool = False
@rx.event
def toggle_modal(self) -> None:
"""
打开/关闭新建会话模态窗
"""
self.is_modal_open = not self.is_modal_open

View File

@ -1,34 +0,0 @@
# -*- coding: utf-8 -*-
"""
侧边栏状态
"""
import reflex as rx
from application.states.auth import AuthState
class SidebarState(rx.State):
"""
侧边栏状态
"""
@rx.var
async def storage_key(self) -> str:
"""
LocalStorage
"""
# 获取认证状态
auth = await self.get_state(AuthState)
# 获取用户唯一标识
user_id = auth.user_id or "default"
return f"sidebar_{user_id}"
# 激活的导航按钮
activated_nav_button: str = rx.LocalStorage(storage_key, sync=True)
@rx.event
def switch_nav_button(self, button: str):
"""
将指定导航按钮设置为激活的导航按钮
"""
self.activated_nav_button = button