Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions components/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import equal from 'fast-deep-equal';
import { AnimatePresence, motion } from 'framer-motion';
import { memo, useState } from 'react';

import { useThinkingText } from '@/hooks/use-thinking-text';
import type { Vote } from '@/lib/db/schema';
import { cn } from '@/lib/utils';

Expand Down Expand Up @@ -206,6 +207,8 @@ export const PreviewMessage = memo(
export const ThinkingMessage = () => {
const role = 'assistant';

const thinkingText = useThinkingText();

return (
<motion.div
className="w-full mx-auto max-w-3xl px-4 group/message "
Expand All @@ -225,10 +228,19 @@ export const ThinkingMessage = () => {
<SparklesIcon size={14} />
</div>

<div className="flex flex-col gap-2 w-full">
<div className="flex flex-col gap-4 text-muted-foreground">
Thinking...
</div>
<div className="flex flex-col justify-center gap-2 w-full">
<AnimatePresence mode="wait">
<motion.div
key={thinkingText}
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ duration: 0.3 }}
className="text-muted-foreground absolute"
>
{thinkingText}
</motion.div>
</AnimatePresence>
</div>
</div>
</motion.div>
Expand Down
30 changes: 30 additions & 0 deletions hooks/use-thinking-text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useState, useEffect } from 'react';

/**
* A custom hook that returns a thinking text that changes every few seconds
* with an exponentially increasing interval.
* @param base - The base of the exponential function
* @returns The current thinking text
*/
export const useThinkingText = (base = 3) => {
const texts = [
'Thinking...',
'Calling relevant tools...',
'Fetching external data...',
'Finalizing response...',
];
const [index, setIndex] = useState(0);

useEffect(() => {
texts.slice(0, -1).map((_, textIndex) => {
setTimeout(
() => {
setIndex(textIndex + 1);
},
1000 * 3 ** (textIndex + 1),
);
});
}, []);

return texts[index];
};