Skip to content

Commit

Permalink
simplify SessionGroups and SessionMessages for basic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
steppy452 committed Aug 10, 2024
1 parent 27b9076 commit 61bef21
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 412 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

49 changes: 26 additions & 23 deletions src/SessionMessages/SessionMessage/MessageQuestion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,40 @@ export interface MessageQuestionProps extends PropsWithChildren {
}

export const MessageQuestion: FC<MessageQuestionProps> = ({
question,
files,
children
children,
...props
}) => {
const { theme, remarkPlugins } =
useContext(ChatContext);
const { theme, remarkPlugins } = useContext(ChatContext);
const { question, files } = props;
const Comp = children ? Slot : 'div';
const [expanded, setExpanded] = useState(false);
const isLong = question.length > 500;

return (
<Comp className={cn(theme.messages.message.question, {
[theme.messages.message.overlay]: isLong && !expanded
})}>
<MessageFiles files={files} />
<Comp
className={cn(theme.messages.message.question, {
[theme.messages.message.overlay]: isLong && !expanded
})}
{...props}
>
{children || (
<Markdown remarkPlugins={remarkPlugins as PluggableList[]}>
{question}
</Markdown>
)}
{isLong && !expanded && (
<Button
variant="link"
size="small"
className={theme.messages.message.expand}
onClick={() => setExpanded(true)}
>
Show more
</Button>
<>
<MessageFiles files={files} />
<Markdown remarkPlugins={remarkPlugins as PluggableList[]}>
{question}
</Markdown>
{isLong && !expanded && (
<Button
variant="link"
size="small"
className={theme.messages.message.expand}
onClick={() => setExpanded(true)}
>
Show more
</Button>
)}
</>
)}
</Comp>
);
};

11 changes: 10 additions & 1 deletion src/SessionMessages/SessionMessages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ChatContext } from '@/ChatContext';
import { Button, cn, useInfinityList } from 'reablocks';
import { AnimatePresence, motion } from 'framer-motion';
import { Conversation } from '@/types';
import { SessionMessage } from './SessionMessage/SessionMessage';

const containerVariants = {
hidden: {},
Expand Down Expand Up @@ -113,7 +114,15 @@ export const SessionMessages: React.FC<SessionMessagesProps> = ({
requestAnimationFrame(() => setIsAnimating(false));
}}
>
{children(convosToRender)}
{children
? children(convosToRender)
: convosToRender.map((conversation, index) => (
<SessionMessage
key={conversation.id}
conversation={conversation}
isLast={index === conversation.length - 1}
/>
))}
</motion.div>
</AnimatePresence>
</div>
Expand Down
18 changes: 16 additions & 2 deletions src/SessionsList/SessionGroups.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import { FC, ReactNode, useContext, useMemo } from 'react';
import { GroupedSessions, groupSessionsByDate } from '@/utils/grouping';
import { ChatContext } from '@/ChatContext';
import { SessionsGroup } from './SessionsGroup';
import { SessionListItem } from './SessionListItem';

export interface SessionGroupsProps {
/**
* Render function for the session groups.
*/
children: (groups: GroupedSessions[]) => ReactNode;
children?: (groups: GroupedSessions[]) => ReactNode;
}

export const SessionGroups: FC<SessionGroupsProps> = ({ children }) => {
const { sessions } = useContext(ChatContext);
const groups = useMemo(() => groupSessionsByDate(sessions), [sessions]);

return <>{children(groups)}</>;
return (
<>
{children
? children(groups)
: groups.map(({ heading, sessions }) => (
<SessionsGroup heading={heading}>
{sessions.map(session => (
<SessionListItem key={session.id} session={session} />
))}
</SessionsGroup>
))}
</>
);
};
24 changes: 2 additions & 22 deletions stories/Chat.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,7 @@ export const Compact = () => {
onDeleteSession={() => alert('delete!')}
>
<SessionMessagePanel>
<SessionMessages>
{conversations =>
conversations.map((conversation, index) => (
<SessionMessage
key={conversation.id}
conversation={conversation}
isLast={index === conversations.length - 1}
/>
))
}
</SessionMessages>
<SessionMessages />
<ChatInput />
</SessionMessagePanel>
</Chat>
Expand Down Expand Up @@ -129,17 +119,7 @@ export const FullScreen = () => {
onDeleteSession={() => alert('delete!')}
>
<SessionMessagePanel>
<SessionMessages>
{conversations =>
conversations.map((conversation, index) => (
<SessionMessage
key={conversation.id}
conversation={conversation}
isLast={index === conversations.length - 1}
/>
))
}
</SessionMessages>
<SessionMessages />
<ChatInput />
</SessionMessagePanel>
</Chat>
Expand Down
36 changes: 3 additions & 33 deletions stories/Companion.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,11 @@ export const Basic = () => {
>
<SessionsList>
<NewSessionButton />
<SessionGroups>
{groups =>
groups.map(({ heading, sessions }) => (
<SessionsGroup heading={heading} key={heading}>
{sessions.map(s => (
<SessionListItem key={s.id} session={s} />
))}
</SessionsGroup>
))
}
</SessionGroups>
<SessionGroups />
</SessionsList>
<SessionMessagePanel>
<SessionMessagesHeader />
<SessionMessages>
{conversations =>
conversations.map((conversation, index) => (
<SessionMessage
key={conversation.id}
conversation={conversation}
isLast={index === conversations.length - 1}
/>
))
}
</SessionMessages>
<SessionMessages />
<ChatInput />
</SessionMessagePanel>
</Chat>
Expand All @@ -118,17 +98,7 @@ export const Empty = () => {
>
<SessionsList>
<NewSessionButton />
<SessionGroups>
{groups =>
groups.map(({ heading, sessions }) => (
<SessionsGroup heading={heading} key={heading}>
{sessions.map(s => (
<SessionListItem key={s.id} session={s} />
))}
</SessionsGroup>
))
}
</SessionGroups>
<SessionGroups />
</SessionsList>
<div className="flex-1 h-full flex flex-col">
<SessionMessages
Expand Down
Loading

0 comments on commit 61bef21

Please sign in to comment.