File tree Expand file tree Collapse file tree 2 files changed +16
-1
lines changed
Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ const SubHeader = ({
2424 const isChatPage = pathname === "/chat" ;
2525 const isFirstQuestionPage = currentPage === 1 ;
2626 const mode = state ?. mode ;
27+ const chatId = state ?. id ;
2728
2829 const handleGoBack = ( ) => {
2930 if ( isProgressPage && ! isFirstQuestionPage ) {
@@ -45,6 +46,10 @@ const SubHeader = ({
4546 const handleCancel = ( ) => setIsLeaveChatModalOpen ( false ) ;
4647
4748 const handleConfirm = ( ) => {
49+ if ( chatId ) {
50+ sessionStorage . removeItem ( `chatMessages_${ chatId } ` ) ;
51+ }
52+
4853 setIsLeaveChatModalOpen ( false ) ;
4954 navigate ( "/" ) ;
5055 } ;
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ interface ChatResponse {
2020
2121const Chat = ( ) => {
2222 const { state } = useLocation ( ) ;
23- const { mbti, mode, id } = state ;
23+ const { mbti, mode, id = Date . now ( ) . toString ( ) } = state ;
2424
2525 const [ messages , setMessages ] = useState < Message [ ] > ( [ ] ) ;
2626 const [ input , setInput ] = useState ( "" ) ;
@@ -30,11 +30,21 @@ const Chat = () => {
3030 const chatTitle = `${ mbti } 와 대화` ;
3131 const assistantInfo = mbti ;
3232 const assistantImgUrl = pickMbtiImage ( assistantInfo ) ;
33+ const storageKey = `chatMessages_${ id } ` ;
3334
3435 useEffect ( ( ) => {
3536 bottomRef . current ?. scrollIntoView ( { behavior : "smooth" } ) ;
3637 } , [ messages , isOpen ] ) ;
3738
39+ useEffect ( ( ) => {
40+ const stored = sessionStorage . getItem ( storageKey ) ;
41+ if ( stored ) setMessages ( JSON . parse ( stored ) ) ;
42+ } , [ storageKey ] ) ;
43+
44+ useEffect ( ( ) => {
45+ sessionStorage . setItem ( storageKey , JSON . stringify ( messages ) ) ;
46+ } , [ messages , storageKey ] ) ;
47+
3848 const handleToggleTips = ( ) => {
3949 const nextAction = ! isOpen ;
4050
You can’t perform that action at this time.
0 commit comments