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

feat: Better autoscroll - for #909 #1054

Merged
merged 1 commit into from
Feb 3, 2025
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
35 changes: 6 additions & 29 deletions ui/desktop/src/ChatWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import LoadingGoose from './components/LoadingGoose';
import MoreMenu from './components/MoreMenu';
import { Card } from './components/ui/card';
import { ScrollArea } from './components/ui/scroll-area';
import { ScrollArea, ScrollAreaHandle } from './components/ui/scroll-area';
import UserMessage from './components/UserMessage';
import WingToWing, { Working } from './components/WingToWing';

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

View workflow job for this annotation

GitHub Actions / Lint Electron Desktop App

'WingToWing' is defined but never used. Allowed unused vars must match /^_/u
import { askAi } from './utils/askAI';
import { getStoredModel, Provider } from './utils/providerUtils';
import { ChatLayout } from './components/chat_window/ChatLayout';
Expand All @@ -22,7 +22,6 @@
import { createSelectedModel } from './components/settings/models/utils';
import { getDefaultModel } from './components/settings/models/hardcoded_stuff';
import Splash from './components/Splash';
import { loadAndAddStoredExtensions } from './extensions';

declare global {
interface Window {
Expand All @@ -35,10 +34,10 @@
logInfo: (message: string) => void;
showNotification: (opts: { title: string; body: string }) => void;
getBinaryPath: (binary: string) => Promise<string>;
app: any;

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

View workflow job for this annotation

GitHub Actions / Lint Electron Desktop App

Unexpected any. Specify a different type
};
appConfig: {
get: (key: string) => any;

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

View workflow job for this annotation

GitHub Actions / Lint Electron Desktop App

Unexpected any. Specify a different type
};
}
}
Expand All @@ -53,13 +52,10 @@
}>;
}

type ScrollBehavior = 'auto' | 'smooth' | 'instant';

export function ChatContent({
chats,
setChats,
selectedChatId,
setSelectedChatId,
initialQuery,
setProgressMessage,
setWorking,
Expand All @@ -77,8 +73,8 @@
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);
const scrollRef = useRef<ScrollAreaHandle>(null);

useEffect(() => {
setWorking(working);
Expand All @@ -94,7 +90,6 @@
onToolCall: ({ toolCall }) => {
updateWorking(Working.Working);
setProgressMessage(`Executing tool: ${toolCall.toolName}`);
requestAnimationFrame(() => scrollToBottom('instant'));
},
onResponse: (response) => {
if (!response.ok) {
Expand All @@ -115,8 +110,6 @@
const fetchResponses = await askAi(message.content);
setMessageMetadata((prev) => ({ ...prev, [message.id]: fetchResponses }));

requestAnimationFrame(() => scrollToBottom('smooth'));

const timeSinceLastInteraction = Date.now() - lastInteractionTime;
window.electron.logInfo('last interaction:' + lastInteractionTime);
if (timeSinceLastInteraction > 60000) {
Expand All @@ -134,7 +127,7 @@
useEffect(() => {
const updatedChats = chats.map((c) => (c.id === selectedChatId ? { ...c, messages } : c));
setChats(updatedChats);
}, [messages, selectedChatId]);

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

View workflow job for this annotation

GitHub Actions / Lint Electron Desktop App

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 @@ -142,7 +135,7 @@
append({ role: 'user', content: initialQuery });
initialQueryAppended.current = true;
}
}, [initialQuery]);

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

View workflow job for this annotation

GitHub Actions / Lint Electron Desktop App

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

useEffect(() => {
if (messages.length > 0) {
Expand All @@ -150,23 +143,6 @@
}
}, [messages]);

const scrollToBottom = (behavior: ScrollBehavior = 'smooth') => {
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) {
scrollToBottom(isLoading || working === Working.Working ? 'instant' : 'smooth');
}
}, [messages, isLoading, working]);

// Handle submit
const handleSubmit = (e: React.FormEvent) => {
window.electron.startPowerSaveBlocker();
Expand All @@ -178,7 +154,9 @@
role: 'user',
content: content,
});
scrollToBottom('instant');
if (scrollRef.current?.scrollToBottom) {
scrollRef.current.scrollToBottom();
}
}
};

