@@ -5,7 +5,14 @@ import { createTrackedSelector } from 'react-tracked'
5
5
6
6
import * as Ariakit from '@ariakit/react'
7
7
import { create } from 'zustand'
8
- import { createContext , useContext , useMemo , useEffect , useRef } from 'react'
8
+ import {
9
+ createContext ,
10
+ useContext ,
11
+ useMemo ,
12
+ useEffect ,
13
+ useRef ,
14
+ useLayoutEffect ,
15
+ } from 'react'
9
16
import type { StoreApi , UseBoundStore } from 'zustand'
10
17
import { ComboboxStore } from '@ariakit/react'
11
18
import { cn } from '../lib/cn.js'
@@ -187,7 +194,7 @@ const ChatProvider = (props: {
187
194
window . removeEventListener ( 'chatRegenerate' , handleChatRegenerate )
188
195
}
189
196
} , [ submit ] )
190
- useEffect ( ( ) => {
197
+ useLayoutEffect ( ( ) => {
191
198
console . log ( `chat remounted, scrolling into the user message` )
192
199
const messages = stableInitialState . messages || [ ]
193
200
const lastUserMessage = [ ...messages ]
@@ -234,27 +241,15 @@ const ChatProvider = (props: {
234
241
// Subscribe to text changes and persist to cookies
235
242
const unsubscribe = store . subscribe ( ( state , prevState ) => {
236
243
if ( state . draftText !== prevState . draftText ) {
237
- if ( state . draftText ) {
238
- const encodedDraft = encodeURIComponent ( state . draftText )
239
- document . cookie = cookie . serialize (
240
- CONTESTO_DRAFT_MESSAGE_KEY ,
241
- encodedDraft ,
242
- {
243
- path : '/' ,
244
- maxAge : 60 * 60 * 24 * 7 , // 7 days
245
- } ,
246
- )
247
- } else {
248
- // Remove the cookie
249
- document . cookie = cookie . serialize (
250
- CONTESTO_DRAFT_MESSAGE_KEY ,
251
- '' ,
252
- {
253
- maxAge : 0 ,
254
- path : '/' ,
255
- } ,
256
- )
257
- }
244
+ const encodedDraft = encodeURIComponent ( state . draftText || '' )
245
+ document . cookie = cookie . serialize (
246
+ CONTESTO_DRAFT_MESSAGE_KEY ,
247
+ encodedDraft ,
248
+ {
249
+ path : '/' ,
250
+ maxAge : 60 * 60 * 24 * 7 , // 7 days
251
+ } ,
252
+ )
258
253
}
259
254
} )
260
255
0 commit comments