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

#30 update Reablocks, fix focus on session change #37

Merged
merged 3 commits into from
Aug 13, 2024
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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inputRef is a ref so it will always be there - so i don't think you need the if(inputRef part.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will it? it could be null but i guess you could simplify with 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
107 changes: 105 additions & 2 deletions 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 @@ -853,7 +854,6 @@ const CustomMessageResponse: FC<any> = ({ response }) => (

const CustomMessageFile: FC<any> = ({ name, type }) => (
<Chip size="small" className="rounded-full border border-gray-700">
<>{console.log('boop', name, type)}</>
{name || type}
</Chip>
);
Expand Down Expand Up @@ -1093,3 +1093,106 @@ export const ImageFiles = () => {
</div>
);
};

export const Working = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats this story?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a "working" version - i made it to test the input stuff, but decided it might be useful to have something that functions even as state variables

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i removed it from the main - we can add it back just needs a better name

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 />
</SessionsList>
<SessionMessagePanel>
<SessionMessagesHeader />
<SessionMessages />
<ChatInput />
</SessionMessagePanel>
</Chat>
</div>
);
};
Loading