Skip to content

Commit a6ba8bc

Browse files
committed
useLayoutEffect
1 parent eb64de8 commit a6ba8bc

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

contesto/src/chat/chat-provider.tsx

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import { createTrackedSelector } from 'react-tracked'
55

66
import * as Ariakit from '@ariakit/react'
77
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'
916
import type { StoreApi, UseBoundStore } from 'zustand'
1017
import { ComboboxStore } from '@ariakit/react'
1118
import { cn } from '../lib/cn.js'
@@ -187,7 +194,7 @@ const ChatProvider = (props: {
187194
window.removeEventListener('chatRegenerate', handleChatRegenerate)
188195
}
189196
}, [submit])
190-
useEffect(() => {
197+
useLayoutEffect(() => {
191198
console.log(`chat remounted, scrolling into the user message`)
192199
const messages = stableInitialState.messages || []
193200
const lastUserMessage = [...messages]
@@ -234,27 +241,15 @@ const ChatProvider = (props: {
234241
// Subscribe to text changes and persist to cookies
235242
const unsubscribe = store.subscribe((state, prevState) => {
236243
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+
)
258253
}
259254
})
260255

0 commit comments

Comments
 (0)