Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ldelalande/fix scroll #504

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
124 changes: 82 additions & 42 deletions ui/desktop/src/ChatWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
chats,
setChats,
selectedChatId,
setSelectedChatId,

Check warning on line 42 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'setSelectedChatId' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 42 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'setSelectedChatId' is defined but never used. Allowed unused args must match /^_/u
initialQuery,
setProgressMessage,
setWorking,
Expand All @@ -57,6 +57,16 @@
const [hasMessages, setHasMessages] = useState(false);
const [lastInteractionTime, setLastInteractionTime] = useState<number>(Date.now());
const [showGame, setShowGame] = useState(false);
const messagesEndRef = useRef<HTMLDivElement>(null);
const [working, setWorkingLocal] = useState<Working>(Working.Idle);

useEffect(() => {
setWorking(working);
}, [working, setWorking]);

const updateWorking = (newWorking: Working) => {
setWorkingLocal(newWorking);
};

const {
messages,
Expand All @@ -69,26 +79,30 @@
api: getApiUrl('/reply'),
initialMessages: chat?.messages || [],
onToolCall: ({ toolCall }) => {
setWorking(Working.Working);
updateWorking(Working.Working);
setProgressMessage(`Executing tool: ${toolCall.toolName}`);
requestAnimationFrame(() => scrollToBottom('instant'));

Check failure on line 84 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'requestAnimationFrame' is not defined

Check failure on line 84 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'requestAnimationFrame' is not defined
},
onResponse: (response) => {
if (!response.ok) {
setProgressMessage('An error occurred while receiving the response.');
setWorking(Working.Idle);
updateWorking(Working.Idle);
} else {
setProgressMessage('thinking...');
setWorking(Working.Working);
updateWorking(Working.Working);
}
},
onFinish: async (message, options) => {

Check warning on line 95 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'options' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 95 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'options' is defined but never used. Allowed unused args must match /^_/u
setProgressMessage('Task finished. Click here to expand.');
setWorking(Working.Idle);
setTimeout(() => {
setProgressMessage('Task finished. Click here to expand.');
updateWorking(Working.Idle);
}, 500);

const fetchResponses = await askAi(message.content);
setMessageMetadata((prev) => ({ ...prev, [message.id]: fetchResponses }));

// Only show notification if it's been more than a minute since last interaction
requestAnimationFrame(() => scrollToBottom('smooth'));

Check failure on line 104 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'requestAnimationFrame' is not defined

Check failure on line 104 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'requestAnimationFrame' is not defined

const timeSinceLastInteraction = Date.now() - lastInteractionTime;
window.electron.logInfo("last interaction:" + lastInteractionTime);
if (timeSinceLastInteraction > 60000) { // 60000ms = 1 minute
Expand All @@ -104,7 +118,7 @@
c.id === selectedChatId ? { ...c, messages } : c
);
setChats(updatedChats);
}, [messages, selectedChatId]);

Check warning on line 121 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

React Hook useEffect has missing dependencies: 'chats' and 'setChats'. Either include them or remove the dependency array. If 'setChats' changes too often, find the parent component that defines it and wrap that definition in useCallback

Check warning on line 121 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

React Hook useEffect has missing dependencies: 'chats' and 'setChats'. Either include them or remove the dependency array. If 'setChats' changes too often, find the parent component that defines it and wrap that definition in useCallback

const initialQueryAppended = useRef(false);
useEffect(() => {
Expand All @@ -112,7 +126,7 @@
append({ role: 'user', content: initialQuery });
initialQueryAppended.current = true;
}
}, [initialQuery]);

Check warning on line 129 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

React Hook useEffect has a missing dependency: 'append'. Either include it or remove the dependency array

Check warning on line 129 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

React Hook useEffect has a missing dependency: 'append'. Either include it or remove the dependency array

useEffect(() => {
if (messages.length > 0) {
Expand All @@ -120,15 +134,41 @@
}
}, [messages]);

const scrollToBottom = (behavior: ScrollBehavior = 'smooth') => {

Check failure on line 137 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'ScrollBehavior' is not defined

Check failure on line 137 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'ScrollBehavior' is not defined
if (messagesEndRef.current) {
messagesEndRef.current.scrollIntoView({
behavior,
block: 'end',
inline: 'nearest'
});
}
};

// Single effect to handle all scrolling
useEffect(() => {
if (isLoading || messages.length > 0 || working === Working.Working) {
// Initial scroll
scrollToBottom(isLoading || working === Working.Working ? 'instant' : 'smooth');

// Additional scrolls to catch dynamic content
[100, 300, 500].forEach(delay => {
lily-de marked this conversation as resolved.
Show resolved Hide resolved
setTimeout(() => scrollToBottom('smooth'), delay);
});
}
}, [messages, isLoading, working]);

