Skip to content

Commit

Permalink
update Reablocks, fix focus on session change
Browse files Browse the repository at this point in the history
  • Loading branch information
steppy452 committed Aug 13, 2024
1 parent b3662c8 commit 1847d5b
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 12 deletions.
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"framer-motion": "^10.16.16",
"katex": "^0.16.11",
"mdast-util-find-and-replace": "^3.0.1",
"reablocks": "^8.4.0",
"reablocks": "^8.4.7",
"react-markdown": "^9.0.1",
"react-syntax-highlighter": "^15.5.0",
"reakeys": "^2.0.3",
Expand Down
14 changes: 10 additions & 4 deletions src/ChatInput/ChatInput.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
FC,
useState,
KeyboardEvent,
ReactElement,
useRef,
ChangeEvent,
useContext,
forwardRef,
useImperativeHandle
useImperativeHandle,
useEffect
} from 'react';
import { Button, Textarea, cn } from 'reablocks';
import SendIcon from '@/assets/send.svg?react';
Expand Down Expand Up @@ -62,11 +62,17 @@ export const ChatInput = forwardRef<ChatInputRef, ChatInputProps>(({
stopIcon = <StopIcon />,
attachIcon
}, ref) => {
const { theme, isLoading, disabled, sendMessage, stopMessage, fileUpload } =
const { theme, isLoading, disabled, sendMessage, stopMessage, fileUpload, activeSessionId } =
useContext(ChatContext);
const [message, setMessage] = useState<string>('');
const inputRef = useRef<HTMLTextAreaElement | null>(null);

useEffect(() => {
if(inputRef && inputRef.current) {
inputRef.current.focus()
}
}, [activeSessionId, inputRef])

useImperativeHandle(ref, () => ({
focus: () => {
inputRef.current?.focus();
Expand Down Expand Up @@ -97,7 +103,7 @@ export const ChatInput = forwardRef<ChatInputRef, ChatInputProps>(({
return (
<div className={cn(theme.input.base)}>
<Textarea
inputRef={inputRef}
ref={inputRef}
containerClassName={cn(theme.input.input)}
minRows={1}
autoFocus
Expand Down
125 changes: 124 additions & 1 deletion stories/Console.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
SessionMessagePanel,
SessionMessagesHeader,
ChatContext,
SessionMessage
SessionMessage,
Conversation
} from '../src';
import {
Card,
Expand Down Expand Up @@ -1093,3 +1094,125 @@ export const ImageFiles = () => {
</div>
);
};

export const Working = () => {
const [activeId, setActiveId] = useState<string>();
const [sessions, setSessions] = useState<Session[]>([]);
const [loading, setLoading] = useState<boolean>(false);
const [count, setCount] = useState<number>(sessions.length + 1);

const handleNewSession = () => {
const newId = count.toLocaleString();
setSessions([
...sessions,
{
id: newId,
title: `New Session #${newId}`,
createdAt: new Date(),
updatedAt: new Date(),
conversations: []
}
]);
setActiveId(newId);
setCount(count + 1);
};

const handleDelete = (id: string) => {
const updated = sessions.filter(s => s.id !== id);
setSessions([...updated]);
};

const handleNewMessage = (message: string) => {
setLoading(true);
const curr = sessions.find(s => s.id === activeId);
if (curr) {
const newMessage: Conversation = {
id: `${curr.id}-${curr.conversations.length}`,
question: message,
response: 'this is an example response',
createdAt: new Date(),
updatedAt: new Date()
};
const updated = {
...curr,
conversations: [...curr.conversations, newMessage]
};
setSessions([...sessions.filter(s => s.id !== activeId), updated]);
} else {
const newId = '1';
const newSession: Session = {
id: newId,
title: message.length > 28 ? `${message.substring(0, 25)}...` : message,
createdAt: new Date(),
updatedAt: new Date(),
conversations: [
{
id: `${newId}-1`,
question: message,
response: 'this is an example response',
createdAt: new Date(),
updatedAt: new Date()
}
]
};
setSessions([newSession]);
setActiveId(newId);
}
setLoading(false);
};

return (
<div
className="dark:bg-gray-950 bg-white"
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
padding: 20,
margin: 20,
borderRadius: 5
}}
>
<Chat
sessions={sessions}
activeSessionId={activeId}
isLoading={loading}
onNewSession={handleNewSession}
onSelectSession={setActiveId}
onDeleteSession={handleDelete}
onSendMessage={handleNewMessage}
>
<SessionsList>
<NewSessionButton />
<SessionGroups>
{groups =>
groups.map(({ heading, sessions }) => (
<SessionsGroup heading={heading} key={heading}>
{sessions.map(s => (
<SessionListItem key={s.id} session={s} />
))}
</SessionsGroup>
))
}
</SessionGroups>
</SessionsList>
<SessionMessagePanel>
<SessionMessagesHeader />
<SessionMessages>
{conversations =>
conversations.map(conversation => (
<SessionMessage
key={conversation.id}
conversation={conversation}
/>
))
}
</SessionMessages>
<ChatInput />
</SessionMessagePanel>
</Chat>
</div>
);
};

0 comments on commit 1847d5b

Please sign in to comment.