Skip to content

Commit 34a92fb

Browse files
STetsingAniket-Engg
authored andcommitted
effect on streaming
1 parent cb474c1 commit 34a92fb

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

libs/remix-ui/remix-ai/src/lib/components/Default.tsx

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useState, useEffect } from 'react'
22
import '../remix-ai.css'
33
import { DefaultModels, GenerationParams, ChatHistory, HandleStreamResponse } from '@remix/remix-ai-core';
44
import { ConversationStarter, StreamSend, StreamingAdapterObserver, useAiChatApi } from '@nlux/react';
@@ -7,28 +7,40 @@ import { user, assistantAvatar } from './personas';
77
import { highlighter } from '@nlux/highlighter'
88
import './color.css'
99
import '@nlux/themes/unstyled.css';
10+
import copy from 'copy-to-clipboard'
1011

1112
export let ChatApi = null
1213

1314
export const Default = (props) => {
15+
const [is_streaming, setIS_streaming] = useState<boolean>(false)
1416

1517
const HandleCopyToClipboard = () => {
16-
const codeBlocks = document.getElementsByClassName('code-block')
18+
const markdown = document.getElementsByClassName('nlux-chatSegments-container')
19+
if (markdown.length < 1) return
20+
21+
const codeBlocks = markdown[0].getElementsByClassName('code-block')
1722
Array.from(codeBlocks).forEach((block) => {
1823
const copyButtons = block.getElementsByClassName('nlux-comp-copyButton')
1924
Array.from(copyButtons).forEach((cp_btn) => {
20-
cp_btn.removeEventListener('click', () => {})
21-
cp_btn.addEventListener('click', async () => {
22-
await navigator.clipboard.writeText(block.textContent)
23-
})
25+
const hdlr = async () => {
26+
copy(block.textContent)
27+
}
28+
cp_btn.removeEventListener('click', async() => { hdlr() })
29+
cp_btn.addEventListener('click', async () => { hdlr() })
2430
})
2531
})
2632
}
33+
34+
useEffect(() => {
35+
HandleCopyToClipboard();
36+
}, [is_streaming]);
37+
2738
const send: StreamSend = async (
2839
prompt: string,
2940
observer: StreamingAdapterObserver,
3041
) => {
3142
GenerationParams.stream_result = true
43+
setIS_streaming(true)
3244
GenerationParams.return_stream_response = GenerationParams.stream_result
3345

3446
let response = null
@@ -44,15 +56,15 @@ export const Default = (props) => {
4456
observer.next(' ') // Add a space to flush the last message
4557
ChatHistory.pushHistory(prompt, result)
4658
observer.complete()
47-
HandleCopyToClipboard()
59+
setTimeout(() => { setIS_streaming(false) }, 1000)
4860
}
4961
)
5062
else {
5163
observer.next(response)
5264
observer.complete()
53-
HandleCopyToClipboard()
54-
}
5565

66+
setTimeout(() => { setIS_streaming(false) }, 1000)
67+
}
5668
};
5769
ChatApi = useAiChatApi();
5870
const conversationStarters: ConversationStarter[] = [
@@ -89,7 +101,7 @@ export const Default = (props) => {
89101
}}
90102
messageOptions={{ showCodeBlockCopyButton: true,
91103
editableUserMessages: true,
92-
streamingAnimationSpeed: 2,
104+
streamingAnimationSpeed: 1,
93105
waitTimeBeforeStreamCompletion: 1000,
94106
syntaxHighlighter: highlighter
95107
}}

0 commit comments

Comments
 (0)