Skip to content

Commit

Permalink
Merge pull request #181 from KDT-hahahoho/Feat/#78
Browse files Browse the repository at this point in the history
�AI 심리상담 gpt 응답에 타이핑 효과 추가
  • Loading branch information
s0zzang authored Nov 20, 2024
2 parents 01083bc + 6b678f1 commit d975b49
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/hooks/useTyping.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useState, useEffect } from 'react';

const TypingEffect = ({ text, container }: { text: string; container: HTMLDivElement }) => {
const [displayedText, setDisplayedText] = useState('');

useEffect(() => {
let index = 0;
const interval = setInterval(() => {
if (index < text.length - 1) {
setDisplayedText((prev) => prev + text[index]);
index++;
container.scrollTo({ top: container.scrollHeight - container.clientHeight, behavior: 'smooth' });
} else {
clearInterval(interval);
}
}, 50);
console.log(container.scrollHeight);
return () => clearInterval(interval);
}, [text]);

return <p>{displayedText}</p>;
};

export default TypingEffect;
7 changes: 6 additions & 1 deletion src/pages/counseling/Counseling.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import CounselingGuide from './CounslingGuide';
import sendIcon from '/img/icon-send.svg';
import wishIcon from '/img/icon-wish-profile.svg';
import PageTitle from '@components/common/PageTitle';
import TypingEffect from '@hooks/useTyping';

interface Message {
sender?: string;
Expand Down Expand Up @@ -269,7 +270,11 @@ const Counseling = () => {

{messages.map(({ sender, message }, idx) => (
<li key={`${sender}-${message!.slice(0, 10)}-${idx}`} className={sender}>
<p>{message}</p>
{message && sender === 'gpt' ? (
<TypingEffect text={message} container={scrollBoxRef.current!} />
) : (
<p>{message}</p>
)}
</li>
))}

Expand Down

0 comments on commit d975b49

Please sign in to comment.