Skip to content

Commit

Permalink
feat: thread top down button
Browse files Browse the repository at this point in the history
  • Loading branch information
mikbry authored Feb 14, 2024
2 parents 2400489 + f38ae46 commit 5ed28d3
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 138 deletions.
37 changes: 16 additions & 21 deletions webapp/components/threads/Conversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { useEffect, useRef } from 'react';
import { ArrowDown } from 'lucide-react';
import useScroll, { KeyedScrollPosition } from '@/hooks/useScroll';
import { Message } from '@/types';
import logger from '@/utils/logger';
import MessageView from './Message';
import { Button } from '../ui/button';

type ConversationProps = {
conversationId: string;
Expand All @@ -37,29 +38,13 @@ function Conversation({
handleShouldDeleteMessage,
handleChangeMessageContent,
}: ConversationProps) {
const bottomOfChatRef = useRef<HTMLDivElement>(null);
// const [updatedScrollPosition, setUpdatedScrollPosition] = useState(scrollPosition);

const handleUpdatePosition = (props: KeyedScrollPosition) => {
// if (position.y === newPosition.y) return;
logger.info('updated newPosition', props);
// onScrollPosition(newPosition.y);
onScrollPosition(props);
};

// const ref = useRef<HTMLDivElement | undefined>(undefined);
const [ref, scrollTo] = useScroll(
conversationId,
{ x: scrollPosition === -1 ? -1 : 0, y: scrollPosition },
handleUpdatePosition,
);
// logger.info(`render Conversation ${messages.length}`, scrollPosition, ref);

useEffect(() => {
scrollTo({ x: 0, y: scrollPosition });
}, [scrollPosition, scrollTo]);

// useDebounceFunc<number>(onScrollPosition, updatedScrollPosition, 500);
const position = { x: scrollPosition === -1 ? -1 : 0, y: scrollPosition };
const [ref, scrollTo] = useScroll(conversationId, position, handleUpdatePosition);

return (
<div className="flex grow flex-col overflow-hidden">
Expand All @@ -83,8 +68,18 @@ function Conversation({
</div>
</div>

<div className="h-4 w-full" />
<div ref={bottomOfChatRef} />
<div className="z-100 relative w-full">
{scrollPosition < 99 && scrollPosition > 0 && (
<Button
variant="ghost"
size="icon"
className="absolute bottom-4 right-8"
onClick={() => scrollTo({ x: 0, y: 100 })}
>
<ArrowDown />
</Button>
)}
</div>
</div>
);
}
Expand Down
20 changes: 8 additions & 12 deletions webapp/components/threads/Thread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,6 @@ function Thread({
handleUpdatePrompt(prompt.value, prompt.name);
};

/* const [updatedScrollPosition, setUpdatedScrollPosition] = useState<KeyedScrollPosition>({
key: undefined,
position: EmptyPosition,
}); */

const handleScrollPosition = ({ key, position }: KeyedScrollPosition) => {
const conversation = getConversation(key, conversations);
if (conversation && conversation.scrollPosition !== position.y && position.y !== -1) {
Expand All @@ -472,8 +467,6 @@ function Thread({
}
};

// useDebounceFunc<KeyedScrollPosition>(handleScrollPosition, updatedScrollPosition, 500);

logger.info(
`render Thread ${conversationId}`,
selectedConversation,
Expand Down Expand Up @@ -535,12 +528,15 @@ function Thread({
<PromptsGrid onPromptSelected={handlePromptSelected} />
</div>
) : (
messages &&
messages[0]?.conversationId === conversationId && (
(message || (messages && messages[0]?.conversationId === conversationId)) && (
<ConversationView
conversationId={selectedConversation?.id as string}
scrollPosition={selectedConversation?.scrollPosition || -1}
messages={messages}
scrollPosition={
selectedConversation && selectedConversation.scrollPosition !== undefined
? selectedConversation.scrollPosition
: -1
}
messages={messages || []}
onScrollPosition={handleScrollPosition}
handleResendMessage={handleResendMessage}
handleShouldDeleteMessage={handleShouldDeleteMessage}
Expand All @@ -550,7 +546,7 @@ function Thread({
)}
<div className="flex flex-col items-center text-sm dark:bg-neutral-800/30" />

{messages && messages[0]?.conversationId === conversationId && (
{(message || (messages && messages[0]?.conversationId === conversationId)) && (
<PromptArea
conversationId={conversationId as string}
message={message}
Expand Down
Loading

0 comments on commit 5ed28d3

Please sign in to comment.