-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from miurla/collapsible
Add collapsible feature
- Loading branch information
Showing
9 changed files
with
111 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,25 @@ | ||
import { useUIState } from 'ai/rsc' | ||
import { StreamableValue, useUIState } from 'ai/rsc' | ||
import type { AI } from '@/app/action' | ||
import { CollapsibleMessage } from './collapsible-message' | ||
|
||
export function ChatMessages() { | ||
const [messages, setMessages] = useUIState<typeof AI>() | ||
|
||
return ( | ||
<> | ||
{messages.map((message: { id: number; component: React.ReactNode }) => ( | ||
<div key={message.id}>{message.component}</div> | ||
))} | ||
{messages.map( | ||
(message: { | ||
id: number | ||
component: React.ReactNode | ||
isCollapsed?: StreamableValue<boolean> | ||
}) => ( | ||
<CollapsibleMessage | ||
key={message.id} | ||
message={message} | ||
isLastMessage={message.id === messages[messages.length - 1].id} | ||
/> | ||
) | ||
)} | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import { | ||
Collapsible, | ||
CollapsibleTrigger, | ||
CollapsibleContent | ||
} from '@radix-ui/react-collapsible' | ||
import { Button } from './ui/button' | ||
import { ChevronDown } from 'lucide-react' | ||
import { StreamableValue, useStreamableValue } from 'ai/rsc' | ||
import { cn } from '@/lib/utils' | ||
import { Separator } from './ui/separator' | ||
|
||
interface CollapsibleMessageProps { | ||
message: { | ||
id: number | ||
isCollapsed?: StreamableValue<boolean> | ||
component: React.ReactNode | ||
} | ||
isLastMessage?: boolean | ||
} | ||
|
||
export const CollapsibleMessage: React.FC<CollapsibleMessageProps> = ({ | ||
message, | ||
isLastMessage = false | ||
}) => { | ||
const [data] = useStreamableValue(message.isCollapsed) | ||
const isCollapsed = data ?? false | ||
const [open, setOpen] = useState(isLastMessage) | ||
|
||
useEffect(() => { | ||
setOpen(isLastMessage) | ||
}, [isCollapsed, isLastMessage]) | ||
|
||
// if not collapsed, return the component | ||
if (!isCollapsed) { | ||
return message.component | ||
} | ||
|
||
return ( | ||
<Collapsible | ||
open={open} | ||
onOpenChange={value => { | ||
setOpen(value) | ||
}} | ||
> | ||
<CollapsibleTrigger asChild> | ||
<div | ||
className={cn( | ||
'w-full flex justify-end', | ||
!isCollapsed ? 'hidden' : '' | ||
)} | ||
> | ||
<Button | ||
variant="ghost" | ||
size={'icon'} | ||
className={cn('-mt-3 rounded-full')} | ||
> | ||
<ChevronDown | ||
className={cn( | ||
open ? 'rotate-180' : 'rotate-0', | ||
'h-4 w-4 transition-all' | ||
)} | ||
/> | ||
<span className="sr-only">collapse</span> | ||
</Button> | ||
</div> | ||
</CollapsibleTrigger> | ||
<CollapsibleContent>{message.component}</CollapsibleContent> | ||
{!open && <Separator className="my-2 bg-muted" />} | ||
</Collapsible> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use client" | ||
|
||
import * as CollapsiblePrimitive from "@radix-ui/react-collapsible" | ||
|
||
const Collapsible = CollapsiblePrimitive.Root | ||
|
||
const CollapsibleTrigger = CollapsiblePrimitive.CollapsibleTrigger | ||
|
||
const CollapsibleContent = CollapsiblePrimitive.CollapsibleContent | ||
|
||
export { Collapsible, CollapsibleTrigger, CollapsibleContent } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters