forked from vercel/ai-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve sidebar, new chat, and share dialog (vercel#190)
- Loading branch information
1 parent
35e83dc
commit be90a40
Showing
23 changed files
with
598 additions
and
217 deletions.
There are no files selected for viewing
File renamed without changes.
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,17 @@ | ||
import { SidebarDesktop } from '@/components/sidebar-desktop' | ||
|
||
interface ChatLayoutProps { | ||
children: React.ReactNode | ||
} | ||
|
||
export default async function ChatLayout({ children }: ChatLayoutProps) { | ||
return ( | ||
<div className="relative flex h-[calc(100vh_-_theme(spacing.16))] overflow-hidden"> | ||
{/* @ts-ignore */} | ||
<SidebarDesktop /> | ||
<div className="group w-full overflow-auto pl-0 animate-in duration-300 ease-in-out peer-[[data-state=open]]:lg:pl-[250px] peer-[[data-state=open]]:xl:pl-[300px]"> | ||
{children} | ||
</div> | ||
</div> | ||
) | ||
} |
File renamed without changes.
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
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,46 @@ | ||
import * as React from 'react' | ||
|
||
import Link from 'next/link' | ||
|
||
import { cn } from '@/lib/utils' | ||
import { SidebarList } from '@/components/sidebar-list' | ||
import { buttonVariants } from '@/components/ui/button' | ||
import { IconPlus } from '@/components/ui/icons' | ||
|
||
interface ChatHistoryProps { | ||
userId?: string | ||
} | ||
|
||
export async function ChatHistory({ userId }: ChatHistoryProps) { | ||
return ( | ||
<div className="flex flex-col h-full"> | ||
<div className="px-2 my-4"> | ||
<Link | ||
href="/" | ||
className={cn( | ||
buttonVariants({ variant: 'outline' }), | ||
'h-10 w-full justify-start bg-zinc-50 px-4 shadow-none transition-colors hover:bg-zinc-200/40 dark:bg-zinc-900 dark:hover:bg-zinc-300/10' | ||
)} | ||
> | ||
<IconPlus className="-translate-x-2 stroke-2" /> | ||
New Chat | ||
</Link> | ||
</div> | ||
<React.Suspense | ||
fallback={ | ||
<div className="flex flex-col flex-1 px-4 space-y-4 overflow-auto"> | ||
{Array.from({ length: 10 }).map((_, i) => ( | ||
<div | ||
key={i} | ||
className="w-full h-6 rounded-md shrink-0 animate-pulse bg-zinc-200 dark:bg-zinc-800" | ||
/> | ||
))} | ||
</div> | ||
} | ||
> | ||
{/* @ts-ignore */} | ||
<SidebarList userId={userId} /> | ||
</React.Suspense> | ||
</div> | ||
) | ||
} |
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,109 @@ | ||
'use client' | ||
|
||
import * as React from 'react' | ||
import Link from 'next/link' | ||
import { type DialogProps } from '@radix-ui/react-dialog' | ||
import { toast } from 'react-hot-toast' | ||
|
||
import { ServerActionResult, type Chat } from '@/lib/types' | ||
import { cn } from '@/lib/utils' | ||
import { badgeVariants } from '@/components/ui/badge' | ||
import { Button } from '@/components/ui/button' | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle | ||
} from '@/components/ui/dialog' | ||
import { IconSpinner } from '@/components/ui/icons' | ||
import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard' | ||
|
||
interface ChatShareDialogProps extends DialogProps { | ||
chat: Pick<Chat, 'id' | 'title' | 'messages'> | ||
shareChat: (id: string) => ServerActionResult<Chat> | ||
onCopy: () => void | ||
} | ||
|
||
export function ChatShareDialog({ | ||
chat, | ||
shareChat, | ||
onCopy, | ||
...props | ||
}: ChatShareDialogProps) { | ||
const { copyToClipboard } = useCopyToClipboard({ timeout: 1000 }) | ||
const [isSharePending, startShareTransition] = React.useTransition() | ||
|
||
const copyShareLink = React.useCallback( | ||
async (chat: Chat) => { | ||
if (!chat.sharePath) { | ||
return toast.error('Could not copy share link to clipboard') | ||
} | ||
|
||
const url = new URL(window.location.href) | ||
url.pathname = chat.sharePath | ||
copyToClipboard(url.toString()) | ||
onCopy() | ||
toast.success('Share link copied to clipboard', { | ||
style: { | ||
borderRadius: '10px', | ||
background: '#333', | ||
color: '#fff', | ||
fontSize: '14px' | ||
}, | ||
iconTheme: { | ||
primary: 'white', | ||
secondary: 'black' | ||
} | ||
}) | ||
}, | ||
[copyToClipboard, onCopy] | ||
) | ||
|
||
return ( | ||
<Dialog {...props}> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Share link to chat</DialogTitle> | ||
<DialogDescription> | ||
Anyone with the URL will be able to view the shared chat. | ||
</DialogDescription> | ||
</DialogHeader> | ||
<div className="p-4 space-y-1 text-sm border rounded-md"> | ||
<div className="font-medium">{chat.title}</div> | ||
<div className="text-muted-foreground"> | ||
{chat.messages.length} messages | ||
</div> | ||
</div> | ||
<DialogFooter className="items-center"> | ||
<Button | ||
disabled={isSharePending} | ||
onClick={() => { | ||
// @ts-ignore | ||
startShareTransition(async () => { | ||
const result = await shareChat(chat.id) | ||
|
||
if (result && 'error' in result) { | ||
toast.error(result.error) | ||
return | ||
} | ||
|
||
copyShareLink(result) | ||
}) | ||
}} | ||
> | ||
{isSharePending ? ( | ||
<> | ||
<IconSpinner className="mr-2 animate-spin" /> | ||
Copying... | ||
</> | ||
) : ( | ||
<>Copy link</> | ||
)} | ||
</Button> | ||
</DialogFooter> | ||
</DialogContent> | ||
</Dialog> | ||
) | ||
} |
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
Oops, something went wrong.