-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug fix of dm in create messages endpoint in convex and chat input, m…
…essage list for 1:1 convo dm added
- Loading branch information
Showing
5 changed files
with
132 additions
and
19 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
95 changes: 95 additions & 0 deletions
95
src/app/workspace/[workspaceId]/member/[memberId]/chat-input.tsx
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,95 @@ | ||
import { useCreateMessage } from "@/features/messages/api/use-create-messages"; | ||
import { useGenerateUploadUrl } from "@/features/upload/api/use-generate-upload-url"; | ||
|
||
import { useWorkspaceId } from "@/hooks/use-workspace-id"; | ||
import dynamic from "next/dynamic"; | ||
import Quill from "quill"; | ||
import { useRef, useState } from "react"; | ||
import { toast } from "sonner"; | ||
import { Id } from "../../../../../../convex/_generated/dataModel"; | ||
|
||
const Editor = dynamic(() => import("@/components/editor"), { ssr: false }); | ||
|
||
interface ChatInputProps { | ||
placeholder: string; | ||
conversationId: Id<"conversations">; | ||
} | ||
|
||
type CreateMessageValues = { | ||
// channelId: Id<"channels">; | ||
conversationId: Id<"conversations">; | ||
workspaceId: Id<"workspaces">; | ||
body: string; | ||
image?: Id<"_storage"> | undefined; | ||
}; | ||
|
||
export const ChatInput = ({ placeholder, conversationId }: ChatInputProps) => { | ||
const [editorKey, setEditorKey] = useState(0); | ||
const [pending, setPending] = useState(false); | ||
|
||
const editorRef = useRef<Quill | null>(null); | ||
|
||
const workspaceId = useWorkspaceId(); | ||
|
||
const { mutate: generateUploadUrl } = useGenerateUploadUrl(); | ||
const { mutate: createMessage } = useCreateMessage(); | ||
|
||
const handleSubmit = async ({ | ||
body, | ||
image, | ||
}: { | ||
body: string; | ||
image: File | null; | ||
}) => { | ||
console.log(body, image); | ||
try { | ||
setPending(true); | ||
editorRef?.current?.enable(false); | ||
|
||
const values: CreateMessageValues = { | ||
conversationId, | ||
workspaceId, | ||
body, | ||
image: undefined, | ||
}; | ||
|
||
if (image) { | ||
const url = await generateUploadUrl({}, { throwError: true }); | ||
|
||
if (!url) throw new Error("Url not found"); | ||
|
||
const result = await fetch(url, { | ||
method: "POST", | ||
headers: { "Content-Type": image.type }, | ||
body: image, | ||
}); | ||
|
||
if (!result.ok) throw new Error("Failed to upload image"); | ||
|
||
const { storageId } = await result.json(); | ||
|
||
values.image = storageId; | ||
} | ||
|
||
await createMessage(values, { throwError: true }); | ||
|
||
setEditorKey((prevKey) => prevKey + 1); | ||
} catch (error) { | ||
toast.error("Failed to send message"); | ||
} finally { | ||
setPending(false); | ||
editorRef?.current?.enable(true); | ||
} | ||
}; | ||
return ( | ||
<div className="px-5 w-full"> | ||
<Editor | ||
key={editorKey} | ||
placeholder={placeholder} | ||
onSubmit={handleSubmit} | ||
disabled={pending} | ||
innerRef={editorRef} | ||
/> | ||
</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
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