Skip to content

Commit 68d7345

Browse files
committed
fix: comments on layout page
1 parent 8b910e2 commit 68d7345

3 files changed

Lines changed: 86 additions & 10 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
export const isNavigatingThroughNav = $state({
22
value: false
33
});
4+
5+
export const showComments = $state({
6+
value: false
7+
})

platforms/metagram/src/routes/(protected)/+layout.svelte

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,55 @@
11
<script lang="ts">
22
import { goto } from '$app/navigation';
33
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';
56
import SideBar from '$lib/fragments/SideBar/SideBar.svelte';
67
import { Settings } from '$lib/icons';
8+
import { showComments } from '$lib/store/store.svelte';
9+
import type { CommentType } from '$lib/types';
710
let { children } = $props();
811
912
let route = $derived(page.url.pathname);
1013
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+
};
1153
1254
$effect(() => {
1355
if (route.includes('home')) {
@@ -28,9 +70,9 @@
2870
});
2971
</script>
3072

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`}>
3274
<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">
3476
<div class="flex items-center justify-between">
3577
<Header variant="primary" {heading} />
3678
{#if route === '/profile'}
@@ -47,5 +89,33 @@
4789
</div>
4890
{@render children()}
4991
</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}
50120
<BottomNav profileSrc="https://picsum.photos/200" />
51121
</main>

platforms/metagram/src/routes/(protected)/home/+page.svelte

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import type { CupertinoPane } from 'cupertino-pane';
66
import { Comment, MessageInput } from '$lib/fragments';
77
import type { CommentType, PostData } from '$lib/types';
8+
import { showComments } from '$lib/store/store.svelte';
89
910
let listElement: HTMLElement;
1011
let visiblePosts: PostData[] = $state([]);
@@ -82,8 +83,9 @@
8283
});
8384
</script>
8485

85-
<ul bind:this={listElement} class="hide-scrollbar h-[100dvh] overflow-auto">
86-
{#each visiblePosts as post}
86+
87+
<ul bind:this={listElement} class="hide-scrollbar h-[100vh] overflow-auto">
88+
{#each visiblePosts as post}
8789
<li class="mb-6">
8890
<Post
8991
avatar={post.avatar}
@@ -99,19 +101,19 @@
99101
if (window.matchMedia('(max-width: 768px)').matches) {
100102
drawer?.present({ animate: true });
101103
} else {
102-
// comments to be shown in right aside
104+
showComments.value = true
103105
}
104106
},
105107
menu: () => alert('menu')
106108
}}
107109
/>
108110
</li>
109-
{/each}
110-
</ul>
111+
{/each}
112+
</ul>
111113

112-
{#if loading}
114+
{#if loading}
113115
<p class="my-4 text-center">Loading more posts…</p>
114-
{/if}
116+
{/if}
115117

116118
<Drawer bind:drawer>
117119
<ul class="pb-4">

0 commit comments

Comments
 (0)