import { useContext, useLayoutEffect, useRef } from 'react' import { PktButton } from '../../button/Button' import { PktIcon } from '../../icon/Icon' import { TFileAttributes, FileItem, PktFileUploadContext, TQueueItemOperation } from '../types' import { PktTextarea } from '../../textarea/Textarea' const COMMENT_SYMBOL = Symbol('comment') export interface IComment { text: string timestamp: Date } const formatTimestamp = (date: Date): string => { const day = String(date.getDate()).padStart(2, '0') const month = String(date.getMonth() + 1).padStart(2, '0') const year = date.getFullYear() const hours = String(date.getHours()).padStart(2, '0') const minutes = String(date.getMinutes()).padStart(2, '0') return `${day}.${month}.${year} kl. ${hours}:${minutes}` } const AddComment = ({ fileItem, closeOperationUi, onAddComment, existingComment, }: { fileItem: FileItem closeOperationUi: () => void onAddComment: (comment: IComment) => void existingComment?: IComment }) => { const inputRef = useRef(null) const isEditing = !!existingComment const handleAddComment = () => { const text = inputRef.current?.value?.trim() if (text) { onAddComment({ text, timestamp: new Date(), }) } closeOperationUi() } useLayoutEffect(() => { const t = setTimeout(() => inputRef.current?.focus({ preventScroll: true }), 0) return () => clearTimeout(t) }, [fileItem.fileId]) return ( <> {isEditing ? 'Lagre kommentar' : 'Legg til kommentar'} Avbryt ) } const ShowComments = ({ comments, onDeleteComment, onEditComment, }: { comments?: IComment[] onDeleteComment?: (index: number) => void onEditComment?: (index: number) => void }) => comments && comments.length > 0 ? (
{comments.map((comment, index) => (
{comment.text}
{onDeleteComment && ( )} {onEditComment && ( )}
))}
) : null const CommentHiddenInput = (props: { comments: IComment[] | undefined }) => { const context = useContext(PktFileUploadContext) return ( ) } export const addCommentOperation = (attributes: TFileAttributes): TQueueItemOperation => { const commentsAttribute = attributes('comments') const addComment = (fileId: string, newComment: IComment) => { commentsAttribute.set(fileId, [newComment]) } const deleteComment = (fileId: string, commentIndex: number) => { const existingComments = commentsAttribute.get(fileId) || [] const newComments = existingComments.filter((_, i) => i !== commentIndex) commentsAttribute.set(fileId, newComments.length > 0 ? newComments : undefined) } return { // Only show button text when no comment exists yet title: (fileItem: FileItem) => { const comments = commentsAttribute.get(fileItem.fileId) // If comment exists, return empty string to hide the button (icons are shown in renderContent) if (comments && comments.length > 0) return '' return 'Legg til kommentar' }, renderExtendedUI: (fileItem: FileItem, closeOperationUi: () => void) => { const existingComments = commentsAttribute.get(fileItem.fileId) const existingComment = existingComments?.[0] return ( addComment(fileItem.fileId, comment)} existingComment={existingComment} /> ) }, renderContent: (fileItem: FileItem, activateOperation?: () => void, isOperationActive?: boolean) => { // Hide comment preview when editing if (isOperationActive) return null const comments = commentsAttribute.get(fileItem.fileId) return ( deleteComment(fileItem.fileId, index)} onEditComment={activateOperation ? () => activateOperation() : undefined} /> ) }, renderHidden: (fileItem: FileItem) => ( ), symbol: COMMENT_SYMBOL, } }