// Handle submit
const handleSubmit = (e: React.FormEvent) => {
const customEvent = e as CustomEvent;
const content = customEvent.detail?.value || '';
if (content.trim()) {
setLastInteractionTime(Date.now()); // Update last interaction time
setLastInteractionTime(Date.now());
append({
role: 'user',
content: content,
});
// Immediate scroll on submit
scrollToBottom('instant');
}
};

Expand Down Expand Up @@ -198,13 +238,12 @@
{messages.length === 0 ? (
<Splash append={append} />
) : (
<ScrollArea className="flex-1 px-[10px]" id="chat-scroll-area">
<ScrollArea
className="flex-1 px-[10px]"
id="chat-scroll-area"
>
<div className="block h-10" />
<div ref={(el) => {
if (el) {
el.scrollIntoView({ behavior: 'smooth', block: 'end' });
}
}}>
<div>
{messages.map((message) => (
<div key={message.id}>
{message.role === 'user' ? (
Expand All @@ -219,37 +258,38 @@
)}
</div>
))}
</div>
{isLoading && (
<div className="flex items-center justify-center p-4">
<div onClick={() => setShowGame(true)} style={{ cursor: 'pointer' }}>
<LoadingGoose />
</div>
</div>
)}
{error && (
<div className="flex flex-col items-center justify-center p-4">
<div className="text-red-700 dark:text-red-300 bg-red-400/50 p-3 rounded-lg mb-2">
{error.message || 'Honk! Goose experienced an error while responding'}
{error.status && (
<span className="ml-2">(Status: {error.status})</span>
)}
{isLoading && (
<div className="flex items-center justify-center p-4">
<div onClick={() => setShowGame(true)} style={{ cursor: 'pointer' }}>
<LoadingGoose />
</div>
</div>
<div
className="p-4 text-center text-splash-pills-text whitespace-nowrap cursor-pointer bg-prev-goose-gradient dark:bg-dark-prev-goose-gradient text-prev-goose-text dark:text-prev-goose-text-dark rounded-[14px] inline-block hover:scale-[1.02] transition-all duration-150"
onClick={async () => {
const lastUserMessage = messages.reduceRight((found, m) => found || (m.role === 'user' ? m : null), null);
if (lastUserMessage) {
append({
role: 'user',
content: lastUserMessage.content
});
}
}}>
Retry Last Message
)}
{error && (
<div className="flex flex-col items-center justify-center p-4">
<div className="text-red-700 dark:text-red-300 bg-red-400/50 p-3 rounded-lg mb-2">
{error.message || 'Honk! Goose experienced an error while responding'}
{error.status && (
<span className="ml-2">(Status: {error.status})</span>
)}
</div>
<div
className="p-4 text-center text-splash-pills-text whitespace-nowrap cursor-pointer bg-prev-goose-gradient dark:bg-dark-prev-goose-gradient text-prev-goose-text dark:text-prev-goose-text-dark rounded-[14px] inline-block hover:scale-[1.02] transition-all duration-150"
onClick={async () => {
const lastUserMessage = messages.reduceRight((found, m) => found || (m.role === 'user' ? m : null), null);
if (lastUserMessage) {
append({
role: 'user',
content: lastUserMessage.content
});
}
}}>
Retry Last Message
</div>
</div>
</div>
)}
)}
<div ref={messagesEndRef} style={{ height: '1px' }} />
</div>
<div className="block h-10" />
</ScrollArea>
)}
Expand Down
8 changes: 4 additions & 4 deletions ui/desktop/src/components/LoadingGoose.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import svg5 from '../images/loading-goose/5.svg';
import svg6 from '../images/loading-goose/6.svg';
import svg7 from '../images/loading-goose/7.svg';

const Example = () => {
const LoadingGoose = () => {
const [currentFrame, setCurrentFrame] = useState(0);
const frames = [svg1, svg2, svg3, svg4, svg5, svg6, svg7];
const frameCount = frames.length;

useEffect(() => {
const interval = setInterval(() => {
setCurrentFrame((prev) => (prev + 1) % frameCount);
}, 200); // 200ms for smoother animation, adjust as needed
}, 200); // 200ms for smoother animation

return () => clearInterval(interval);
}, [frameCount]); // Add frameCount as dependency
}, [frameCount]);

return (
<div>
Expand All @@ -27,4 +27,4 @@ const Example = () => {
);
};

export default Example;
export default LoadingGoose;
Loading