-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
1,177 additions
and
303 deletions.
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
|
@@ -11,6 +11,6 @@ | |
}, | ||
"aliases": { | ||
"components": "@/components", | ||
"utils": "@/utils" | ||
"utils": "@/lib/utils" | ||
} | ||
} |
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
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,106 @@ | ||
import { Button } from '@/components/ui/button' | ||
import { Form, FormControl, FormDescription, FormField, FormItem, FormMessage } from '@/components/ui/form' | ||
import { useChatStore } from '@/lib/store' | ||
import { cn } from '@/lib/utils' | ||
import { useOmegle } from '@/providers/omegle-provider' | ||
import { zodResolver } from '@hookform/resolvers/zod' | ||
import { useEffect, useRef } from 'react' | ||
import { useForm } from 'react-hook-form' | ||
import { z } from 'zod' | ||
import { Textarea } from '../ui/textarea' | ||
import { useToast } from '../ui/use-toast' | ||
import NameDialog from './name-dialog' | ||
|
||
const formSchema = z.object({ | ||
message: z.string().min(1) | ||
}) | ||
|
||
const Chat = () => { | ||
const ref = useRef<HTMLDivElement>(null) | ||
const { messages, name } = useChatStore() | ||
const { sendMessage, strangerId } = useOmegle() | ||
const { toast } = useToast() | ||
const form = useForm<z.infer<typeof formSchema>>({ | ||
resolver: zodResolver(formSchema), | ||
defaultValues: { | ||
message: '' | ||
} | ||
}) | ||
|
||
useEffect(() => { | ||
// When a new message is received, scroll to the bottom of the chat | ||
ref?.current?.scrollTo(0, ref?.current?.scrollHeight) | ||
}, [messages]) | ||
|
||
const onSubmit = (data: z.infer<typeof formSchema>) => { | ||
if (!strangerId) { | ||
toast({ | ||
title: 'Error', | ||
description: 'You are not connected to a chat.', | ||
variant: 'destructive' | ||
}) | ||
return | ||
} | ||
sendMessage?.(data.message) | ||
form.reset() | ||
} | ||
|
||
return ( | ||
<div className="flex-grow flex flex-col h-full w-full max-h-full"> | ||
<div className="flex gap-2"> | ||
<span> | ||
You're connected as <b>{name}</b> | ||
</span> | ||
<NameDialog /> | ||
</div> | ||
<h3>Chat log: </h3> | ||
<div className="flex-grow flex flex-col gap-2 overflow-y-auto h-5/6 py-8" ref={ref}> | ||
{messages.map((message, i) => ( | ||
<div key={i} className="flex flex-col gap-2 "> | ||
<span | ||
className={cn('font-bold', { | ||
'text-accent': message.sender === strangerId | ||
})} | ||
> | ||
{message.sender}:{' '} | ||
</span> | ||
<span>{message.message}</span> | ||
</div> | ||
))} | ||
</div> | ||
<div className="flex w-full"> | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)} className=" flex flex-col md:flex-row gap-4 w-full items-start"> | ||
<FormField | ||
control={form.control} | ||
name="message" | ||
render={({ field }) => ( | ||
<FormItem className="flex-grow"> | ||
<FormControl> | ||
<Textarea | ||
placeholder="Type your message here" | ||
{...field} | ||
onKeyDown={(e) => { | ||
if (e.key === 'Enter' && !e.shiftKey) { | ||
e.preventDefault() | ||
form.handleSubmit(onSubmit)() | ||
} | ||
}} | ||
/> | ||
</FormControl> | ||
<FormDescription>This is your public display name.</FormDescription> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<Button type="submit" size={'lg'}> | ||
Send message | ||
</Button> | ||
</form> | ||
</Form> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default Chat |
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,26 @@ | ||
import { Moon, Sun } from 'lucide-react' | ||
|
||
import { Button } from '@/components/ui/button' | ||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu' | ||
import { useTheme } from '@/providers/theme-provider' | ||
|
||
export function ModeToggle() { | ||
const { setTheme } = useTheme() | ||
|
||
return ( | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button variant="outline" size="icon"> | ||
<Sun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" /> | ||
<Moon className="absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" /> | ||
<span className="sr-only">Toggle theme</span> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent align="end"> | ||
<DropdownMenuItem onClick={() => setTheme('light')}>Light</DropdownMenuItem> | ||
<DropdownMenuItem onClick={() => setTheme('dark')}>Dark</DropdownMenuItem> | ||
<DropdownMenuItem onClick={() => setTheme('system')}>System</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
) | ||
} |
Oops, something went wrong.