@@ -3,11 +3,11 @@ import { useEffect, useReducer, useState, useMemo } from 'react';
33import { useRouter } from 'next/router' ;
44
55import { useCreateReducer } from '@/hooks/useCreateReducer' ;
6+ import { useIsMobile } from '@/hooks/useMobile' ;
67import useTranslation from '@/hooks/useTranslation' ;
78
89import { currentISODateString } from '@/utils/date' ;
910import { findSelectedMessageByLeafId } from '@/utils/message' ;
10- import { getSettings } from '@/utils/settings' ;
1111import { getUserSession , redirectToLoginPage } from '@/utils/user' ;
1212
1313import {
@@ -36,6 +36,7 @@ import {
3636import { setModelMap , setModels } from '@/actions/model.actions' ;
3737import { setDefaultPrompt , setPrompts } from '@/actions/prompt.actions' ;
3838import {
39+ setChatBarWidth ,
3940 setShowChatBar ,
4041} from '@/actions/setting.actions' ;
4142import HomeContext , {
@@ -67,10 +68,16 @@ import {
6768 postChats ,
6869 stopChat ,
6970} from '@/apis/clientApis' ;
71+ import {
72+ getDesktopChatbarMaxWidth ,
73+ getEffectiveChatbarWidth ,
74+ getSettings ,
75+ } from '@/utils/settings' ;
7076
7177const HomeContent = ( ) => {
7278 const router = useRouter ( ) ;
7379 const { t } = useTranslation ( ) ;
80+ const isMobile = useIsMobile ( ) ;
7481 const [ chatState , chatDispatch ] = useReducer ( chatReducer , chatInitialState ) ;
7582 const [ messageState , messageDispatch ] = useReducer (
7683 messageReducer ,
@@ -88,9 +95,27 @@ const HomeContent = () => {
8895 promptReducer ,
8996 promptInitialState ,
9097 ) ;
98+ const [ viewportWidth , setViewportWidth ] = useState < number > (
99+ typeof window !== 'undefined' ? window . innerWidth : 1200 ,
100+ ) ;
91101
92102 const { chats, chatPaging, stopIds, selectedChatId, chatGroups } = chatState ;
93103 const { models } = modelState ;
104+ const { showChatBar, chatBarWidth } = settingState ;
105+ const chatBarMaxWidth = useMemo (
106+ ( ) => getDesktopChatbarMaxWidth ( viewportWidth ) ,
107+ [ viewportWidth ] ,
108+ ) ;
109+ const effectiveChatBarWidth = useMemo (
110+ ( ) =>
111+ getEffectiveChatbarWidth ( {
112+ preferredWidth : chatBarWidth ,
113+ viewportWidth,
114+ isMobileView : isMobile ,
115+ isOpen : showChatBar ,
116+ } ) ,
117+ [ chatBarWidth , isMobile , showChatBar , viewportWidth ] ,
118+ ) ;
94119
95120 // 解析 hash 中的 chatId,例如 "#/abc" -> "abc"
96121 const getHashChatId = ( ) : string | undefined => {
@@ -378,8 +403,24 @@ const HomeContent = () => {
378403
379404 useEffect ( ( ) => {
380405 // 加载设置
381- const { showChatBar } = getSettings ( ) ;
382- settingDispatch ( setShowChatBar ( showChatBar ) ) ;
406+ const settings = getSettings ( ) ;
407+ settingDispatch ( setShowChatBar ( settings . showChatBar ) ) ;
408+ settingDispatch ( setChatBarWidth ( settings . chatBarWidth , false ) ) ;
409+ } , [ ] ) ;
410+
411+ useEffect ( ( ) => {
412+ if ( typeof window === 'undefined' ) return undefined ;
413+
414+ const handleResize = ( ) => {
415+ setViewportWidth ( window . innerWidth ) ;
416+ } ;
417+
418+ handleResize ( ) ;
419+ window . addEventListener ( 'resize' , handleResize ) ;
420+
421+ return ( ) => {
422+ window . removeEventListener ( 'resize' , handleResize ) ;
423+ } ;
383424 } , [ ] ) ;
384425
385426 useEffect ( ( ) => {
@@ -438,6 +479,8 @@ const HomeContent = () => {
438479 ...modelState ,
439480 ...settingState ,
440481 ...promptState ,
482+ effectiveChatBarWidth,
483+ chatBarMaxWidth,
441484 } ,
442485 selectedChat,
443486 chatDispatch : chatDispatch ,
0 commit comments