fix(ui): 优化会话初始化逻辑
- 首次启动时自动创建会话 - 用户删除所有会话后不再自动创建,显示空状态 - 使用 localStorage 记录是否曾有会话
This commit is contained in:
@@ -33,19 +33,29 @@ export function App() {
|
|||||||
const [showProviders, setShowProviders] = useState(false);
|
const [showProviders, setShowProviders] = useState(false);
|
||||||
const [sessionTitleUpdate, setSessionTitleUpdate] = useState<{ sessionId: string; name: string } | null>(null);
|
const [sessionTitleUpdate, setSessionTitleUpdate] = useState<{ sessionId: string; name: string } | null>(null);
|
||||||
|
|
||||||
// 初始化:加载或创建会话
|
// 初始化:加载会话(只在首次启动时自动创建)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const HAS_SESSIONS_KEY = 'ai-assistant-has-sessions';
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
try {
|
try {
|
||||||
const { data: sessions } = await listSessions();
|
const { data: sessions } = await listSessions();
|
||||||
|
|
||||||
if (sessions.length > 0) {
|
if (sessions.length > 0) {
|
||||||
// 选择最近的会话
|
// 有会话,选择最近的
|
||||||
setCurrentSessionId(sessions[0].id);
|
setCurrentSessionId(sessions[0].id);
|
||||||
|
localStorage.setItem(HAS_SESSIONS_KEY, 'true');
|
||||||
} else {
|
} else {
|
||||||
// 创建新会话
|
// 无会话:检查是否是首次启动
|
||||||
const { data: newSession } = await createSession();
|
const hasHadSessions = localStorage.getItem(HAS_SESSIONS_KEY);
|
||||||
setCurrentSessionId(newSession.id);
|
|
||||||
|
if (!hasHadSessions) {
|
||||||
|
// 首次启动,自动创建会话
|
||||||
|
const { data: newSession } = await createSession();
|
||||||
|
setCurrentSessionId(newSession.id);
|
||||||
|
localStorage.setItem(HAS_SESSIONS_KEY, 'true');
|
||||||
|
}
|
||||||
|
// 用户删除了所有会话:不自动创建,显示空状态
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to initialize:', error);
|
console.error('Failed to initialize:', error);
|
||||||
|
|||||||
@@ -35,19 +35,29 @@ export function App() {
|
|||||||
const [showProviders, setShowProviders] = useState(false);
|
const [showProviders, setShowProviders] = useState(false);
|
||||||
const [sessionTitleUpdate, setSessionTitleUpdate] = useState<{ sessionId: string; name: string } | null>(null);
|
const [sessionTitleUpdate, setSessionTitleUpdate] = useState<{ sessionId: string; name: string } | null>(null);
|
||||||
|
|
||||||
// 初始化:加载或创建会话
|
// 初始化:加载会话(只在首次启动时自动创建)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const HAS_SESSIONS_KEY = 'ai-assistant-has-sessions';
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
try {
|
try {
|
||||||
const { data: sessions } = await listSessions();
|
const { data: sessions } = await listSessions();
|
||||||
|
|
||||||
if (sessions.length > 0) {
|
if (sessions.length > 0) {
|
||||||
// 选择最近的会话
|
// 有会话,选择最近的
|
||||||
setCurrentSessionId(sessions[0].id);
|
setCurrentSessionId(sessions[0].id);
|
||||||
|
localStorage.setItem(HAS_SESSIONS_KEY, 'true');
|
||||||
} else {
|
} else {
|
||||||
// 创建新会话
|
// 无会话:检查是否是首次启动
|
||||||
const { data: newSession } = await createSession();
|
const hasHadSessions = localStorage.getItem(HAS_SESSIONS_KEY);
|
||||||
setCurrentSessionId(newSession.id);
|
|
||||||
|
if (!hasHadSessions) {
|
||||||
|
// 首次启动,自动创建会话
|
||||||
|
const { data: newSession } = await createSession();
|
||||||
|
setCurrentSessionId(newSession.id);
|
||||||
|
localStorage.setItem(HAS_SESSIONS_KEY, 'true');
|
||||||
|
}
|
||||||
|
// 用户删除了所有会话:不自动创建,显示空状态
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to initialize:', error);
|
console.error('Failed to initialize:', error);
|
||||||
|
|||||||
Reference in New Issue
Block a user