Skip to content

Commit

Permalink
refactor(ui): display skeleton before onLoaded is executed (#2235)
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung authored May 24, 2024
1 parent eb0128f commit 5aedf9c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { QuestionAnswerList } from './question-answer'
import { useTabbyAnswer } from './use-tabby-answer'
import { useLatest } from '@/lib/hooks/use-latest'
import { useDebounceCallback } from '@/lib/hooks/use-debounce'
import { ListSkeleton } from '../skeleton'

type ChatContextValue = {
isLoading: boolean
Expand Down Expand Up @@ -131,9 +132,10 @@ function ChatRenderer(
}: ChatProps,
ref: React.ForwardedRef<ChatRef>
) {
const [initialized, setInitialzed] = React.useState(false)
const isOnLoadExecuted = React.useRef(false)
const [qaPairs, setQaPairs] = React.useState(initialMessages ?? [])
const [input, setInput] = React.useState<string>('')
const loaded = React.useRef(false)

const { triggerRequest, isLoading, error, answer, stop } = useTabbyAnswer({
api,
Expand Down Expand Up @@ -334,7 +336,7 @@ function ChatRenderer(
}

React.useEffect(() => {
if (!loaded.current) return
if (!isOnLoadExecuted.current) return
onThreadUpdates(qaPairs)
}, [qaPairs])

Expand All @@ -351,12 +353,15 @@ function ChatRenderer(
)

React.useEffect(() => {
if (loaded?.current) return
if (isOnLoadExecuted.current) return

loaded.current = true
isOnLoadExecuted.current = true
onLoaded?.()
setInitialzed(true)
}, [])

if (!initialized) return <ListSkeleton />

return (
<ChatContext.Provider
value={{
Expand Down

0 comments on commit 5aedf9c

Please sign in to comment.