|
1 | 1 | <script lang="ts"> |
2 | 2 | import { goto } from '$app/navigation'; |
3 | 3 | import { page } from '$app/state'; |
4 | | - import { BottomNav, Header } from '$lib/fragments'; |
| 4 | + import { comments } from '$lib/dummyData'; |
| 5 | + import { BottomNav, Header, Comment, MessageInput } from '$lib/fragments'; |
5 | 6 | import SideBar from '$lib/fragments/SideBar/SideBar.svelte'; |
6 | 7 | import { Settings } from '$lib/icons'; |
| 8 | + import { showComments } from '$lib/store/store.svelte'; |
| 9 | + import type { CommentType } from '$lib/types'; |
7 | 10 | let { children } = $props(); |
8 | 11 |
|
9 | 12 | let route = $derived(page.url.pathname); |
10 | 13 | let heading = $state(''); |
| 14 | + let commentValue: string = $state(''); |
| 15 | + let commentInput: HTMLInputElement | undefined = $state(); |
| 16 | + let _comments = $state(comments); |
| 17 | + let activeReplyToId: string | null = $state(null); |
| 18 | +
|
| 19 | + const handleSend = async () => { |
| 20 | + const newComment = { |
| 21 | + userImgSrc: 'https://www.gravatar.com/avatar/2c7d99fe281ecd3bcd65ab915bac6dd5?s=250', |
| 22 | + name: 'You', |
| 23 | + commentId: Date.now().toString(), |
| 24 | + comment: commentValue, |
| 25 | + isUpVoted: false, |
| 26 | + isDownVoted: false, |
| 27 | + upVotes: 0, |
| 28 | + time: 'Just now', |
| 29 | + replies: [] |
| 30 | + }; |
| 31 | +
|
| 32 | + if (activeReplyToId) { |
| 33 | + // Find the parent comment by id and push reply |
| 34 | + const addReplyToComment = (commentsArray: CommentType[]) => { |
| 35 | + for (const c of commentsArray) { |
| 36 | + if (c.commentId === activeReplyToId) { |
| 37 | + c.replies.push(newComment); |
| 38 | + return true; |
| 39 | + } else if (c.replies.length) { |
| 40 | + if (addReplyToComment(c.replies)) return true; |
| 41 | + } |
| 42 | + } |
| 43 | + return false; |
| 44 | + }; |
| 45 | + addReplyToComment(_comments); |
| 46 | + } else { |
| 47 | + // If no activeReplyToId, add as a new parent comment |
| 48 | + _comments = [newComment, ..._comments]; |
| 49 | + } |
| 50 | + commentValue = ''; |
| 51 | + activeReplyToId = null; |
| 52 | + }; |
11 | 53 |
|
12 | 54 | $effect(() => { |
13 | 55 | if (route.includes('home')) { |
|
28 | 70 | }); |
29 | 71 | </script> |
30 | 72 |
|
31 | | -<main class="block h-[100dvh] grid-cols-[20vw_auto_30vw] md:grid"> |
| 73 | +<main class={`block h-[100dvh] ${route !== "/home"? "grid-cols-[20vw_auto]" : "grid-cols-[20vw_auto_30vw]"} md:grid`}> |
32 | 74 | <SideBar profileSrc="https://picsum.photos/200" handlePost={async () => alert('adas')} /> |
33 | | - <section class="mx-4 md:mx-8 md:pt-8"> |
| 75 | + <section class="px-4 md:px-8 md:pt-8"> |
34 | 76 | <div class="flex items-center justify-between"> |
35 | 77 | <Header variant="primary" {heading} /> |
36 | 78 | {#if route === '/profile'} |
|
47 | 89 | </div> |
48 | 90 | {@render children()} |
49 | 91 | </section> |
| 92 | + {#if route === "/home"} |
| 93 | + <aside class="relative hidden md:block h-[100dvh] overflow-y-scroll hide-scrollbar px-8 pt-14 border border-s-gray-200 border-e-0 border-t-0 border-b-0"> |
| 94 | + {#if showComments.value} |
| 95 | + <ul class="pb-4"> |
| 96 | + <h3 class="text-black-600 mb-6 text-center">{comments.length} Comments</h3> |
| 97 | + {#each _comments as comment} |
| 98 | + <li class="mb-4"> |
| 99 | + <Comment |
| 100 | + {comment} |
| 101 | + handleReply={() => { |
| 102 | + activeReplyToId = comment.commentId; |
| 103 | + commentInput?.focus(); |
| 104 | + }} |
| 105 | + /> |
| 106 | + </li> |
| 107 | + {/each} |
| 108 | + <MessageInput |
| 109 | + class="sticky start-0 bottom-4 mt-4 w-full px-2" |
| 110 | + variant="comment" |
| 111 | + src="https://www.gravatar.com/avatar/2c7d99fe281ecd3bcd65ab915bac6dd5?s=250" |
| 112 | + bind:value={commentValue} |
| 113 | + {handleSend} |
| 114 | + bind:input={commentInput} |
| 115 | + /> |
| 116 | + </ul> |
| 117 | + {/if} |
| 118 | + </aside> |
| 119 | + {/if} |
50 | 120 | <BottomNav profileSrc="https://picsum.photos/200" /> |
51 | 121 | </main> |
0 commit comments