Expand Down Expand Up @@ -241,7 +219,7 @@
{messages.length === 0 ? (
<Splash append={append} />
) : (
<ScrollArea className="flex-1 px-4" id="chat-scroll-area">
<ScrollArea ref={scrollRef} className="flex-1 px-4" autoScroll>
{messages.map((message) => (
<div key={message.id} className="mt-[16px]">
{message.role === 'user' ? (
Expand Down Expand Up @@ -288,7 +266,6 @@
</div>
)}
<div className="block h-16" />
<div ref={messagesEndRef} style={{ height: '1px' }} />
</ScrollArea>
)}

Expand All @@ -314,7 +291,7 @@
const openNewChatWindow = () => {
window.electron.createChatWindow();
};
const { switchModel, currentModel } = useModel(); // Access switchModel via useModel

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

View workflow job for this annotation

GitHub Actions / Lint Electron Desktop App

'currentModel' is assigned a value but never used. Allowed unused vars must match /^_/u
const { addRecentModel } = useRecentModels(); // Access addRecentModel from useRecentModels

// Add keyboard shortcut handler
Expand Down Expand Up @@ -353,9 +330,9 @@

const [selectedChatId, setSelectedChatId] = useState(1);
const [mode, setMode] = useState<'expanded' | 'compact'>(initialQuery ? 'compact' : 'expanded');
const [working, setWorking] = useState<Working>(Working.Idle);

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

View workflow job for this annotation

GitHub Actions / Lint Electron Desktop App

'working' is assigned a value but never used. Allowed unused vars must match /^_/u
const [progressMessage, setProgressMessage] = useState<string>('');

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

View workflow job for this annotation

GitHub Actions / Lint Electron Desktop App

'progressMessage' is assigned a value but never used. Allowed unused vars must match /^_/u
const [selectedProvider, setSelectedProvider] = useState<string | Provider | null>(null);

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

View workflow job for this annotation

GitHub Actions / Lint Electron Desktop App

'selectedProvider' is assigned a value but never used. Allowed unused vars must match /^_/u
const [showWelcomeModal, setShowWelcomeModal] = useState(true);

// Add this useEffect to track changes and update welcome state
Expand Down
2 changes: 1 addition & 1 deletion ui/desktop/src/components/LoadingGoose.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import GooseLogo from './GooseLogo';
const LoadingGoose = () => {
return (
<div className="w-full pb-[2px]">
<div className="flex items-center text-xs text-textStandard mb-2 pl-4 animate-[appear_250ms_ease-in_forwards]">
<div className="flex items-center text-xs text-textStandard mb-2 mt-2 pl-4 animate-[appear_250ms_ease-in_forwards]">
<GooseLogo className="mr-2" size="small" hover={false} />
goose is working on it..
</div>
Expand Down
96 changes: 80 additions & 16 deletions ui/desktop/src/components/ui/scroll-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,86 @@ import * as ScrollAreaPrimitive from '@radix-ui/react-scroll-area';

import { cn } from '../../utils';

const ScrollArea = React.forwardRef<
React.ElementRef<typeof ScrollAreaPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root>
>(({ className, children, ...props }, ref) => (
<ScrollAreaPrimitive.Root
ref={ref}
className={cn('relative overflow-hidden', className)}
{...props}
>
<ScrollAreaPrimitive.Viewport className="h-full w-full rounded-[inherit]">
{children}
</ScrollAreaPrimitive.Viewport>
<ScrollBar />
<ScrollAreaPrimitive.Corner />
</ScrollAreaPrimitive.Root>
));
export interface ScrollAreaHandle {
scrollToBottom: () => void;
}

interface ScrollAreaProps extends React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root> {
autoScroll?: boolean;
}

const ScrollArea = React.forwardRef<ScrollAreaHandle, ScrollAreaProps>(
({ className, children, autoScroll = false, ...props }, ref) => {
const rootRef = React.useRef<React.ElementRef<typeof ScrollAreaPrimitive.Root>>(null);
const viewportRef = React.useRef<HTMLDivElement>(null);
const viewportEndRef = React.useRef<HTMLDivElement>(null);
const [isFollowing, setIsFollowing] = React.useState(true);

const scrollToBottom = React.useCallback(() => {
if (viewportEndRef.current) {
viewportEndRef.current.scrollIntoView({
behavior: 'smooth',
block: 'end',
inline: 'nearest',
});
setIsFollowing(true);
}
}, []);

// Expose the scrollToBottom method to parent components
React.useImperativeHandle(
ref,
() => ({
scrollToBottom,
}),
[scrollToBottom]
);

// Handle scroll events to update isFollowing state
const handleScroll = React.useCallback(() => {
if (!viewportRef.current) return;

const viewport = viewportRef.current;
const { scrollHeight, scrollTop, clientHeight } = viewport;

const scrollBottom = scrollTop + clientHeight;
const newIsFollowing = scrollHeight === scrollBottom;

// react will internally optimize this to not re-store the same values
setIsFollowing(newIsFollowing);
}, []);

React.useEffect(() => {
if (!autoScroll || !isFollowing) return;

scrollToBottom();
}, [children, autoScroll, isFollowing, scrollToBottom]);

// Add scroll event listener
React.useEffect(() => {
const viewport = viewportRef.current;
if (!viewport) return;

viewport.addEventListener('scroll', handleScroll);
return () => viewport.removeEventListener('scroll', handleScroll);
}, [handleScroll]);

return (
<ScrollAreaPrimitive.Root
ref={rootRef}
className={cn('relative overflow-hidden', className)}
{...props}
>
<ScrollAreaPrimitive.Viewport ref={viewportRef} className="h-full w-full rounded-[inherit]">
{children}
{autoScroll && <div ref={viewportEndRef} style={{ height: '1px' }} />}
</ScrollAreaPrimitive.Viewport>
<ScrollBar />
<ScrollAreaPrimitive.Corner />
</ScrollAreaPrimitive.Root>
);
}
);
ScrollArea.displayName = ScrollAreaPrimitive.Root.displayName;

const ScrollBar = React.forwardRef<
Expand Down
Loading