11"use client" ;
22
3- import { Button } from "@comp/ui/button" ;
4- import { Textarea } from "@comp/ui/textarea" ;
5- import { ArrowUp , Loader2 , Paperclip } from "lucide-react" ;
63import { uploadFile } from "@/actions/files/upload-file" ;
74import { authClient } from "@/utils/auth-client" ;
8- import { AttachmentEntityType } from "@comp/db/types" ;
5+ import { AttachmentEntityType , CommentEntityType } from "@comp/db/types" ;
6+ import { Button } from "@comp/ui/button" ;
97import { Label } from "@comp/ui/label" ;
8+ import { Textarea } from "@comp/ui/textarea" ;
109import clsx from "clsx" ;
10+ import { ArrowUp , Loader2 , Paperclip } from "lucide-react" ;
1111import React , { useCallback , useRef , useState } from "react" ;
1212import { toast } from "sonner" ;
13+ import { useParams , useRouter } from "next/navigation" ;
1314import { createComment } from "../../actions/createComment" ;
14- import { AttachmentItem } from "./AttachmentItem" ;
15+ import { AttachmentItem } from "../../tasks/[taskId]/components /AttachmentItem" ;
1516
16- interface TaskCommentFormProps {
17- taskId : string ;
17+ interface CommentFormProps {
18+ entityId : string ;
19+ entityType : CommentEntityType ;
1820}
1921
2022interface PendingAttachment {
@@ -24,31 +26,28 @@ interface PendingAttachment {
2426 signedUrl : string | null ;
2527}
2628
27- function mapFileTypeToAttachmentType ( fileType : string ) : string {
28- const type = fileType . split ( "/" ) [ 0 ] ;
29- switch ( type ) {
30- case "image" :
31- return "image" ;
32- case "video" :
33- return "video" ;
34- case "audio" :
35- return "audio" ;
36- case "application" :
37- if ( fileType === "application/pdf" ) return "document" ; // Specific PDF check
38- return "document" ;
39- default :
40- return "other" ;
41- }
42- }
43-
44- export function TaskCommentForm ( { taskId } : TaskCommentFormProps ) {
29+ export function CommentForm ( { entityId, entityType } : CommentFormProps ) {
4530 const session = authClient . useSession ( ) ;
4631 const [ newComment , setNewComment ] = useState ( "" ) ;
4732 const [ pendingAttachments , setPendingAttachments ] = useState <
4833 PendingAttachment [ ]
4934 > ( [ ] ) ;
5035 const [ isUploading , setIsUploading ] = useState ( false ) ;
5136 const fileInputRef = useRef < HTMLInputElement > ( null ) ;
37+ const { orgId } = useParams < { orgId : string } > ( ) ;
38+
39+ let pathToRevalidate = "" ;
40+ switch ( entityType ) {
41+ case "task" :
42+ pathToRevalidate = `/${ orgId } /tasks/${ entityId } ` ;
43+ break ;
44+ case "vendor" :
45+ pathToRevalidate = `/${ orgId } /vendors/${ entityId } ` ;
46+ break ;
47+ case "risk" :
48+ pathToRevalidate = `/${ orgId } /risks/${ entityId } ` ;
49+ break ;
50+ }
5251
5352 const triggerFileInput = ( ) => {
5453 fileInputRef . current ?. click ( ) ;
@@ -93,8 +92,9 @@ export function TaskCommentForm({ taskId }: TaskCommentFormProps) {
9392 fileName : file . name ,
9493 fileType : file . type ,
9594 fileData : base64Data ,
96- entityId : taskId ,
97- entityType : AttachmentEntityType . comment ,
95+ entityId,
96+ entityType : entityType as AttachmentEntityType ,
97+ pathToRevalidate,
9898 } ) ;
9999 if ( error ) {
100100 console . error (
@@ -145,7 +145,7 @@ export function TaskCommentForm({ taskId }: TaskCommentFormProps) {
145145 if ( fileInputRef . current ) fileInputRef . current . value = "" ;
146146 } ) ( ) ;
147147 } ,
148- [ taskId , pendingAttachments . length ] ,
148+ [ entityId , entityType , pendingAttachments . length ] ,
149149 ) ;
150150
151151 const handleRemovePendingAttachment = ( attachmentIdToRemove : string ) => {
@@ -194,8 +194,10 @@ export function TaskCommentForm({ taskId }: TaskCommentFormProps) {
194194
195195 const { success, data, error } = await createComment ( {
196196 content : newComment ,
197- taskId : taskId ,
197+ entityId,
198+ entityType,
198199 attachmentIds : pendingAttachments . map ( ( att ) => att . id ) ,
200+ pathToRevalidate,
199201 } ) ;
200202
201203 if ( success && data ) {
0 commit comments