|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { CardComment } from '../../../types/comments' |
| 3 | +
|
| 4 | +const props = defineProps<{ |
| 5 | + topLevelComments: CardComment[] |
| 6 | + editingCommentId: string | null |
| 7 | + editingCommentContent: string |
| 8 | + replyDraftByParent: Record<string, string> |
| 9 | + canEditCommentFn: (comment: CardComment) => boolean |
| 10 | + getRepliesFn: (parentCommentId: string) => CardComment[] |
| 11 | +}>() |
| 12 | +
|
| 13 | +const emit = defineEmits<{ |
| 14 | + (e: 'add-comment', parentCommentId?: string): void |
| 15 | + (e: 'start-edit-comment', comment: CardComment): void |
| 16 | + (e: 'cancel-edit-comment'): void |
| 17 | + (e: 'save-edit-comment', commentId: string): void |
| 18 | + (e: 'delete-comment', comment: CardComment): void |
| 19 | + (e: 'update:editingCommentContent', value: string): void |
| 20 | + (e: 'update:replyDraftByParent', value: Record<string, string>): void |
| 21 | +}>() |
| 22 | +
|
| 23 | +const newCommentContent = defineModel<string>('newCommentContent', { required: true }) |
| 24 | +
|
| 25 | +function updateReplyDraft(commentId: string, value: string) { |
| 26 | + const updated = { ...props.replyDraftByParent, [commentId]: value } |
| 27 | + emit('update:replyDraftByParent', updated) |
| 28 | +} |
| 29 | +</script> |
| 30 | + |
| 31 | +<template> |
| 32 | + <div class="pt-4 border-t border-outline-variant/30 space-y-3"> |
| 33 | + <h3 class="text-sm font-semibold text-on-surface">Comments</h3> |
| 34 | + <div class="space-y-2"> |
| 35 | + <textarea |
| 36 | + id="new-card-comment" |
| 37 | + v-model="newCommentContent" |
| 38 | + rows="2" |
| 39 | + class="w-full px-3 py-2 bg-surface-container-high border border-outline-variant/40 rounded-md text-on-surface placeholder-on-surface-variant/50 focus:outline-none focus:ring-2 focus:ring-primary/50" |
| 40 | + placeholder="Write a comment... Use @username to mention teammates." |
| 41 | + ></textarea> |
| 42 | + <div class="flex justify-end"> |
| 43 | + <button |
| 44 | + id="add-card-comment" |
| 45 | + type="button" |
| 46 | + class="px-3 py-1.5 text-sm font-medium text-on-primary-container bg-primary-container hover:brightness-110 disabled:opacity-40 disabled:cursor-not-allowed rounded-md transition-all" |
| 47 | + :disabled="newCommentContent.trim().length === 0" |
| 48 | + @click="$emit('add-comment')" |
| 49 | + > |
| 50 | + Add Comment |
| 51 | + </button> |
| 52 | + </div> |
| 53 | + </div> |
| 54 | + |
| 55 | + <div v-if="topLevelComments.length === 0" class="text-sm text-on-surface-variant italic"> |
| 56 | + No comments yet. |
| 57 | + </div> |
| 58 | + |
| 59 | + <div v-else class="space-y-3"> |
| 60 | + <div |
| 61 | + v-for="comment in topLevelComments" |
| 62 | + :key="comment.id" |
| 63 | + class="border border-outline-variant/30 rounded-md p-3 space-y-2 bg-surface-container-low" |
| 64 | + > |
| 65 | + <div class="flex items-start justify-between gap-2"> |
| 66 | + <div class="text-xs text-on-surface-variant"> |
| 67 | + <span class="font-medium text-on-surface">{{ comment.authorUsername }}</span> |
| 68 | + <span class="mx-1">•</span> |
| 69 | + <span>{{ new Date(comment.createdAt).toLocaleString() }}</span> |
| 70 | + <span v-if="comment.editedAt" class="ml-1 italic">(edited)</span> |
| 71 | + </div> |
| 72 | + <div v-if="canEditCommentFn(comment) && !comment.isDeleted" class="flex gap-2 text-xs"> |
| 73 | + <button |
| 74 | + type="button" |
| 75 | + class="text-primary hover:text-primary/80" |
| 76 | + @click="$emit('start-edit-comment', comment)" |
| 77 | + > |
| 78 | + Edit |
| 79 | + </button> |
| 80 | + <button |
| 81 | + type="button" |
| 82 | + class="text-error hover:text-error/80" |
| 83 | + @click="$emit('delete-comment', comment)" |
| 84 | + > |
| 85 | + Delete |
| 86 | + </button> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + |
| 90 | + <div v-if="editingCommentId === comment.id" class="space-y-2"> |
| 91 | + <textarea |
| 92 | + :value="editingCommentContent" |
| 93 | + aria-label="Edit comment" |
| 94 | + rows="2" |
| 95 | + class="w-full px-3 py-2 bg-surface-container-high border border-outline-variant/40 rounded-md text-on-surface focus:outline-none focus:ring-2 focus:ring-primary/50" |
| 96 | + @input="$emit('update:editingCommentContent', ($event.target as HTMLTextAreaElement).value)" |
| 97 | + ></textarea> |
| 98 | + <div class="flex justify-end gap-2"> |
| 99 | + <button |
| 100 | + type="button" |
| 101 | + class="px-3 py-1.5 text-sm text-on-surface-variant border border-outline-variant/40 rounded-md hover:bg-surface-container-high transition-colors" |
| 102 | + @click="$emit('cancel-edit-comment')" |
| 103 | + > |
| 104 | + Cancel |
| 105 | + </button> |
| 106 | + <button |
| 107 | + type="button" |
| 108 | + class="px-3 py-1.5 text-sm text-on-primary-container bg-primary-container rounded-md hover:brightness-110 disabled:opacity-40" |
| 109 | + :disabled="editingCommentContent.trim().length === 0" |
| 110 | + @click="$emit('save-edit-comment', comment.id)" |
| 111 | + > |
| 112 | + Save |
| 113 | + </button> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + |
| 117 | + <p |
| 118 | + v-else |
| 119 | + class="text-sm whitespace-pre-wrap" |
| 120 | + :class="comment.isDeleted ? 'text-on-surface-variant italic' : 'text-on-surface'" |
| 121 | + > |
| 122 | + {{ comment.content }} |
| 123 | + </p> |
| 124 | + |
| 125 | + <div class="pl-3 border-l-2 border-outline-variant/30 space-y-2"> |
| 126 | + <div |
| 127 | + v-for="reply in getRepliesFn(comment.id)" |
| 128 | + :key="reply.id" |
| 129 | + class="space-y-1" |
| 130 | + > |
| 131 | + <div class="text-xs text-on-surface-variant"> |
| 132 | + <span class="font-medium text-on-surface">{{ reply.authorUsername }}</span> |
| 133 | + <span class="mx-1">•</span> |
| 134 | + <span>{{ new Date(reply.createdAt).toLocaleString() }}</span> |
| 135 | + </div> |
| 136 | + <p |
| 137 | + class="text-sm whitespace-pre-wrap" |
| 138 | + :class="reply.isDeleted ? 'text-on-surface-variant italic' : 'text-on-surface'" |
| 139 | + > |
| 140 | + {{ reply.content }} |
| 141 | + </p> |
| 142 | + </div> |
| 143 | + |
| 144 | + <div v-if="!comment.isDeleted" class="space-y-2 pt-1"> |
| 145 | + <textarea |
| 146 | + :value="replyDraftByParent[comment.id] ?? ''" |
| 147 | + aria-label="Reply to comment" |
| 148 | + rows="2" |
| 149 | + class="w-full px-3 py-2 bg-surface-container-high border border-outline-variant/40 rounded-md text-on-surface placeholder-on-surface-variant/50 focus:outline-none focus:ring-2 focus:ring-primary/50" |
| 150 | + placeholder="Reply..." |
| 151 | + @input="updateReplyDraft(comment.id, ($event.target as HTMLTextAreaElement).value)" |
| 152 | + ></textarea> |
| 153 | + <div class="flex justify-end"> |
| 154 | + <button |
| 155 | + type="button" |
| 156 | + class="px-3 py-1.5 text-sm font-medium text-on-primary-container bg-primary-container hover:brightness-110 disabled:opacity-40 disabled:cursor-not-allowed rounded-md transition-all" |
| 157 | + :disabled="!(replyDraftByParent[comment.id] ?? '').trim().length" |
| 158 | + @click="$emit('add-comment', comment.id)" |
| 159 | + > |
| 160 | + Reply |
| 161 | + </button> |
| 162 | + </div> |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | +</template> |
0 commit comments