-
Notifications
You must be signed in to change notification settings - Fork 20
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 #40 from joaoreider/simple-chat
feat(web): add a text chat using the existing connection
- Loading branch information
Showing
9 changed files
with
314 additions
and
72 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use client' | ||
|
||
import React, { useRef, useState } from 'react' | ||
import { MessageCircle } from 'lucide-react' | ||
import { Button } from '@/components/ui/button' | ||
import { Input } from '@/components/ui/input' | ||
import { ScrollArea } from '@/components/ui/scroll-area' | ||
import { MessageItem } from './message-item' | ||
|
||
export type Message = { | ||
sender: 'me' | 'other' | ||
content: string | ||
} | ||
|
||
type Chat = { | ||
messages: Message[] | ||
onSend: (message: string) => void | ||
} | ||
|
||
export const Chat: React.FC<Chat> = ({ messages, onSend }) => { | ||
const [inputValue, setInputValue] = useState('') | ||
|
||
const sendMessage = () => { | ||
const message = inputValue.trim() | ||
if (message) { | ||
onSend(message) | ||
setInputValue('') | ||
} | ||
} | ||
|
||
const handleKeyPress = (event: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (event.key === 'Enter' && !event.shiftKey) { | ||
event.preventDefault() | ||
sendMessage() | ||
} | ||
} | ||
|
||
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setInputValue(event.target.value) | ||
} | ||
|
||
return ( | ||
<div className="align-center flex h-full w-full flex-col items-center justify-center gap-2"> | ||
<ScrollArea className="flex w-full flex-1 items-center gap-2 rounded-lg border p-4"> | ||
{!messages.length ? ( | ||
<div className="mt-6 flex flex-col items-center "> | ||
<MessageCircle className="text-muted" /> | ||
<span className="italic text-muted">No messages yet</span> | ||
</div> | ||
) : ( | ||
messages.map((message, index) => ( | ||
<div key={index} className="w-full"> | ||
<MessageItem message={message} /> | ||
</div> | ||
)) | ||
)} | ||
</ScrollArea> | ||
<div className="flex w-full flex-row gap-2 rounded-lg align-baseline"> | ||
<Input | ||
className="flex-shrink" | ||
placeholder="Type a message..." | ||
value={inputValue} | ||
onKeyDown={handleKeyPress} | ||
onChange={handleInputChange} | ||
/> | ||
<Button onClick={sendMessage}>Send</Button> | ||
</div> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import clsx from 'clsx' | ||
import { Message } from './chat' | ||
|
||
type MessageItem = { | ||
message: Message | ||
} | ||
|
||
export const MessageItem: React.FC<MessageItem> = ({ message }) => { | ||
const isMe = message.sender === 'me' | ||
|
||
return ( | ||
<div | ||
className={clsx( | ||
'flex items-center gap-3', | ||
isMe ? 'justify-end' : 'justify-start' | ||
)} | ||
> | ||
<span | ||
className={clsx( | ||
'm-1 max-w-xs rounded-md p-3', | ||
isMe ? 'bg-accent' : 'bg-purple-950' | ||
)} | ||
> | ||
<span className="text-sm">{message.content}</span> | ||
</span> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Layout({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}): JSX.Element { | ||
return <div className="h-content">{children}</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,47 @@ | ||
'use client' | ||
|
||
import * as React from 'react' | ||
import { cn } from '@/lib/utils' | ||
import * as ScrollAreaPrimitive from '@radix-ui/react-scroll-area' | ||
|
||
const ScrollArea = React.forwardRef< | ||
React.ElementRef<typeof ScrollAreaPrimitive.Root>, | ||
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root> | ||
>(({ className, children, ...props }, ref) => ( | ||
<ScrollAreaPrimitive.Root | ||
ref={ref} | ||
className={cn('relative overflow-hidden', className)} | ||
{...props} | ||
> | ||
<ScrollAreaPrimitive.Viewport className="h-full w-full rounded-[inherit]"> | ||
{children} | ||
</ScrollAreaPrimitive.Viewport> | ||
<ScrollBar /> | ||
<ScrollAreaPrimitive.Corner /> | ||
</ScrollAreaPrimitive.Root> | ||
)) | ||
ScrollArea.displayName = ScrollAreaPrimitive.Root.displayName | ||
|
||
const ScrollBar = React.forwardRef< | ||
React.ElementRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>, | ||
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar> | ||
>(({ className, orientation = 'vertical', ...props }, ref) => ( | ||
<ScrollAreaPrimitive.ScrollAreaScrollbar | ||
ref={ref} | ||
orientation={orientation} | ||
className={cn( | ||
'flex touch-none select-none transition-colors', | ||
orientation === 'vertical' && | ||
'h-full w-2.5 border-l border-l-transparent p-[1px]', | ||
orientation === 'horizontal' && | ||
'h-2.5 flex-col border-t border-t-transparent p-[1px]', | ||
className | ||
)} | ||
{...props} | ||
> | ||
<ScrollAreaPrimitive.ScrollAreaThumb className="relative flex-1 rounded-full bg-border" /> | ||
</ScrollAreaPrimitive.ScrollAreaScrollbar> | ||
)) | ||
ScrollBar.displayName = ScrollAreaPrimitive.ScrollAreaScrollbar.displayName | ||
|
||
export { ScrollArea, ScrollBar } |
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
Oops, something went wrong.