|
| 1 | +<script lang="ts"> |
| 2 | + import { Like } from '$lib/icons'; |
| 3 | + import { Avatar } from '$lib/ui'; |
| 4 | + import { cn } from '$lib/utils'; |
| 5 | + import type { HTMLAttributes } from 'svelte/elements'; |
| 6 | + import type { CommentType } from '$lib/types'; |
| 7 | +
|
| 8 | + interface ICommentProps extends HTMLAttributes<HTMLElement> { |
| 9 | + comment: CommentType; |
| 10 | + handleReply: () => void; |
| 11 | + } |
| 12 | +
|
| 13 | + let visibleReplies = $state(2); |
| 14 | +
|
| 15 | + const showMoreReplies = () => { |
| 16 | + visibleReplies = comment.replies.length; |
| 17 | + }; |
| 18 | +
|
| 19 | + let { comment, handleReply, ...restProps }: ICommentProps = $props(); |
| 20 | +</script> |
| 21 | + |
| 22 | +<article {...restProps} class={cn([restProps.class].join(' '))}> |
| 23 | + <div class="align-start flex gap-2"> |
| 24 | + <Avatar src={comment.userImgSrc} size="sm" /> |
| 25 | + <div> |
| 26 | + <h3 class="font-semibold text-black">{comment.name}</h3> |
| 27 | + <p class="text-black-600 mt-0.5">{comment.comment}</p> |
| 28 | + </div> |
| 29 | + </div> |
| 30 | + <div class="ms-12 mt-2 flex items-center gap-2"> |
| 31 | + <button |
| 32 | + onclick={() => { |
| 33 | + if (!comment.isUpVoted) { |
| 34 | + comment.upVotes++; |
| 35 | + comment.isUpVoted = true; |
| 36 | + comment.isDownVoted = false; |
| 37 | + } |
| 38 | + }} |
| 39 | + > |
| 40 | + <Like |
| 41 | + size="18px" |
| 42 | + color={comment.isUpVoted |
| 43 | + ? 'var(--color-brand-burnt-orange)' |
| 44 | + : 'var(--color-black-600)'} |
| 45 | + fill={comment.isUpVoted |
| 46 | + ? 'var(--color-brand-burnt-orange)' |
| 47 | + : 'var(--color-black-600)'} |
| 48 | + /> |
| 49 | + </button> |
| 50 | + <p class="text-black-600 font-semibold">{comment.upVotes}</p> |
| 51 | + <button |
| 52 | + onclick={() => { |
| 53 | + if (!comment.isDownVoted) { |
| 54 | + comment.upVotes--; |
| 55 | + comment.isDownVoted = true; |
| 56 | + comment.isUpVoted = false; |
| 57 | + } |
| 58 | + }} |
| 59 | + > |
| 60 | + <Like |
| 61 | + size="18px" |
| 62 | + color={comment.isDownVoted |
| 63 | + ? 'var(--color-brand-burnt-orange)' |
| 64 | + : 'var(--color-black-600)'} |
| 65 | + fill={comment.isDownVoted |
| 66 | + ? 'var(--color-brand-burnt-orange)' |
| 67 | + : 'var(--color-black-600)'} |
| 68 | + class="rotate-180" |
| 69 | + /> |
| 70 | + </button> |
| 71 | + <span class="bg-black-600 inline-block h-1 w-1 rounded-full"></span> |
| 72 | + <button onclick={handleReply} class="text-black-600 font-semibold">Reply</button> |
| 73 | + <span class="bg-black-600 inline-block h-1 w-1 rounded-full"></span> |
| 74 | + <p class="text-black-600">{comment.time}</p> |
| 75 | + </div> |
| 76 | + {#if comment?.replies?.length} |
| 77 | + <ul class="ms-12 mt-4 space-y-2"> |
| 78 | + {#each comment.replies.slice(0, visibleReplies) as reply} |
| 79 | + <li> |
| 80 | + <div class="align-start flex gap-2"> |
| 81 | + <Avatar src={reply.userImgSrc} size="sm" /> |
| 82 | + <div> |
| 83 | + <h3 class="font-semibold text-black">{reply.name}</h3> |
| 84 | + <p class="text-black-600 mt-0.5">{reply.comment}</p> |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + <div class="ms-12 mt-2 flex items-center gap-2"> |
| 88 | + <button |
| 89 | + onclick={() => { |
| 90 | + if (!reply.isUpVoted) { |
| 91 | + reply.upVotes++; |
| 92 | + reply.isUpVoted = true; |
| 93 | + reply.isDownVoted = false; |
| 94 | + } |
| 95 | + }} |
| 96 | + > |
| 97 | + <Like |
| 98 | + size="18px" |
| 99 | + color={reply.isUpVoted |
| 100 | + ? 'var(--color-brand-burnt-orange)' |
| 101 | + : 'var(--color-black-600)'} |
| 102 | + fill={reply.isUpVoted |
| 103 | + ? 'var(--color-brand-burnt-orange)' |
| 104 | + : 'var(--color-black-600)'} |
| 105 | + /> |
| 106 | + </button> |
| 107 | + <p class="text-black-600 font-semibold">{reply.upVotes}</p> |
| 108 | + <button |
| 109 | + onclick={() => { |
| 110 | + if (!reply.isDownVoted) { |
| 111 | + reply.upVotes--; |
| 112 | + reply.isDownVoted = true; |
| 113 | + reply.isUpVoted = false; |
| 114 | + } |
| 115 | + }} |
| 116 | + > |
| 117 | + <Like |
| 118 | + size="18px" |
| 119 | + color={reply.isDownVoted |
| 120 | + ? 'var(--color-brand-burnt-orange)' |
| 121 | + : 'var(--color-black-600)'} |
| 122 | + fill={reply.isDownVoted |
| 123 | + ? 'var(--color-brand-burnt-orange)' |
| 124 | + : 'var(--color-black-600)'} |
| 125 | + class="rotate-180" |
| 126 | + /> |
| 127 | + </button> |
| 128 | + <span class="bg-black-600 inline-block h-1 w-1 rounded-full"></span> |
| 129 | + <button onclick={handleReply} class="text-black-600 font-semibold" |
| 130 | + >Reply</button |
| 131 | + > |
| 132 | + <span class="bg-black-600 inline-block h-1 w-1 rounded-full"></span> |
| 133 | + <p class="text-black-600">{reply.time}</p> |
| 134 | + </div> |
| 135 | + </li> |
| 136 | + {/each} |
| 137 | + {#if comment.replies.length > visibleReplies} |
| 138 | + <button |
| 139 | + onclick={showMoreReplies} |
| 140 | + class="text-brand-burnt-orange mt-1 text-sm font-medium" |
| 141 | + > |
| 142 | + See {comment.replies.length - visibleReplies} more replies |
| 143 | + </button> |
| 144 | + {/if} |
| 145 | + </ul> |
| 146 | + {/if} |
| 147 | +</article> |
0 commit comments