Open History to see the last commit
1import type { ComponentProps } from 'react'
2import { useCallback } from 'react'
3import { ArrowDownIcon } from 'lucide-react'
4import { StickToBottom, useStickToBottomContext } from 'use-stick-to-bottom'
5import { Button } from '@/components/ui/button'
6import cn from '@/utils/cn'
7
8export type ConversationProps = ComponentProps<typeof StickToBottom>
9
10export function Conversation({ className, ...props }: ConversationProps) {
11 return (
12 <StickToBottom
13 className={cn('relative flex-1 overflow-y-hidden', className)}
14 initial="smooth"
15 resize="smooth"
16 role="log"
17 {...props}
18 />
19 )
20}
21
22export type ConversationContentProps = ComponentProps<
23 typeof StickToBottom.Content
24>
25
26export function ConversationContent({
27 className,
28 ...props
29}: ConversationContentProps) {
30 return (
31 <StickToBottom.Content
32 className={cn('flex flex-col gap-8 p-4', className)}
33 {...props}
34 />
35 )
36}
37
38export type ConversationEmptyStateProps = ComponentProps<'div'> & {
39 title?: string
40 description?: string
41 icon?: React.ReactNode
42}
43
44export function ConversationEmptyState({
45 className,
46 title = 'No messages yet',
47 description = 'Start a conversation to see messages here',
48 icon,
49 children,
50 ...props
51}: ConversationEmptyStateProps) {
52 return (
53 <div
54 className={cn(
55 'flex size-full flex-col items-center justify-center gap-3 p-8 text-center',
56 className,
57 )}
58 {...props}
59 >
60 {children ?? (
61 <>
62 {icon ? <div className="text-muted-foreground">{icon}</div> : null}
63 <div className="space-y-1">
64 <h3 className="font-medium text-sm">{title}</h3>
65 {description ? (
66 <p className="text-muted-foreground text-sm">{description}</p>
67 ) : null}
68 </div>
69 </>
70 )}
71 </div>
72 )
73}
74
75export type ConversationScrollButtonProps = ComponentProps<typeof Button>
76
77export function ConversationScrollButton({
78 className,
79 ...props
80}: ConversationScrollButtonProps) {
81 const { isAtBottom, scrollToBottom } = useStickToBottomContext()
82 const handleScrollToBottom = useCallback(() => {
83 scrollToBottom()
84 }, [scrollToBottom])
85
86 if (isAtBottom) return null
87
88 return (
89 <Button
90 className={cn(
91 'absolute bottom-4 left-[50%] translate-x-[-50%] rounded-full',
92 className,
93 )}
94 onClick={handleScrollToBottom}
95 size="icon"
96 type="button"
97 variant="outline"
98 {...props}
99 >
100 <ArrowDownIcon className="size-4" />
101 </Button>
102 )
103}