-
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.
Merge pull request #7 from YubaNeupane/dev
Added socket io and channel chatting ui
- Loading branch information
Showing
13 changed files
with
594 additions
and
7 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
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,81 @@ | ||
"use client"; | ||
import { useForm } from "react-hook-form"; | ||
import * as z from "zod"; | ||
import axios from "axios"; | ||
import qs from "query-string"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
|
||
import { Form, FormControl, FormField, FormItem } from "@/components/ui/form"; | ||
import { Input } from "@/components/ui/input"; | ||
import { Plus, SmileIcon } from "lucide-react"; | ||
|
||
interface ChatInputProps { | ||
apiUrl: string; | ||
query: Record<string, any>; | ||
name: string; | ||
type: "channel" | "conversation"; | ||
} | ||
|
||
const formSchema = z.object({ | ||
content: z.string().min(1), | ||
}); | ||
|
||
export const ChatInput = ({ apiUrl, query, name, type }: ChatInputProps) => { | ||
const form = useForm<z.infer<typeof formSchema>>({ | ||
defaultValues: { | ||
content: "", | ||
}, | ||
resolver: zodResolver(formSchema), | ||
}); | ||
|
||
const isLoading = form.formState.isSubmitting; | ||
|
||
const onSubmit = async (values: z.infer<typeof formSchema>) => { | ||
try { | ||
const url = qs.stringifyUrl({ | ||
url: apiUrl, | ||
query, | ||
}); | ||
await axios.post(url, values); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}; | ||
|
||
return ( | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)}> | ||
<FormField | ||
control={form.control} | ||
name="content" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormControl> | ||
<div className="relative p-4 pb-6"> | ||
<button | ||
typeof="button" | ||
onClick={() => {}} | ||
className="absolute top-7 left-8 h-[24px] w-[24px] bg-zinc-500 dark:bg-zinc-400 hover:bg-zinc-600 dark:hover:bg-zinc-300 transition rounded-full p-1 flex items-center justify-center" | ||
> | ||
<Plus className="text-white dark:text-[#313338]" /> | ||
</button> | ||
<Input | ||
disabled={isLoading} | ||
{...field} | ||
placeholder={`Message ${ | ||
type === "conversation" ? name : `#${name}` | ||
}`} | ||
className="py-6 border-0 border-none px-14 bg-zinc-200/90 dark:bg-zinc-700/75 focus-visible:ring-0 focus-visible:ring-offset-0 text-zinc-600 dark:text-zinc-200" | ||
/> | ||
<div className="absolute top-7 right-8"> | ||
<SmileIcon /> | ||
</div> | ||
</div> | ||
</FormControl> | ||
</FormItem> | ||
)} | ||
/> | ||
</form> | ||
</Form> | ||
); | ||
}; |
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,53 @@ | ||
"use client"; | ||
|
||
import { createContext, useContext, useEffect, useState } from "react"; | ||
import { io as ClientIO } from "socket.io-client"; | ||
|
||
type SocketContextType = { | ||
socket: any | null; | ||
isConnected: boolean; | ||
}; | ||
|
||
const SocketContext = createContext<SocketContextType>({ | ||
socket: null, | ||
isConnected: false, | ||
}); | ||
|
||
export const useSocket = () => { | ||
return useContext(SocketContext); | ||
}; | ||
|
||
export const SocketProvider = ({ children }: { children: React.ReactNode }) => { | ||
const [socket, setSocket] = useState(null); | ||
const [isConnected, setIsConnected] = useState(false); | ||
|
||
useEffect(() => { | ||
const socketInstance = new (ClientIO as any)( | ||
process.env.NEXT_PUBLIC_SITE_URL!, | ||
{ | ||
path: "/api/socket/io", | ||
addTrailingSlash: false, | ||
} | ||
); | ||
|
||
socketInstance.on("connect", () => { | ||
setIsConnected(true); | ||
}); | ||
|
||
socketInstance.on("disconnect", () => { | ||
setIsConnected(false); | ||
}); | ||
|
||
setSocket(socketInstance); | ||
|
||
return () => { | ||
socketInstance.disconnect(); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<SocketContext.Provider value={{ socket, isConnected }}> | ||
{children} | ||
</SocketContext.Provider> | ||
); | ||
}; |
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,23 @@ | ||
"use client"; | ||
|
||
import { useSocket } from "@/components/providers/socket-provider"; | ||
|
||
import { Badge } from "@/components/ui/badge"; | ||
|
||
export const SocketIndicator = () => { | ||
const { isConnected } = useSocket(); | ||
|
||
if (!isConnected) { | ||
return ( | ||
<Badge variant="outline" className="text-white bg-yellow-600 border-none"> | ||
Fallback: Polling every 1s | ||
</Badge> | ||
); | ||
} | ||
|
||
return ( | ||
<Badge variant="outline" className="text-white border-none bg-emerald-600"> | ||
Live: Real-time updates | ||
</Badge> | ||
); | ||
}; |
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,36 @@ | ||
import * as React from "react" | ||
import { cva, type VariantProps } from "class-variance-authority" | ||
|
||
import { cn } from "@/lib/utils" | ||
|
||
const badgeVariants = cva( | ||
"inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2", | ||
{ | ||
variants: { | ||
variant: { | ||
default: | ||
"border-transparent bg-primary text-primary-foreground hover:bg-primary/80", | ||
secondary: | ||
"border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80", | ||
destructive: | ||
"border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80", | ||
outline: "text-foreground", | ||
}, | ||
}, | ||
defaultVariants: { | ||
variant: "default", | ||
}, | ||
} | ||
) | ||
|
||
export interface BadgeProps | ||
extends React.HTMLAttributes<HTMLDivElement>, | ||
VariantProps<typeof badgeVariants> {} | ||
|
||
function Badge({ className, variant, ...props }: BadgeProps) { | ||
return ( | ||
<div className={cn(badgeVariants({ variant }), className)} {...props} /> | ||
) | ||
} | ||
|
||
export { Badge, badgeVariants } |
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.
c3cbd82
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
discord-clone – ./
discord-clone-yubaneupane.vercel.app
discord-clone-nine-topaz.vercel.app
discord-clone-git-main-yubaneupane.vercel.app