Skip to content

Commit 446a0ae

Browse files
committed
joining with profiles instead of user
1 parent 227bef3 commit 446a0ae

File tree

6 files changed

+26
-22
lines changed

6 files changed

+26
-22
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
"sideEffects": false,
55
"type": "module",
66
"scripts": {
7-
"dev": "vite dev",
7+
"dev": "npm run docker:up && vite dev",
88
"build": "vite build",
99
"start": "npm run db:migrate && node .output/server/index.mjs",
10+
"docker:up": "docker compose up -d",
11+
"docker:down": "docker compose down",
1012
"db:push": "drizzle-kit push --config=drizzle.config.ts",
1113
"db:migrate": "tsx ./src/db/migrate.ts",
1214
"db:generate": "drizzle-kit generate --config=drizzle.config.ts",

src/data-access/comments.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ export async function getComments(segmentId: number) {
88
return database.query.comments.findMany({
99
where: and(eq(comments.segmentId, segmentId), isNull(comments.parentId)),
1010
with: {
11-
user: true,
11+
profile: true,
1212
children: {
1313
with: {
14-
user: true,
15-
repliedTo: true,
14+
profile: true,
15+
repliedToProfile: true,
1616
},
1717
},
1818
},

src/db/schema.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ export const segmentsRelations = relations(segments, ({ one, many }) => ({
175175
}));
176176

177177
export const commentsRelations = relations(comments, ({ one, many }) => ({
178-
user: one(users, {
178+
profile: one(profiles, {
179179
fields: [comments.userId],
180-
references: [users.id],
180+
references: [profiles.userId],
181181
}),
182182
segment: one(segments, {
183183
fields: [comments.segmentId],
@@ -191,9 +191,9 @@ export const commentsRelations = relations(comments, ({ one, many }) => ({
191191
children: many(comments, {
192192
relationName: "parent",
193193
}),
194-
repliedTo: one(users, {
194+
repliedToProfile: one(profiles, {
195195
fields: [comments.repliedToId],
196-
references: [users.id],
196+
references: [profiles.userId],
197197
}),
198198
}));
199199

src/hooks/mutations/use-create-comment.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@ export function useCreateComment() {
2323
const newComment: CommentsWithUser[number] = {
2424
id: Math.random(),
2525
content: variables.content,
26-
user: user,
2726
userId: user.id,
27+
profile: {
28+
id: user.id,
29+
displayName: user.email,
30+
image: null,
31+
userId: user.id,
32+
imageId: null,
33+
bio: "",
34+
},
2835
segmentId: segment.id,
2936
createdAt: new Date(),
3037
updatedAt: new Date(),

src/routes/learn/$slug/-components/comment-list.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function CommentItem({ comment, level = 0 }: CommentItemProps) {
6363

6464
const isEditingThis = editingCommentId === comment.id;
6565
const isReplyingToThis = replyingToCommentId === comment.id;
66-
const isOwner = user && comment.user.email === user.email;
66+
const isOwner = user && comment.profile.userId === user.id;
6767

6868
const handleEdit = (commentId: number) => {
6969
setEditingCommentId(commentId);
@@ -153,7 +153,7 @@ function CommentItem({ comment, level = 0 }: CommentItemProps) {
153153
segmentId: segment.id,
154154
content: replyContent,
155155
parentId: rootParentId,
156-
repliedToId: comment.user.id,
156+
repliedToId: comment.profile.userId,
157157
},
158158
{
159159
onSuccess: () => {
@@ -200,7 +200,7 @@ function CommentItem({ comment, level = 0 }: CommentItemProps) {
200200
<div className="flex shrink-0 size-10 rounded-full overflow-hidden bg-gradient-to-br from-theme-100 to-theme-200 dark:from-theme-800 dark:to-theme-700 shadow-elevation-1 hover:shadow-elevation-2 transition-all duration-200">
201201
<img
202202
className="max-h-10 w-auto object-cover"
203-
src={`https://api.dicebear.com/9.x/initials/svg?seed=${comment.user.email}&backgroundColor=6366f1&textColor=ffffff`}
203+
src={`https://api.dicebear.com/9.x/initials/svg?seed=${comment.profile.displayName}&backgroundColor=6366f1&textColor=ffffff`}
204204
alt="User avatar"
205205
/>
206206
</div>
@@ -210,7 +210,7 @@ function CommentItem({ comment, level = 0 }: CommentItemProps) {
210210
<div className="flex items-start justify-between gap-2">
211211
<div className="flex items-center gap-2 min-w-0 flex-wrap">
212212
<p className="text-sm font-semibold text-foreground truncate">
213-
{comment.user.email}
213+
{comment.profile.displayName}
214214
</p>
215215

216216
{/* Visual separator */}
@@ -221,7 +221,7 @@ function CommentItem({ comment, level = 0 }: CommentItemProps) {
221221
</p>
222222

223223
{/* Reply indicator */}
224-
{comment.repliedTo && (
224+
{comment.repliedToProfile && (
225225
<>
226226
<span className="text-xs text-muted-foreground/60">
227227
@@ -231,7 +231,7 @@ function CommentItem({ comment, level = 0 }: CommentItemProps) {
231231
<span className="text-xs text-muted-foreground">
232232
replying to{" "}
233233
<span className="font-medium text-theme-600 dark:text-theme-400 hover:text-theme-700 dark:hover:text-theme-300 transition-colors">
234-
{comment.repliedTo.email}
234+
{comment.repliedToProfile.displayName}
235235
</span>
236236
</span>
237237
</div>
@@ -350,7 +350,7 @@ function CommentItem({ comment, level = 0 }: CommentItemProps) {
350350
<div className="flex shrink-0 size-8 rounded-full overflow-hidden bg-gradient-to-br from-theme-100 to-theme-200 dark:from-theme-800 dark:to-theme-700 shadow-elevation-1">
351351
<img
352352
className="max-h-8 w-auto object-cover"
353-
src={`https://api.dicebear.com/9.x/initials/svg?seed=${user?.email || "user"}&backgroundColor=6366f1&textColor=ffffff`}
353+
src={`https://api.dicebear.com/9.x/initials/svg?seed=${user?.id || "user"}&backgroundColor=6366f1&textColor=ffffff`}
354354
alt="Your avatar"
355355
/>
356356
</div>
@@ -450,10 +450,8 @@ function CommentItem({ comment, level = 0 }: CommentItemProps) {
450450
}
451451

452452
export function CommentList({
453-
showCommentForm,
454453
onStartDiscussion,
455454
}: {
456-
showCommentForm?: boolean;
457455
onStartDiscussion?: () => void;
458456
}) {
459457
const { segment } = useLoaderData({ from: "/learn/$slug/_layout/" });

src/routes/learn/$slug/-components/comments-panel.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ export function CommentsPanel({
9292
</div>
9393
}
9494
>
95-
<CommentList
96-
showCommentForm={showCommentForm}
97-
onStartDiscussion={onStartDiscussion}
98-
/>
95+
<CommentList onStartDiscussion={onStartDiscussion} />
9996
</Suspense>
10097
</div>
10198
</div>

0 commit comments

Comments
 (0)